Introduction
Project Lombok is a Java library that uses annotation processing to generate common boilerplate code at compile time. By adding annotations like @Data, @Builder, or @Slf4j to classes, developers eliminate repetitive code while keeping source files clean and readable.
What Lombok Does
- Generates getters, setters, toString, equals, and hashCode with @Data or individual annotations
- Creates builder patterns via @Builder for readable object construction
- Adds constructors with @AllArgsConstructor, @NoArgsConstructor, and @RequiredArgsConstructor
- Injects logger fields with @Slf4j, @Log4j2, or @Log without manual declaration
- Provides @Value for immutable classes and @With for functional-style copy methods
Architecture Overview
Lombok hooks into the Java compilation process via the annotation processing API (JSR 269). During compilation, Lombok's processor intercepts annotated elements and modifies the abstract syntax tree (AST) directly, adding methods and fields before the compiler generates bytecode. This means generated code exists in the .class files but not in the source, keeping source files concise.
Self-Hosting & Configuration
- Add via Maven or Gradle: org.projectlombok:lombok with scope provided/compileOnly
- Install the Lombok plugin in IntelliJ IDEA or Eclipse for IDE support and code completion
- Enable annotation processing in IDE settings (Build > Compiler > Annotation Processors)
- Customize behavior with a lombok.config file (e.g., lombok.accessors.fluent=true)
- Use delombok to generate plain Java source if you need to inspect or distribute source code
Key Features
- @Data combines @Getter, @Setter, @ToString, @EqualsAndHashCode, and @RequiredArgsConstructor
- @Builder generates a fluent builder with optional defaults and toBuilder() support
- @SneakyThrows wraps checked exceptions without declaring them in the method signature
- @Cleanup ensures resources are closed at the end of scope without try-with-resources
- val and var provide local variable type inference for older Java versions
Comparison with Similar Tools
- Java Records (JDK 16+) — language-level immutable data carriers; replace @Value for simple cases but lack builders and customization
- AutoValue (Google) — generates value classes from abstract classes; more explicit but requires more boilerplate than Lombok
- Immutables — annotation processor for immutable objects with builders; similar to Lombok @Value + @Builder but as a separate artifact
- Kotlin Data Classes — language-level feature achieving similar goals; relevant if the team is migrating to Kotlin
FAQ
Q: Does Lombok work with Java 21+? A: Yes. Lombok tracks JDK releases and is compatible with the latest LTS versions.
Q: Is Lombok safe for production use? A: Yes. Lombok has been widely used in production since 2009 and is a dependency in many enterprise Java projects.
Q: Can I use Lombok with Kotlin? A: It is not recommended. Kotlin has built-in features (data classes, default parameters) that replace most Lombok functionality.
Q: Does Lombok affect runtime performance? A: No. Lombok operates at compile time only. The generated bytecode is identical to hand-written Java code.