2016-02-19 31 views
0

此方法可以獲取項目方法的名稱和行的方法數量 ,但似乎沒有獲取聲明這些方法的類。 我已經試過這個帖子 - How to get a class name of a method by using Eclipse JDT ASTParser?,但是當我使用resolveMethodBinding時,它返回null。如何從使用ASTVisitor的方法返回類名稱

public static void calculateAndSaveNumberMethodsFromFile(String path)throws IOException, ClassNotFoundException { 
     ASTParser parser = ASTParser.newParser(AST.JLS3); 
     parser.setKind(ASTParser.K_COMPILATION_UNIT); 
     String codigo = Utils.getStringFromFile(path); 
     codigo = RegexUtils.removeComments(codigo); 

     parser.setSource(codigo.toCharArray()); 
     final CompilationUnit cu = (CompilationUnit) parser.createAST(null); 

     cu.accept(new ASTVisitor() { 
      public boolean visit(MethodDeclaration node) { 

       SimpleName name = node.getName();//method name 
       int qtdLinhas = 0; 

       if (node.getBody() == null) { 
        LOG.info("-->Empty method!"); 
        qtdLinhas = 0; 
       } else { 
        qtdLinhas = (node.getBody().toString().split("\n").length - 2); //code lines method 
       } 

       LOG.info("Method '" + name + "' at line:" + cu.getLineNumber(name.getStartPosition()) + " Code lines: " + qtdLinhas); 

       Metodo metodo = new Metodo("OK".toString(), 123); 
       SingletonClass.addValue(metodo); 

       return false; // do not continue to avoid usage infoxc 
      } 
     }); 
    } 
+0

的[綁定不與日食AST處理解決]可能的複製(http://stackoverflow.com/questions/2017945/bindings-未解決與 - AST-處理功能於蝕) – sevenforce

回答

0

您缺少一個重要的路線是:

parser.setUnitName("any_name"); // set to the name of your class 
相關問題