Why Lombok @Data + @Builder Breaks No‑Args Constructors and How to Fix It
This article explains why combining Lombok's @Data and @Builder annotations removes the automatically generated no‑argument constructor, demonstrates the compilation process behind Lombok, and provides two practical solutions using @Tolerate or @RequiredArgsConstructor/@NoArgsConstructor to restore the missing constructor.
Problem Background
Lombok throws a compilation error when @Data and @Builder are used together because the generated no‑argument constructor disappears, which many frameworks rely on.
Lombok @Data and @Builder Individual Usage
@Data generates getters, setters, and a default no‑argument constructor for a class.
Applying @Builder generates an all‑args constructor but does not create getters/setters on its own.
When both @Data and @Builder are applied, Lombok still creates getters/setters, but the no‑argument constructor is omitted, causing compilation failures.
Solutions
Method 1
Use the @Tolerate annotation on a manually written no‑argument constructor so Lombok ignores it during generation.
Method 2
Combine @RequiredArgsConstructor (to generate the all‑args constructor) with @NoArgsConstructor (to generate the no‑args constructor).
Lombok Internals
Java compilation consists of symbol table filling, annotation processing, analysis and bytecode generation, and finally class file creation.
Lombok uses JDK 6's JSR 269 Pluggable Annotation Processing API to transform annotation code into regular Java methods during compilation.
During the compilation phase, Lombok modifies the abstract syntax tree (AST) to inject new code nodes, which are then compiled into the final .class file.
Creating a simple custom setter via an annotation processor involves:
Defining a custom annotation and its processor.
Using the tools.jar javac API to manipulate the AST.
Compiling the processor and applying it to target classes.
Example files (MySetter.java, processor implementation, and test class) are shown in the images below.
After compiling the processor and applying it to the test class, the generated Person.class contains the expected setter method.
Conclusion
Although Lombok can generate setters at compile time, they are not directly callable in source code, which is why Lombok provides a plugin mechanism to expose its features during development.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
