为什么即使我在 ASTParser 上 setResolveBindings(true),resolveBinding() 也会返回 null?

发布于 2024-08-28 08:44:29 字数 1179 浏览 10 评论 0原文

我正在编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

屋顶上的小猫咪 2024-09-04 08:44:29

隐藏在 ASTParser.setKind() 概述的底部,小心地向人们排除 resolveBinding()setResolveBindings() 的问题,是声明

仅当 kindK_COMPILATION_UNIT 时才计算绑定信息。

(来自 在线 Javadoc)

我不明白为什么会出现这种情况,但它似乎非常清楚地指出了需要做什么与众不同!

Tucked away at the bottom of the overview of ASTParser.setKind(), carefully hidden from people troubleshooting resolveBinding() and setResolveBindings(), is the statement

Binding information is only computed when kind is K_COMPILATION_UNIT.

(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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文