为什么即使我在 ASTParser 上 setResolveBindings(true),resolveBinding() 也会返回 null?
我正在编写一个 Eclipse 插件,它使用 JDT AST 的 ASTParser 来解析方法。我正在该方法中寻找创建特定类型对象的方法。
当我找到 ClassInstanceCreation
时,我对其调用 getType()
以查看正在实例化的类型。我想确保正在处理的完全解析类型就是我认为的类型,因此我将生成的 Type
对象告诉 resolveBinding()
。即使没有编译错误,即使我在 ASTParser
上调用了 setResolveBindings(true)
,我还是得到了 null
。我为我的 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
?如何获取绑定信息?
I am writing an Eclipse plug-in that uses JDT AST's ASTParser
to parse a method. I am looking within that method for the creation of a particular type of object.
When I find a ClassInstanceCreation
, I call getType()
on it to see what type is being instantiated. I want to be sure that the fully-resolved type being dealt with there is the one I think it is, so I tell the resultant Type
object to resolveBinding()
. I get null
back even though there are no compilation errors and even though I called setResolveBindings(true)
on my ASTParser
. I gave my ASTParser
(via setSource()
) the ICompilationUnit
that contains my method, so the parser has access to the entire workspace context.
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());
Why does resolveBinding()
return null
? How can I get the binding information?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
隐藏在
ASTParser.setKind()
概述的底部,小心地向人们排除resolveBinding()
和setResolveBindings()
的问题,是声明(来自 在线 Javadoc)
我不明白为什么会出现这种情况,但它似乎非常清楚地指出了需要做什么与众不同!
Tucked away at the bottom of the overview of
ASTParser.setKind()
, carefully hidden from people troubleshootingresolveBinding()
andsetResolveBindings()
, is the statement(from the online Javadoc)
I don't understand offhand why this would be the case, but it does seem to point pretty clearly at what needs to be different!