Why Lombok @Data + @Builder Breaks No‑Arg Constructors and How to Fix It

This article explains why using Lombok's @Data and @Builder together removes the default no‑argument constructor, demonstrates the compilation behavior, and provides two practical solutions—including @Tolerate and combining @RequiredArgsConstructor with @NoArgsConstructor—while also detailing Lombok's annotation‑processing mechanism.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
Why Lombok @Data + @Builder Breaks No‑Arg Constructors and How to Fix It

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 Usage

@Data generates getters, setters, and a no‑argument constructor, while @Builder creates an all‑argument constructor. Using them together generates getters/setters but removes the no‑argument constructor.

When only @Builder is applied, an all‑args constructor appears, but getters/setters are not generated.

Combining @Data and @Builder yields getters/setters but no no‑arg constructor, causing compilation failures when a manual no‑arg constructor is added.

Solutions

Method 1

Introduce @Tolerate on a manually written no‑argument constructor so Lombok ignores it while still generating other code.

Method 2

Use @RequiredArgsConstructor to generate the all‑args constructor and @NoArgsConstructor to generate the no‑args constructor, keeping both functionalities.

Lombok Principle

Java compilation consists of parsing, annotation processing, bytecode generation, and class file creation.

解析与填充符号表->注解处理->分析与字节码生成->生成二进制class文件。

Lombok uses JDK 6's JSR 269 Pluggable Annotation Processing API to transform annotations into regular Java code during compilation.

During the compilation phase, Lombok modifies the abstract syntax tree (AST) to inject new nodes, which are then compiled into the final .class file.

Creating a custom setter via an annotation processor involves defining a custom annotation, implementing a processor, using tools.jar 's javac API to manipulate the AST, and compiling the processor before applying it to target classes.

可以借助注解处理器实现一个简单的 Setter,我们的实现步骤是:

Define a custom annotation and its processor.

Use tools.jar to handle the AST.

Compile the processor and then compile the target class with it.

Example images illustrate the creation of MySetter.java, the test class, and the generated Person.class containing the setter method.

Summary

Although Lombok can generate setters at compile time, they are not directly callable during development; therefore Lombok provides IDE plugins that expose these features for convenient use.

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.

JavaAnnotation ProcessingLombokBuilder PatternNo-Args Constructor
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

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.