Backend Development 13 min read

Automating Java Getter/Setter Replacement with Lombok Using JavaParser

This article explains how to build a tool that scans Java projects, identifies simple getter and setter methods, and automatically replaces them with Lombok's @Data annotation by leveraging JavaParser for AST manipulation, complete with Maven integration and usage guidelines.

JD Tech
JD Tech
JD Tech
Automating Java Getter/Setter Replacement with Lombok Using JavaParser

The author describes a practical need to reduce boilerplate in large Java codebases by replacing IDE‑generated getter/setter methods with Lombok's @Data annotation, aiming for cleaner code, less manual work, and lower risk of errors.

The solution is divided into three parts: (1) the overall implementation idea, (2) an introduction to the open‑source library JavaParser, and (3) detailed usage instructions for the custom tool.

Implementation Steps

1. Scan the entire project (including multi‑module projects) to collect all .java files. 2. Filter out interfaces, classes without fields, and classes already using Lombok. 3. Detect explicit getter/setter methods, ensuring they contain no special logic (boolean fields need special handling). 4. Verify that the getter/setter bodies are simple return or assignment statements. 5. Remove the simple getter/setter methods. 6. Add the @Data annotation and import the Lombok package. 7. Write the modified content back to the original Java files.

The core scanning code is shown below:

private static List<File> scanJavaFiles(File file) {
    List<File> result = Lists.newArrayList();
    if (file.isDirectory()) {
        File[] files = file.listFiles();
        if (files == null) {
            return result;
        }
        for (File f : files) {
            result.addAll(scanJavaFiles(f));
        }
    }
    if (file.getName().endsWith(".java")) {
        result.add(file);
    }
    return result;
}

JavaParser Overview

JavaParser is an open‑source Java source‑code analysis library that provides APIs to parse, modify, and generate Java code. Its main components are:

Lexer – tokenizes the source code.

Parser – builds an abstract syntax tree (AST) from tokens.

AST – hierarchical representation of code elements (packages, imports, types, comments, annotations).

Visitors – allow custom traversal and manipulation of the AST.

Printer – converts the modified AST back to source code.

Typical usage involves parsing a file into a CompilationUnit , navigating or modifying nodes, and then writing the result back.

Tool Usage

The tool is packaged as a Maven project. Add the following dependency to your pom.xml :

<dependency>
    <groupId>com.jd.omni.opdd</groupId>
    <artifactId>lombok-replace</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

Configure the exec-maven-plugin to run the converter:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
            <goals>
                <goal>java</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <mainClass>com.jd.omni.opdd.tools.lombok.LombokConverter</mainClass>
        <arguments>
            <argument>../../pop-jingme-customs</argument>
        </arguments>
    </configuration>
</plugin>

Replace the argument value with the path to your project (absolute or relative) and execute mvn exec:java . The console will display the conversion progress.

Notes

After running the tool, rebuild the project to ensure no compilation errors remain; some edge‑case field names may require manual adjustment.

Modules that do not already depend on Lombok need the Lombok dependency added manually.

The article concludes that such automated refactoring acts like a precise surgical tool, and while the presented utility is simple, the same approach can be extended to other code‑generation or transformation tasks.

code refactoringMavenStatic AnalysisLombokJavaParser
JD Tech
Written by

JD Tech

Official JD technology sharing platform. All the cutting‑edge JD tech, innovative insights, and open‑source solutions you’re looking for, all in one place.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.