Introduction to Project Lombok in Java and How to get started?
Java is a very popular language, but it has few drawbacks. One of the most popular drawbacks is that we still need to write the boilerplate codes like getters, setters, toString method in Java whereas Kotlin and Scala, which are also JVM based don’t need so and hence, this is the reason for their increased popularity in the community. This is where Lombok comes into the picture and overcomes this drawback of Java.
Project Lombok is a java library tool that is used to minimize/remove the boilerplate code and save the precious time of developers during development by just using some annotations. In addition to it, it also increases the readability of the source code and saves space. But you might be thinking that nowadays, everyone uses IDEs which provides an option for generating these boilerplate codes, then what is the use of Lombok. Whenever we use IDEs to generate these boilerplate codes, we just save ourselves from writing all these codes but it is actually present in our source code and increases the LOC (lines of code), and reduces maintainability and readability. On the other hand, Lombok adds all these boilerplate codes at the compile-time in the “.class” file and not in our source code.
Let us compare our source code with and without using Lombok.
1. Without Lombok: A java model class with four private fields and their getters, setters, no-args constructor, parameterized construct, and toString method.
2. With Lombok: A java model class with four private fields and their getters, setters, no-args constructor, parameterized construct, and toString method using Lombok annotations.
The difference is clearly visible in the codes. The original source code contains around 60 lines of code which has been reduced by Lombok to around 20 lines of code only. It not only reduces the lines of code but also increases the readability and maintainability of source code.
How to configure project Lombok to eclipse?
1. Download the Lombok jar file from this official link.
2. Double click on the downloaded jar file to execute it. A GUI will appear to specify the IDE in which you want to plugin it.
3. Click on specify a location and select the IDE.
4. Click on install to complete the installation and then quit the installer.
5. To verify the correct installation, go to the Eclipse IDE, select help, and then about the eclipse. You will see project Lombok installed there.
- Lombok annotations: Lombok provides a set of annotations to make our coding life easier. Let us look at the few most frequently used annotations of Lombok.
1. @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. @Getter annotation generates a getter method with access type as public which simply returns the field and with name getName() if the field name is “Name”. @Setter annotation generates a setter method with access type as public which returns void and takes a single parameter to assign the value to the field. The default setter will have the name setName() if the field name is “Name”.
2. @NoArgsConstructor: This annotation is used to generate a constructor with no arguments. It has an empty body and does nothing. It is generally used in combination with some other parameterized constructor in use. It is required when you want to generate an object of the class by passing no arguments in the constructor.
3. @AllArgsConstructor: This annotation is used to generate a parameterized constructor which accepts a single parameter for each field and initializes them using it. It is required when you want to generate an object of the class by passing the initial values of the fields in the constructor.
4. @ToString: This annotation is used to override the toString() method and generate a default implementation for it. The default implementation prints the class name and the fields in order, separated by commas. You can also skip some fields that you don’t want to print by annotating them with @ToString.Exclude.
5. @EqualsAndHashCode: This annotation is used to override the equals() and hashCode() methods and provides a default implementation for thu. The default implementation uses all the non-static fields, and we can modify it and can exclude some fields using the annotation @EqualsAndHashCode.Exclude.
6. @Data: This annotation is a shortcut annotation and bundles @ToString, @Getter, @Setter, @EqualsAndHashCode and @RequiredArgsConstructor annotations into a single annotation. This annotation provides all the normally used boilerplate code in the model classes of java like getters for all the fields, setter for all the non-final fields, a default implementation for toString(), equals() and hashCode() using all the fields of the class and a constructor that initializes all the fields of the class.