Auto‑Replace Java Getters/Setters with Lombok @Data Using JavaParser

This article explains how to build a Maven‑based tool that scans a Java codebase, removes trivial getter and setter methods, adds Lombok’s @Data annotation, and rewrites the source files using JavaParser, complete with implementation steps, code examples, and usage instructions.

JD Cloud Developers
JD Cloud Developers
JD Cloud Developers
Auto‑Replace Java Getters/Setters with Lombok @Data Using JavaParser

Introduction

During a recent agile‑team effort the author used a Suite executor for unit testing and wondered what other executors JUnit provides. This curiosity led to the development of a small utility that automatically replaces IDE‑generated getter/setter methods with Lombok's @Data annotation.

Why Replace Getters/Setters?

Reduces boilerplate, making code cleaner and shorter.

Improves readability; teammates no longer need to inspect trivial accessor logic.

Prevents accidental omissions that cause communication overhead.

Implementation Idea

The tool follows six main steps:

Scan the entire project (including multi‑module projects) for .java files.

Read each file and filter out classes that should not be processed (interfaces, classes without fields, already annotated with Lombok, etc.).

Detect explicit getter/setter methods and verify they contain only simple return/assignment logic (boolean fields receive special handling).

Remove those trivial getter/setter methods.

Add the @Data annotation to the class and import the Lombok package.

Write the modified source back to the original file.

The scanning logic is illustrated 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;
}

Using JavaParser

JavaParser is an open‑source library that parses, modifies, and generates Java source code. Its core components include:

Lexer – tokenizes the source code.

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

AST – hierarchical representation of the code (packages, imports, type declarations, comments, annotations, etc.).

Visitors – traverse and manipulate the AST.

Printer – converts the modified AST back to source code.

Typical operations with JavaParser are:

Analyze classes, methods, fields, inheritance, parameters, and return types.

Rename methods, modify method bodies, add or delete lines.

Generate new classes, methods, or fields.

Relevant resources:

Website: https://javaparser.org/

GitHub: https://github.com/javaparser/javaparser

Javadoc: https://www.javadoc.io/doc/com.github.javaparser/javaparser-core/latest/index.html

Tool Usage

Add the following Maven dependency to your project:

<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>

Run the plugin with:

mvn exec:java

Notes & Best Practices

After the conversion, rebuild the project to ensure no compilation errors remain; the tool performs strict checks but may miss edge‑case variable names. Modules that do not already depend on Lombok must add the Lombok dependency manually.

Code refactoring should be precise and efficient—like a scalpel. This article mainly introduces JavaParser; the provided tool is a simple example, and the author has built similar utilities for generating matrix.json and i18n files from error codes.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaCode RefactoringmavenLombokJavaParser
JD Cloud Developers
Written by

JD Cloud Developers

JD Cloud Developers (Developer of JD Technology) is a JD Technology Group platform offering technical sharing and communication for AI, cloud computing, IoT and related developers. It publishes JD product technical information, industry content, and tech event news. Embrace technology and partner with developers to envision the future.

0 followers
Reader feedback

How this landed with the community

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.