eclipse-plugin - 为什么即使我在我的 ASTParser 上 setResolveBindings(true),resolveBinding() 仍会返回 null?

标签 eclipse-plugin eclipse-jdt

我正在编写一个使用 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 is K_COMPILATION_UNIT.



(来自the online Javadoc)

我不明白为什么会这样,但它似乎很清楚地指出了需要改变的地方!

关于eclipse-plugin - 为什么即使我在我的 ASTParser 上 setResolveBindings(true),resolveBinding() 仍会返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2631981/

相关文章:

java - 无法在 eclipse 中突出显示文本/变量

java - 从插件中以编程方式显示 SWT 浏览器

eclipse - 在Aptana Studio 3上使用Eclipse Marketplace

java - 如何从 Activity 的 Eclipse 编辑器中获取当前方法?

java - 将方法/变量声明添加到 org.eclipse.jdt.core.dom.CompilationUnit

java - Eclipse Activity 不工作

java - AbstractUIPlugin 导致 NoClassDefFoundError

eclipse-jdt - Eclipse JDT 教程

java - 我可以将 eclipse JDT/AST 用于其他编程语言吗?

java - ASTParser 类的 setEnvironment 方法未定义