2012-10-06 44 views
3

我試圖用this article來試試eclipse jdt/ast。VariableDeclarationFragment節點resolveBindind()在eclipse/jdt/ast中返回null

這是java代碼作爲輸入:

class Hello 
{ 
    int hello() 
    { 
     int a = 0, b = 3; 
     /* hello */ 
     { 
      b = a * 3; 
     } 
     return a; 
    } 
    public static void main(String[] args) 
    { 
     int z = 0, i = 3; 
     /* hello */ 
     { 
      i = z * 3; 
     } 
    } 
} 

隨着ASTView,它表明已經VariableDeclarationFragment相應的結合。 enter image description here

然而,在此VariableDeclarationFragment node遊客代碼,我總是空值4個局部變量(A,B,Z,I)爲resolveBinding()返回值。

這是怎麼回事?我使用日食靛藍。

enter image description here

這是代碼來獲取AST

private static CompilationUnit createCompilationUnit(String sourceFile) { 
    String source = readWithStringBuilder(sourceFile); 
    ASTParser parser = ASTParser.newParser(AST.JLS3); 
    parser.setKind(ASTParser.K_COMPILATION_UNIT); 
    parser.setSource(source.toCharArray()); // set source 
    parser.setResolveBindings(true); // we need bindings later on 
    return (CompilationUnit) parser.createAST(null /* IProgressMonitor */); // parse 
} 

回答

4

這是因爲從setResolveBindings文檔如下:從Java模型獲得

綁定信息。這意味着編譯單元必須相對於Java模型定位。當源代碼來自setSource(ICompilationUnit)或setSource(IClassFile)時,會自動發生。 當通過setSource(char [])提供源時,必須通過使用setProject(IJavaProject)setEnvironment(String [],String [],String [],boolean)和一個單位名稱setUnitName(字符串)。請注意,影響文檔註釋檢查的編譯器選項也可能影響文檔註釋中是否解析任何綁定。

這意味着你可以使用類似的東西(從你的鏈接):

IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); 
IProject project = root.getProject("someJavaProject"); 
project.open(null /* IProgressMonitor */); 

IJavaProject javaProject = JavaCore.create(project); 

,並添加setProject電話:

private static CompilationUnit createCompilationUnit(String sourceFile, 
     IJavaProject javaProject) { 
    String source = readWithStringBuilder(sourceFile); 
    ASTParser parser = ASTParser.newParser(AST.JLS3); 
    parser.setKind(ASTParser.K_COMPILATION_UNIT); 
    parser.setSource(source.toCharArray()); // set source 
    parser.setProject(javaProject); 
    parser.setResolveBindings(true); // we need bindings later on 
    return (CompilationUnit) parser.createAST(null /* IProgressMonitor */); // parse 
} 

第二種方法(不setProject) :

private static CompilationUnit createCompilationUnit(String sourceFile, 
     String unitName) { 
    String source = readWithStringBuilder(sourceFile); 
    ASTParser parser = ASTParser.newParser(AST.JLS3); 
    parser.setKind(ASTParser.K_COMPILATION_UNIT); 
    parser.setSource(source.toCharArray()); // set source 
    String[] classpathEntries = ...; 
    String[] sourcepathEntries = ...; 
    parser.setEnvironment(classpathEntries, sourcepathEntries, null, true); 
    parser.setUnitName(unitName); 
    parser.setResolveBindings(true); 
    // optional 
    // parser.setBindingsRecovery(true); 
    return (CompilationUnit) parser.createAST(null /* IProgressMonitor */); // parse 
} 
+0

我使用JDT/AST作爲一個獨立的Java程序,所以我不能給IJavaProject的訪問。感謝你的回答。 – prosseek

0

這是否意味着我必須導入我想在當前項目中運行ASTParser的源代碼?

現在我正在使用ASTParser與UIMA框架合作。 在該框架中,我只能在char []中獲取源代碼。 不知道如何獲得相應的IJavaProject和UnitName,如果將.setSource()設置爲char [],則需要使用它。

0

我試圖解決這個使用下面的代碼段

protected static CompilationUnit parseStatements(String source) { 
    ASTParser parser = ASTParser.newParser(AST.JLS8); 
     parser.setSource(source.toCharArray()); 
    parser.setKind(ASTParser.K_COMPILATION_UNIT); 
    parser.setResolveBindings(true); 

    parser.setEnvironment(// apply classpath 
      new String[] { "//home//user//Projects//SmartCopy//ASTParser_Test//bin" }, // 
      null, null, true); 
    parser.setUnitName("any_name"); 
    final CompilationUnit cu = (CompilationUnit) parser.createAST(null); 
    return cu; 

} 


static void newcheckVariableDeclaration(){ 
    String source ="package javaproject;" // package for all classes 
      + "class Dummy {" 
      + "int j;" // 
      + " public void add(){" 
      + "int x=0,y=0;" 
      + "j=x+y;\n" // 
      + " }" // 
      + "}"; 

    final CompilationUnit root = parseStatements(source); 
    root.accept(new ASTVisitor() { 
     public boolean visit(SimpleName node) { 
      System.out.println(node.toString()); 
      if(node.resolveBinding() == null){ 
       System.out.println(node.toString()+" is not declared"); 
      } 
      else{ 
       System.out.println(node.toString() + " is declared"); 
      } 
      System.out.println(); 
      return true; 
     } 
    }); 
+0

歡迎使用StackOverflow,並感謝您的回答。但是,如果提供解決問題的代碼很好,我們將非常感謝在此網站上解釋爲什麼以及如何讓答案可以在以後重新使用。你真的應該更新你的帖子來添加一些精度。 –