2016-02-19 78 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 
相关问题