Skip to content

Java parsers

Damir Petrov edited this page Nov 7, 2020 · 3 revisions

Java parser

There are some projects for converting java source code to AST.

Pluses:

  • Community
  • Documentation
  • Introduction book
  • Under develop
  • Easy to use

Pluses:

  • Wiki
  • Under develop

Pluses:

  • CLI

Minuses:

  • Stopped in develop

4. Spoon

Pluses:

  • Community
  • Documentation
  • CLI

Minuses:

  • Uses Maven

As we see, JavaParser is better than everything else. So let me show a quick introduction for JavaParser.

The easiest way to get a first result is to create a Gradle project.

  1. Add the following line to build.gradle compile group: 'com.github.javaparser', name: 'javaparser-symbol-solver-core', version: '3.13.3'

  2. Create file SomeClass.java in src/main/java directory with the following code

public class SomeClass {
    private SomeClass(int a) {
        System.out.println("Oh, hello!");
    }
    public SomeClass() {
        System.out.println("I'm here!");
    }
    public void A(String X, double Z) {}
    private String B(int Y) { return ""; }
    protected SomeClass C() { return new SomeClass(); }
    private class YetAnotherClass {}
    public class C1{}
    private class C2{}
}
  1. Create a file that will analyze our source code in SomeClass.java
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.BodyDeclaration;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class Analyzer {
    private static final String FILE_PATH = "src/main/java/SomeClass.java";
    public static void main(String[] args) throws FileNotFoundException {
        CompilationUnit cu = StaticJavaParser.parse(new FileInputStream(FILE_PATH));
        ClassOrInterfaceDeclaration c = cu.getClassByName("SomeClass").get();
        System.out.println(c.getName() + ":");
        for (BodyDeclaration i : c.getMembers()) {
            if (i.isClassOrInterfaceDeclaration()) {
                System.out.println("Inner class: " + i.asClassOrInterfaceDeclaration().getName());
            } else if (i.isMethodDeclaration()) {
                System.out.println("Method: " + i.asMethodDeclaration().getName());
            } else {
                System.out.print("Constructor: ");
                i.asConstructorDeclaration().getParameters().forEach(
                        (j) -> System.out.print(j.getType().asString() + " " + j.getName() + " ")
                );
                System.out.println();
                i.asConstructorDeclaration().getBody().getStatements().forEach(
                        (j) -> System.out.println(j.toString())
                );
            }
        }
    }
}

You can execute that and see the following

SomeClass:
Constructor: int a 
System.out.println("Oh, hello!");
Constructor: 
System.out.println("I'm here!");
Method: A
Method: B
Method: C
Inner class: YetAnotherClass
Inner class: C1
Inner class: C2

So we can get any information of source code with a way similar to the above.

Clone this wiki locally