2011-05-18 54 views
1

我正在開發一個eclipse插件來分析java源代碼。我遍歷整個AST樹,並寫一個訪問者來訪問每個變量DeclartionStatement,我注意到一些變量,「解析綁定」返回一個IVariableBinding的實例,但其他人不。我無法區分它們。順便說一句:我已經設置了ASTParser.setKind(K_COMPILATION_UNIT)和setResolveBindings(true)。我的代碼如下:爲什麼一些VariableDeclaration resolveBinding返回null,但其他變量不是

@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()); 
      } 

     }      
    } 
} 

我的問題是:我如何區分變量聲明可以與其他哪些不可以解決的綁定?

預先

回答

1

從JavaDoc的許多感謝上VariableDeclarationFragment

變量聲明片段AST節點 類型,在字段聲明, 局部變量聲明使用,並且 ForStatement初始化。它與 對比 SingleVariableDeclaration,片段 缺少修飾符和 類型;這些位於 片段的父節點中。

嘗試從VariableDeclarationFragment的父級獲得綁定類型。

相關問題