Welcome Developers!

Tools for your Java code

Explore the most common use cases for the JavaParser library

Analyse

Write code that can traverse Java source and look for the patterns you are interested in.

Transform

Build tools that can not just identify code patterns, but also has the ability to change them.

Generate

Be smart, don't spend time writing boiler plate, generate it!

Amazing stats

The most popular parser for the Java language

When choosing open source technologies it is important to know your choice will be rewarded by continuous support. The JavaParser community is vibrant and active, with a weekly release cadence that supports language features up to Java 12.

3,833

Stars

891

Forks

2,638

Dependants

10,402

Readers

Business friendly license

Available either under either the terms of the LGPL or Apache Licenses.

Active Community

An awesome community of approachable developers over on Gitter

Simple and lightweight

Bring your source code to life

Our goal is to build a simple and lightweight set of tools to analyze, transform and generate Java code.


...

Quick Start

The JavaParser library provides you with an Abstract Syntax Tree of your Java code. The AST structure then allows you to work with your Java code in an easy programmatic way.

<!-- Add the following dependency to your pom.xml,  -->
<!-- replacing LATEST with specific version as required  -->

<dependency>
    <groupId>com.github.javaparser</groupId>
    <artifactId>javaparser-symbol-solver-core</artifactId>
    <version>LATEST</version>
</dependency>
                
// Create a Java object representation of your code

CompilationUnit compilationUnit
		= StaticJavaParser.parse("class A { }");
Optional<ClassOrInterfaceDeclaration> classA
		= compilationUnit.getClassByName("A");
            
// Look for fields which are public and not static

compilationUnit.findAll(FieldDeclaration.class).stream()
        .filter(f -> f.isPublic() && !f.isStatic())
        .forEach(f -> System.out.println("Check field at line " +
            f.getRange().map(r -> r.begin.line).orElse(-1)));
                            
// Ensure all abstract classes have a name starting with Abstract

compilationUnit.findAll(ClassOrInterfaceDeclaration.class).stream()
        .filter(c -> !c.isInterface()
                && c.isAbstract()
                && !c.getNameAsString().startsWith("Abstract"))
        .forEach(c -> {
            String from = c.getNameAsString();
            String to = "Abstract" + from;
            System.out.println("Renaming class " + from + " into " + to);
            c.setName(to);
        });
                            
// Create source code on the fly

CompilationUnit compilationUnit = new CompilationUnit();
ClassOrInterfaceDeclaration myClass = compilationUnit
        .addClass("MyClass")
        .setPublic(true);
myClass.addField(int.class, "A_CONSTANT", PUBLIC, STATIC);
myClass.addField(String.class, "name", PRIVATE);
String code = myClass.toString();
                            

Join as Contributor

JavaParser is an open-source project built by amazing volunteers. We are always looking for new contributors and will support you throughout the process.

Donate

The best way to help the JavaParser community is to become a contributor; however if you would like to show your support in another way, please consider a purchase of our book.