0

我想使用 JavaParser 来获取 java 类具有的标识符的数量。

我下载了 JavaParser jar 文件并将其添加到我的项目中,然后,我按照其中的一些说明进行操作,现在我能够以编程方式解析一些 Java 类并使用ClassOrInterfaceDeclaration类似.getMethods().getMembers()等的方法...

现在,我想知道如何获取每个类中的标识符数量。没有.getIdentifiers()办法,那我应该采取什么办法呢?

4

1 回答 1

3

如果您阅读 的javadocjavaparser-core您会发现它JavaParser.parse(...)返回 a CompilationUnit,它是NodeAST(抽象语法树)中的 a。

可以遍历 AST,例如使用walk(Consumer<Node> consumer).

这是一个程序,它将遍历先前在评论中发布的源的 AST ,并打印所有节点。它将打印节点的标识符,如果它有一个:

import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.nodeTypes.NodeWithIdentifier;

public class Test {
    public static void main(String[] args) throws Exception {
        String javaSource = "class MyClass {" +
                            "  void main(String[] args) {" +
                            "    int a = 5, b = 6;" +
                            "    int c = a * b;" +
                            "    System.out.println(c);" +
                            "  }" +
                            "}";

        System.out.printf("%-28s %-12s %s%n", "Node.class.simpleName", "Identifier", "Node.toString()");
        System.out.printf("%-28s %-12s %s%n", "=====================", "==========", "===============");
        JavaParser.parse(javaSource).walk(node -> {
            String identifier = "";
            if (node instanceof NodeWithIdentifier)
                identifier = ((NodeWithIdentifier<?>) node).getIdentifier();
            System.out.printf("%-28s %-12s %s%n",
                              node.getClass().getSimpleName(),
                              identifier,
                              node.toString().replaceFirst("(?s)\\R.*", "..."));
        });
    }
}

输出

Node.class.simpleName        Identifier   Node.toString()
=====================        ==========   ===============
CompilationUnit                           class MyClass {...
ClassOrInterfaceDeclaration               class MyClass {...
SimpleName                   MyClass      MyClass
MethodDeclaration                         void main(String[] args) {...
SimpleName                   main         main
Parameter                                 String[] args
ArrayType                                 String[]
ClassOrInterfaceType                      String
SimpleName                   String       String
SimpleName                   args         args
VoidType                                  void
BlockStmt                                 {...
ExpressionStmt                            int a = 5, b = 6;
VariableDeclarationExpr                   int a = 5, b = 6
VariableDeclarator                        a = 5
PrimitiveType                             int
SimpleName                   a            a
IntegerLiteralExpr                        5
VariableDeclarator                        b = 6
PrimitiveType                             int
SimpleName                   b            b
IntegerLiteralExpr                        6
ExpressionStmt                            int c = a * b;
VariableDeclarationExpr                   int c = a * b
VariableDeclarator                        c = a * b
PrimitiveType                             int
SimpleName                   c            c
BinaryExpr                                a * b
NameExpr                                  a
SimpleName                   a            a
NameExpr                                  b
SimpleName                   b            b
ExpressionStmt                            System.out.println(c);
MethodCallExpr                            System.out.println(c)
FieldAccessExpr                           System.out
NameExpr                                  System
SimpleName                   System       System
SimpleName                   out          out
SimpleName                   println      println
NameExpr                                  c
SimpleName                   c            c
于 2018-05-05T20:05:58.180 回答