6

我正在编写一个使用 JDT ASTASTParser解析方法的 Eclipse 插件。我正在该方法中寻找创建特定类型对象的方法。

当我找到 aClassInstanceCreation时,我会调用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?如何获取绑定信息?

4

1 回答 1

5

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!

于 2010-04-13T18:14:42.720 回答