开发者

Using class as a field name in JDT dom

开发者 https://www.devze.com 2022-12-09 22:37 出处:网络
I\'m trying to add a call to the StaticClassName.class field access to an existing class us开发者_开发知识库ing JDT\'s Dom methods.

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").

I think this is because 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 a TypeLiteral. 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 file AST. 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)

0

精彩评论

暂无评论...
验证码 换一张
取 消