I'm trying to add a call to the StaticClassName.class
field access to an existing class us开发者_开发知识库ing JDT
's Dom methods.
I get an IllegalArgumentException
when I try to create a simple name using ast.newSimpleName("class")
.
JDT
treats it as a keyword when it is also used as a field name.
Is there anyway to make the JDT
to accept "class" as an identifier name or another way of access the class object? (it has to work in both static and non static methods)
As mentioned in this thread:
<Type>.class
is not a usual simple name, but rather aTypeLiteral
. So I think your code should look more like this:
TypeLiteral tr = ast.newTypeLiteral();
tr.setType(ast.newSimpleType(ast.newSimpleName("MyClass")));
Which in result creates expression "
Myclass.class
".By the way, there is a really nice
ASTView
plugin, with view of currently edited Java source fileAST
. It's very helpful in determining what are correct node types for different language statements. You can get it from here
(See also AST JDT core Dom javadoc)
精彩评论