我正在编写一个使用 JDT AST 的 ASTParser
的 Eclipse 插件。解析一个方法。我正在该方法中寻找创建特定类型对象的方法。
当我找到 ClassInstanceCreation
, 我调用 getType()
在其上查看正在实例化的类型。我想确定正在处理的完全解析的类型是我认为的类型,所以我告诉结果 Type
反对resolveBinding()
.我得到 null
即使没有编译错误,即使我调用了 setResolveBindings(true)
,也会返回在我的 ASTParser
.我给了我的ASTParser
(通过 setSource()
)ICompilationUnit
包含我的方法,因此解析器可以访问整个工作区上下文。
final IMethod method = ...;
final ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setResolveBindings(true);
parser.setSource(method.getCompilationUnit());
parser.setSourceRange(method.getSourceRange().getOffset(), method.getSourceRange().getLength());
parser.setKind(ASTParser.K_CLASS_BODY_DECLARATIONS);
final TypeDeclaration astRoot = (TypeDeclaration) parser.createAST(null);
final ClassInstanceCreation classInstanceCreation = walkAstAndFindMyExpression(astRoot);
final Type instantiatedType = classInstanceCreation.getType();
System.out.println("BINDING: " + instantiatedType.resolveBinding());
为什么
resolveBinding()
返回 null
?如何获取绑定(bind)信息?
最佳答案
隐藏在 ASTParser.setKind()
概览的底部, 小心隐藏,不让人们排除故障 resolveBinding()
和 setResolveBindings()
, 是陈述
Binding information is only computed when
kind
isK_COMPILATION_UNIT
.
(来自the online Javadoc)
我不明白为什么会这样,但它似乎很清楚地指出了需要改变的地方!
关于eclipse-plugin - 为什么即使我在我的 ASTParser 上 setResolveBindings(true),resolveBinding() 仍会返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2631981/