为什么有些 VariableDeclaration resolveBinding 返回 null 而其他则不返回

发布于 2024-11-08 20:14:32 字数 1141 浏览 5 评论 0原文

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

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

发布评论

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

评论(1

泛泛之交 2024-11-15 20:14:32

来自 VariableDeclarationFragment 上的 JavaDoc:

变量声明片段 AST 节点
类型,在字段声明中使用,
局部变量声明,以及
ForStatement 初始值设定项。它
对比于
SingleVariableDeclaration,片段
缺少修饰符和
类型;这些位于
片段的父节点。

尝试从 VariableDeclarationFragment 的父级获取类型绑定。

From the JavaDoc on VariableDeclarationFragment:

Variable declaration fragment AST node
type, used in field declarations,
local variable declarations, and
ForStatement initializers. It
contrast to
SingleVariableDeclaration, fragments
are missing the modifiers and the
type; these are located in the
fragment's parent node.

Try to get the type binding from the VariableDeclarationFragment's parent.

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