1

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

4

1 回答 1

1

从 JavaDoc 开始VariableDeclarationFragment

变量声明片段 AST 节点类型,用于字段声明、局部变量声明和 ForStatement初始化程序。与 相比 SingleVariableDeclaration,片段缺少修饰符和类型;这些位于片段的父节点中。

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

于 2011-06-06T11:48:08.890 回答