How Lombok Transforms Java: Reduce Boilerplate with Simple Annotations
This article explains why Lombok was created to eliminate repetitive Java code, shows how to add the Maven dependency, configure IDE support, describes its annotation‑processing mechanism, and demonstrates the most common Lombok annotations with before‑and‑after code examples.
Traditional Java projects are filled with boilerplate such as getters, setters, toString methods, exception handling, and I/O stream closing, which add no technical value and clutter code; Lombok was created to solve this problem.
1) Add the Maven dependency
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<scope>provided</scope>
</dependency>Setting scope=provided means Lombok is only needed at compile time and is not packaged into the final artifact; during compilation Lombok processes annotated source files and generates complete class files.
2) Enable Lombok support in IDEs
In IntelliJ IDEA, install the Lombok plugin via File → Settings → Plugins and enable annotation processing in File → Settings → Build, Execution, Deployment → Compiler → Annotation Processors .
For Eclipse, add the Lombok jar to the -Xbootclasspath option in eclipse.ini so that the Eclipse Compiler for Java (ECJ) can use Lombok.
3) Lombok’s implementation principle
Since Java 6, javac supports the JSR 269 Pluggable Annotation Processing API. Lombok implements this API and participates in the compilation process as follows:
javac parses source code and builds an abstract syntax tree (AST).
During compilation, javac invokes Lombok’s annotation processor.
Lombok traverses the AST, finds Lombok annotations, and modifies the tree by adding the corresponding nodes.
javac then generates bytecode from the modified AST.
4) Common Lombok annotations
Typical annotations for POJOs include: @Getter/@Setter: generate getter and setter methods for fields or the whole class. @ToString: generate a toString() method, with of and exclude options. @EqualsAndHashCode: generate equals() and hashCode() methods. @NonNull: add a null‑check and throw NullPointerException if the value is null. @NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor: generate constructors with various parameter sets. @Data: a shortcut that combines @Getter, @Setter, @ToString, @EqualsAndHashCode, and @RequiredArgsConstructor. @Builder: implement the builder pattern for the class. @Log family: generate a logger field for different logging frameworks. @Cleanup: automatically close resources that implement java.io.Closeable. @SneakyThrows: bypass checked‑exception handling. @Synchronized: replace explicit synchronized blocks.
Example usage:
package com.trace;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
@Getter(value = AccessLevel.PUBLIC)
@Setter(value = AccessLevel.PUBLIC)
public class Person {
private String name;
private int age;
private boolean friendly;
}In the IDE’s structure view, Lombok‑generated getter and setter methods become visible.
After compilation, the generated bytecode corresponds to the following source (simplified):
public class Person {
private String name;
private int age;
private boolean friendly;
public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
public int getAge() { return this.age; }
public void setAge(int age) { this.age = age; }
public boolean isFriendly() { return this.friendly; }
public void setFriendly(boolean friendly) { this.friendly = friendly; }
public String toString() {
return "Person(name=" + this.name + ", age=" + this.age + ")";
}
}Additional annotations such as @Log generate a logger field, and @Cleanup ensures streams are closed automatically.
Overall, Lombok dramatically reduces repetitive code, making Java classes more concise and readable.
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.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
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.
