How does lombok work?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Lombok is a popular library in the Java ecosystem known for reducing boilerplate code. By using annotations, it allows developers to automatically generate commonly used code like getters, setters, equals, hashcode, and more. This article delves into how Lombok works, its technical underpinnings, and provides practical examples.
What is Lombok?
Lombok is a Java library that uses annotations to simplify the development process by generating repetitive code at compile time. This promotes cleaner, more readable code, and minimizes the chances of introducing bugs that might occur from manually coded methods.
How Lombok Works: Behind the Scenes
Lombok operates by leveraging the Java Annotation Processing Tool (APT). Let's explore the key mechanisms:
- Annotation Processing: Lombok hooks into the Java compiler using custom annotation processors. When you compile your code, Lombok modifies the abstract syntax tree (AST) generated by the compiler to add methods directly to the Java bytecode.
- AST Manipulation: Lombok leverages the internal APIs like
javacto alter the AST. This makes it possible to inject methods such as getters, setters, constructors, etc., directly into the class. The AST manipulation happens before the code is converted into bytecode, making it seamless to developers. - Bytecode Generation: Once the AST is altered, Lombok allows the regular Java compiler process to convert this into bytecode, including the methods Lombok added.
- IDE Integration: Lombok is designed to work seamlessly with Integrated Development Environments (IDEs) like IntelliJ IDEA and Eclipse via plugins. These plugins ensure that the IDE recognizes the Lombok-generated methods, providing features like autocompletion and error-checking.
Common Lombok Annotations and Their Usage
1. @Getter and @Setter
These annotations generate getter and setter methods for fields.
2. @ToString
Generates a toString() method that includes the class field names and their values.
3. @EqualsAndHashCode
Generates equals() and hashCode() methods based on the fields in the class.
4. @Data
A composite annotation that includes @Getter, @Setter, @ToString, @EqualsAndHashCode, and @RequiredArgsConstructor.
5. @Builder
Provides a builder pattern implementation for the class.
Benefits of Using Lombok
- Reduces Boilerplate: Minimizes the amount of getter, setter, and constructor code you have to write.
- Improves Readability: Keeps your classes cleaner and easier to understand.
- Error Reduction: Reduces common errors that arise from manual coding of repetitive tasks.
- Encourages Immutable Design: With annotations like
@Builder, encourages design patterns that promote immutability.
Limitations and Considerations
- Learning Curve: New developers might struggle initially to understand the hidden methods in classes.
- IDE Support: Requires appropriate plugins for IDEs, though most modern IDEs support Lombok.
- Build and Debugging: Makes debugging the bytecode more challenging since methods aren't explicitly present in the source code.
Summary Table
Below is a table summarizing key Lombok annotations and their purposes:
| Annotation | Description |
@Getter/@Setter | Generates getter and setter methods for fields. |
@ToString | Generates a toString() implementation. |
@EqualsAndHashCode | Generates equals() and hashCode() methods. |
@Data | Combines @Getter, @Setter, @ToString, @EqualsAndHashCode, and more. |
@Builder | Facilitates builder pattern implementation for complex object creation. |
Conclusion
Lombok is a powerful tool in the Java developer's arsenal. While it simplifies code and improves maintainability, it's essential to understand its under-the-hood workings to effectively use and troubleshoot it. Careful attention should be given to its impact on project compilation and team dynamics, especially when considering onboarding new team members unfamiliar with Lombok.
Related reading
- How does Spring achieve IOC with autowiring?
- How does Spring Kafka BATCH ack mode work with non-batch listener?
- How does spring.kafka.consumer.auto-offset-reset works in spring-kafka
- How does the final keyword in Java work? (I can still modify an object.)
- How does the singleton Bean serve the concurrent request?
- How find all unused classes in Intellij Idea?
- How good is Java's UUID.randomUUID?
- How is concurrency in Spring AMQP Listener Container implemented?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.