Backend Development 12 min read

Design and Implementation of a Java Code Optimization Tool Using JavaParser and Lombok

This article presents a developer‑centric guide to designing a Java code‑optimization tool that automatically replaces trivial getter/setter methods with Lombok’s @Data annotation, detailing the scanning, filtering, modification steps, JavaParser usage, Maven integration, and practical usage instructions.

JD Retail Technology
JD Retail Technology
JD Retail Technology
Design and Implementation of a Java Code Optimization Tool Using JavaParser and Lombok

The author, a frontline developer, introduces a code‑optimization tool that replaces explicit getter/setter methods with Lombok's @Data annotation, aiming to inspire similar utilities.

Existing static‑analysis tools such as PMD, IDEA's inspect feature, and FindBugs are either too conservative or produce bugs, making manual one‑by‑one fixes tedious; therefore a one‑click tool was created.

The article is divided into three parts: the implementation idea, a brief introduction to the open‑source library JavaParser, and detailed usage instructions for the tool.

Implementation Idea

Using Lombok @Data brings several advantages: cleaner code with reduced size, less repetitive work when adding fields, improved readability, and fewer omissions that can cause communication overhead.

The tool follows these steps:

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

Read all .java files.

Filter out unnecessary classes such as interfaces, classes without fields, or those already annotated.

Delete getter/setter methods after confirming they contain no special logic.

Add the @Data annotation and import the Lombok package.

Write the modified content back to the source files.

Engine Scan Code

private static List
scanJavaFiles(File file) {
List
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;
}

Annotation Replacement Process

After obtaining the list of files, the tool filters out classes without fields and those already using Lombok, detects explicit getter/setter methods (with special handling for boolean fields), checks that the methods are simple return/assignment operations, deletes them, adds @Data , and inserts the Lombok import. JavaParser is used for parsing, modifying, and writing back the source code.

JavaParser Overview

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

Lexer – tokenizes the source code.

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

AST – hierarchical representation of code elements.

Visitors – visitor‑pattern classes (GenericVisitor, VoidVisitor) for traversing and manipulating the AST.

Printer – converts the AST back to formatted Java source code.

Typical operations involve using StaticJavaParser to obtain a CompilationUnit , then accessing or modifying package declarations, imports, type declarations, comments, and applying custom visitors.

Usage Instructions

Add the following Maven dependency to include the tool:

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

<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 node with the path to your project (absolute or relative) and execute:

mvn exec:java

After execution, verify the console output and rebuild the project to ensure no compilation errors remain; some edge‑case variable names may require manual adjustment. Modules that do not already depend on Lombok need the Lombok dependency added manually.

Conclusion

The tool acts like a surgical knife for code refactoring: fast, precise, and decisive. While the article mainly showcases JavaParser, the same approach can be extended to other automation scenarios such as cleaning up unused methods via middleware monitoring APIs or visualizing code structures for non‑technical stakeholders.

Javacode refactoringmavenstatic analysisLombokJavaParser
JD Retail Technology
Written by

JD Retail Technology

Official platform of JD Retail Technology, delivering insightful R&D news and a deep look into the lives and work of technologists.

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.