此方法可以获取项目方法的名称和行方法的数量, 但似乎没有得到声明这些方法的类。 我已经尝试过这篇文章 - How to get a class name of a method by using Eclipse JDT ASTParser?,但是当我使用resolveMethodBinding时它会返回null。
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 :(得分:0)
您缺少的重要一行是:
parser.setUnitName("any_name"); // set to the name of your class