Java 如何使用ASTVisitor从方法返回类名

Java 如何使用ASTVisitor从方法返回类名,java,eclipse-jdt,Java,Eclipse Jdt,此方法可以获取项目方法的名称和方法行的数量, 但是似乎没有得到声明这些方法的类。 我已经尝试过这篇文章,但是当我使用resolveMethodBinding时,它返回null public static void calculateAndSaveNumberMethodsFromFile(String path)throws IOException, ClassNotFoundException { ASTParser parser = ASTParser.newParser(A

此方法可以获取项目方法的名称和方法行的数量, 但是似乎没有得到声明这些方法的类。 我已经尝试过这篇文章,但是当我使用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
            }
        });
    }

您缺少的一条重要信息是:

 parser.setUnitName("any_name"); // set to the name of your class
可能重复的