Using a class as a field name in JDT dom

I am trying to add a call to a field StaticClassName.classto an existing class using JDTDom methods .

I get IllegalArgumentExceptionwhen I try to create a simple name with ast.newSimpleName("class").
I think this is because it JDTtreats it as a keyword when it is also used as a field name.

Is there a way to force a JDT“class” to be accepted as the name of an identifier or another way to access the class object? (it should work in both static and non-stationary methods)

+3
source share
1 answer

As mentioned in this thread :

<Type>.classis not an ordinary simple name, but rather TypeLiteral. So I think your code should look like this:

TypeLiteral tr = ast.newTypeLiteral();
tr.setType(ast.newSimpleType(ast.newSimpleName("MyClass")));

As a result, the expression " Myclass.class" is created.

By the way, there is a really nice plugin ASTViewwith the appearance of an editable Java source file AST. This is very useful when determining the correct node types for different language operators. You can get here

(See also AST JDT core Dom javadoc )

+4
source

Source: https://habr.com/ru/post/1721031/


All Articles