- It aims to reduce the boilerplate code that developers have to write, such as
- getters
- setters
- constructors,
- No Argument constructors etc.
- Equals, toString() etc.
- Lombok achieves this by generating this code automatically during compilation, based on annotations we add to our Java classes.
- Annotations:
- @Getters
- @Setter
- @ToString
- @EqualsAndHashCode
- @NoArgsConstructor
- @AllArgsConstructor
- @Builder
- @Slf4j
- @Data - contains - @Getter + @Setter + @ToString + @EqualAndHashCode + @Value + @RequiredArgsConstructor
- How does it works -
- Lombok generates bytecode for methods like getters, setters, equals, hashcode as specified by the annotations used in your code. This generated code is added to the compiled class files(.class files)
- Java compiler compiles your classes, including the generated code. This means that the generated methods become part of your compiles class files. When you run application, the generated methods are available for use, just like any other methods in you classes.
- Using
@Builderoverrides the default constructor of the class, hence there might be possibility that we might need to add@NoArgsConstructorand@AllArgsConstructor