我正在尝试使用 JDT
的 Dom 方法添加对 StaticClassName.class
字段访问的调用,以访问现有类。
当我尝试使用 ast.newSimpleName("class")
创建简单名称时,出现 IllegalArgumentException
。
我认为这是因为 JDT
当它也用作字段名称时将其视为关键字。
是否有办法使JDT
接受“class”作为标识符名称或访问类对象的其他方式? (它必须在静态和非静态方法中工作)
最佳答案
如 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
(另请参阅 AST JDT core Dom javadoc )
关于eclipse - 在 JDT dom 中使用 class 作为字段名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1622160/