tags:

views:

51

answers:

1

Hello, I'm trying to add a call to the StaticClassName.class field access to an existing class using 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)

+1  A: 

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)

VonC