What is Lombok?
Lombok is a compile-time code generator for Java that uses annotations to remove boilerplate (getters, setters, constructors, builders, equals/hashCode
, toString
, loggers, etc.). It works via annotation processing: your IDE/build runs Lombok, which generates the missing code into the compiled classes.
Why use it?
- Fewer lines of code, more readable POJOs/DTOs
- Consistent, generated
equals/hashCode/toString
- Easy builders and immutables
- Built-in logging fields
Core annotations (most used)
-
@Getter
,@Setter
β generate accessors (field/class level) -
@ToString
,@EqualsAndHashCode
-
@NoArgsConstructor
,@AllArgsConstructor
,@RequiredArgsConstructor
-
@Data
β shortcut for: getter/setter +equals/hashCode
+toString
+ required constructor -
@Builder
(+@SuperBuilder
,@Singular
) -
@Value
β immutable class (all fieldsprivate final
, getters, properequals/hashCode
) -
@Slf4j
(and friends) β adds a logger field -
@NonNull
β null-checks -
@With
β copy-with semantics -
@SneakyThrows
β rethrow checked exceptions without declaring (use carefully)
Quick before/after
Without Lombok
public class User {
private String id;
private String name;
public User() {}
public User(String id, String name) { this.id = id; this.name = name; }
public String getId() { return id; }
public void setId(String id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
@Override public String toString() { return "User{id='" + id + "', name='" + name + "'}"; }
}
With Lombok
import lombok.*;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String id;
private String name;
}
Builder & immutable example
import lombok.*;
@Value
@Builder(toBuilder = true)
public class Address {
String line1;
String city;
String pin;
}
Logging
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class Service {
public void work() {
log.info("Hello from Lombok logger");
}
}
Setup
Gradle
dependencies {
compileOnly 'org.projectlombok:lombok:1.18.34'
annotationProcessor 'org.projectlombok:lombok:1.18.34'
testCompileOnly 'org.projectlombok:lombok:1.18.34'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.34'
}
Maven
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
<scope>provided</scope> <!-- compile-time only -->
</dependency>
IDE
- IntelliJ: enable Annotation Processing; install Lombok plugin for better hints.
- Eclipse: run the Lombok JAR once to hook into Eclipse (or use newer Eclipse with built-in support).
- VS Code: install Lombok extension.
Gotchas / best practices
-
@Data
on entities: can cause issues with JPA and collections inequals/hashCode
. Prefer:@Getter @Setter @ToString @EqualsAndHashCode(onlyExplicitlyIncluded = true) public class Person { @EqualsAndHashCode.Include private Long id; // ... }
-
Immutability: use
@Value
(or Java records in 16+); donβt mix mutable fields. -
@SneakyThrows
: handy, but hides checked exceptionsβuse sparingly. - Binary size & debugging: generated methods exist in bytecode, so stack traces are fine; source may look shorter than decompiled classβuse βdelombokβ if you need plain source for tools.
- Alternative tools: Java records, Immutables, AutoValueβchoose per team standards.