为什么有些 VariableDeclaration resolveBinding 返回 null 而其他则不返回
我正在开发一个eclipse插件来分析java源代码。我遍历整个 AST 树并编写一个访问者来访问每个变量声明语句,我注意到对于某些变量,“resolveBinding”返回 IVariableBinding 的实例,但其他变量则不会。我无法区分它们。顺便说一句:我已经设置了 ASTParser.setKind(K_COMPILATION_UNIT) 和 setResolveBindings(true)。我的代码如下:
@Override
public boolean visit(VariableDeclarationStatement vStatement) {
Type theType = vStatement.getType();
for(Iterator iterator = vStatement.fragments().iterator();iterator.hasNext();){
VariableDeclarationFragment fragment = (VariableDeclarationFragment)iterator.next();
IVariableBinding binding = fragment.resolveBinding();
if(binding !=null){
ITypeBinding tBinding = binding.getType();
if(tBinding !=null){
// if there is ArrayType, get the root type
while(tBinding.getComponentType()!=null){
tBinding = tBinding.getComponentType();
}
System.out.println("USING BINDING VARIABLE CLASS IS: " + tBinding.getQualifiedName());
}
}
}
}
我的问题是:如何区分可以解析绑定的变量声明与其他不能解析的变量声明?
预先非常感谢
I am developing an eclipse plug-in to analyze the java source code. I traverse the whole AST tree and write a visitor to visit each variableDeclartionStatement, I noticed for some variables, the "resolvebinding" return an instance of IVariableBinding, but others does not. I can not differentiate them. BTW: I have set the ASTParser.setKind(K_COMPILATION_UNIT) and setResolveBindings(true). My code is as follows:
@Override
public boolean visit(VariableDeclarationStatement vStatement) {
Type theType = vStatement.getType();
for(Iterator iterator = vStatement.fragments().iterator();iterator.hasNext();){
VariableDeclarationFragment fragment = (VariableDeclarationFragment)iterator.next();
IVariableBinding binding = fragment.resolveBinding();
if(binding !=null){
ITypeBinding tBinding = binding.getType();
if(tBinding !=null){
// if there is ArrayType, get the root type
while(tBinding.getComponentType()!=null){
tBinding = tBinding.getComponentType();
}
System.out.println("USING BINDING VARIABLE CLASS IS: " + tBinding.getQualifiedName());
}
}
}
}
My question is: How can I differentiate the variable declarations which can resolve bindings with others which can not?
Many thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自
VariableDeclarationFragment
上的 JavaDoc:尝试从
VariableDeclarationFragment
的父级获取类型绑定。From the JavaDoc on
VariableDeclarationFragment
:Try to get the type binding from the
VariableDeclarationFragment
's parent.