#Java #Lombok #Eclipse
What is Project Lombok in Java? | How to use Project Lombok in Eclipse
Lombok is used to reduce the boilerplate code for models/data objects in Java, e.g., it can generate getters and setters for those fields automatically by using Lombok annotations. Lombok acts as an annotation processor that “adds” code to your classes at compile time. Lombok generates the code directly in the ".class" file directly
@Getter and @Setter: These annotations provide the getter and setter methods for a field. These annotations can be used at both the levels, field as well as class.
@NoArgsConstructor: This annotation is used to generate a constructor with no arguments.
@AllArgsConstructor: This annotation is used to generate a parameterized constructor that accepts a single parameter for each field and initializes them using it.
@ToString: This annotation is used to override the toString() method and generate a default implementation for it.
@EqualsAndHashCode: This annotation is used to override the equals() and hashCode() methods and provides a default implementation for those
The @Data annotation can be used to apply functionality behind all the annotations discussed so far in this section. That is, simply annotating a class with @Data causes Lombok to generate getters and setters for each of the nonstatic class fields and a class constructor, as well as the toString(), equals(), and hashCode() methods.
The @Value annotation is similar to the @Data annotation, but it generates an immutable class. The annotation is placed at the class level, and it invokes the automatic generation of getters for all private final fields. No setters are generated, and the class is marked as final. Lastly, the toString(), equals(), and hashCode() methods are generated, and a constructor is generated that contains arguments for each of the fields.
Annotating a class with @Builder produces a class that adheres to the builder pattern - The builder pattern builds a complex object using simple objects and using a step by step approach.
@Cleanup - Calls close() method on the resource by wrapping the code within the try and finally.
@log - You can annotate any class with a log annotation to let Lombok generate a logger field, The logger is named log and the field's type depends on which logger you have selected.
Refer @s/all for all the available features
Lombok helps keep your code clean, concise, and to the point also save time during the development.