Our goal is to build a simple and lightweight set of tools under a business friendly license.

JavaParser is used in dozens of open-source and commercial projects. Show your love adding one star more or spreading the word.

Star JavaParser Star JavaSymbolSolver

What you can do with JavaParser

JavaParser is a set of tools to:

Java code.

Want to learn JavaParser?

We are writing a book on JavaParser and we need your feedback!

Help us understanding what is not clear, what should we add, share examples and ideas with us. You can also contact us on gitter or on the book repository.

It would be great if you could help us spread the word:

Parsing Java code

Using JavaParser you can get an Abstract Syntax Tree (AST) from Java code. The AST is a structure representing your Java code in a way that it is easy to process:

CompilationUnit compilationUnit = JavaParser.parse("class A { }");
ClassOrInterfaceDeclaration classA = compilationUnit.getClassByName("A");

Analyze Java code

Suppose you want to look for fields which are public and not static. This is pretty easy using JavaParser:

compilationUnit.getNodesByType(FieldDeclaration.class).stream().
        filter(f -> f.getModifiers().contains(PUBLIC) && 
                !f.getModifiers().contains(STATIC)).
        forEach(f -> System.out.println("Check field at line " + f.getBegin().get().line));

Transform Java code

Maybe you want to ensure all abstract classes have a name starting with Abstract:

compilationUnit.getNodesByType(ClassOrInterfaceDeclaration.class).stream().
        filter(c -> !c.isInterface()
                && c.getModifiers().contains(ABSTRACT)
                && !c.getName().getId().startsWith("Abstract")).
        forEach(c -> {
            String oldName = c.getName().getIdentifier();
            String newName = "Abstract" + oldName;
            System.out.println("Renaming class " + oldName + " into " + newName);
            c.getName().setIdentifier(newName);
        });

Generate Java code

ClassOrInterfaceDeclaration myClass = new ClassOrInterfaceDeclaration(
        EnumSet.of(PUBLIC), false, "MyClass");
myClass.addField("int", "A_CONSTANT", PUBLIC, STATIC);
myClass.addField("String", "name", PRIVATE);
String code = myClass.toString();

Components of JavaParser

JavaParser is both the name of the set of tools and of the main component: the parser. In addition to JavaParser there is another component in the set of tools: JavaSymbolSolver.

The parser is extra lightweight (no dependencies at all!) and can give you super fast an AST for your Java code. Sometimes an AST is not enough because you want:


JavaSymbolSolver does the extra work to answer this questions for you.

License

JavaParser is available either under the terms of the LGPL License or the Apache License. You as the user are entitled to choose the terms under which adopt JavaParser.

For details about the LGPL License please refer to LICENSE.LGPL.

For details about the Apache License please refer to LICENSE.APACHE.