开发者

JDT ASTParser to get the value of a string field

开发者 https://www.devze.com 2023-01-02 13:59 出处:网络
Is there a way to use jdt ASTParser to get the value of a String field declared in a java file. Actually what I need is to resolve any possible dependencies from other classe开发者_如何学Pythons e.g.

Is there a way to use jdt ASTParser to get the value of a String field declared in a java file. Actually what I need is to resolve any possible dependencies from other classe开发者_如何学Pythons e.g.

public String str = "somethig"+SomeTherClass.SOMETHING_ELSE.


I figured it out ...it was quite simple actually ..here's the code:

ICompilationUnit cu = ....
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setResolveBindings(true);
parser.setSource(cu);
parser.setStatementsRecovery(true);
parser.setBindingsRecovery(true);
ASTNode node = parser.createAST(null);
node.accept(new YourVisitor());

Then in your implementation of the ASTVisitor you need to call resolveConstantExpressionValue() on the node being visited.

0

精彩评论

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