Resolving Lombok @Data and @Builder Conflict: Restoring the No‑Args Constructor
This article explains why combining Lombok's @Data and @Builder annotations can remove the automatically generated no‑args constructor, demonstrates the resulting compilation errors, and provides two practical solutions using @Tolerate or a combination of @RequiredArgsConstructor and @NoArgsConstructor, while also describing Lombok's annotation‑processing mechanism.
When @Data and @Builder are used together in a Lombok‑enabled class, the generated no‑args constructor disappears, causing compilation failures in frameworks that rely on it.
@Data creates getters, setters, and a default no‑args constructor, while @Builder generates an all‑args constructor. Using both results in the loss of the no‑args constructor, as shown by the compiled class screenshots.
Solution 1 : Add @Tolerate to a manually written no‑args constructor so Lombok ignores it during generation.
Solution 2 : Use @RequiredArgsConstructor to generate the all‑args constructor and @NoArgsConstructor to generate the no‑args constructor, avoiding the conflict.
The article also outlines Lombok's underlying principle: it leverages JDK 6's JSR 269 Pluggable Annotation Processing API to modify the abstract syntax tree (AST) during compilation, inserting the required methods before the final .class file is produced.
To illustrate custom annotation processing, the author walks through creating a MySetter annotation, implementing its processor, compiling it with javac -cp , and applying it to a Person class, resulting in automatically generated setter methods.
Finally, the author notes that although Lombok generates bytecode at compile time, developers cannot directly call the generated methods without the Lombok plugin, which provides IDE support for seamless usage.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
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.