开发者

JDT ASTParser to get the value of a string field

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜