问题描述
我正在编写一个使用JDT AST的 ASTParser
来解析方法的Eclipse插件。我正在寻找创建一个特定类型对象的方法。
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.
当我找到一个 ClassInstanceCreation
我调用 getType()
来查看要实例化的类型。我想确保正在处理的完全解决的类型是我认为的那样,所以我将结果 Type
object to $ code> resolveBinding()。即使没有编译错误,即使我在 null
中调用 setResolveBindings(true)
C $ C> ASTParser 。我通过 ASTParser
(通过 setSource()
) ICompilationUnit
包含我的方法,所以解析器可以访问整个工作区上下文。
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());
为什么 resolveBinding()
return 空
?如何获取绑定信息?
Why does resolveBinding()
return null
? How can I get the binding information?
推荐答案
隐藏在 ASTParser概述的底部。 setKind()
,仔细隐藏人员疑难解答 resolveBinding()
和 setResolveBindings()
是
Tucked away at the bottom of the overview of ASTParser.setKind()
, carefully hidden from people troubleshooting resolveBinding()
and setResolveBindings()
, is the statement
(从)
我不明白为什么会这样,但它似乎很明确地指出需要什么不同!
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!
这篇关于为什么resolveBinding()返回null即使我在我的ASTParser上setResolveBindings(true)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!