Introduction to Project Lombok
Project Lombok is a boilerplate code remover and space saver that generates code in ".class" file instead of in the source code file. In this article, I will try to explain why Lombok is so popular and its implementation with a step-by-step example.
Introduction
Project Lombok is a Java library tool that is used to minimize boilerplate code and save timeduring development. It is no doubt that Java is a great language, but recently, it is criticized by the community for one important reason — verbosity.
Lombok generates code, for example, getters, setters, and toString
, and the IDE does the same thing for us only it generates in our source code while Lombok generates it in the ".class" file directly.
How to Configure Project Lombok to IDE?
Setup With IntelliJ
Step 1: Go to File > Settings > Plugins
Step 2: Click on Browse repositories
Step 3: Search for Lombok plugin
Step 4: Click on Install Plugin
Step 5: Restart IntelliJ IDEA
Setup With Eclipse
Lombok will operate if we put it in the project classpath, and for that, we need to do the following steps.
Step 1: Download lombok.jar from https://projectlombok.org/downloads/lombok.jar
Step 2: To have library version consistency and avoid version conflicts at the project level, let's use Maven to perform the download on our behalf.
Step 3: Create a Maven project and add the following Maven dependency in pom.xml.
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.16</version>
</dependency>
</dependencies>
Step 4: After building the project, you will find lombok.jar downloaded in the .m2 repository. Copy this jar and paste it to your Eclipse folder.
Step 5: Open the command prompt window and navigate to the location where lombok.jar is copied. Execute the command "java -jar lombok.jar." It will open an installation wizard, as shown below.
Step 6: Click on the "Specify location" button and provide a path of "eclipse.exe," as shown in the image below.
Step 7: After providing the path, click on the "Install/Update" button. This is the last step of installation and there will be a popup with the successful installation completion message.
Now, our IDE is equipped with the Lombok apparatus.
Lombok Annotations
Lombok provides a set of annotations. Some of the annotations can be used in day-to-day coding. Let's have a look at a few of them.
@Getter and @Setter
The @Getter
and @Setter
annotations provide getter and setter methods for a field respectively. These annotations can be used at the field and class level.
Encapsulating object properties using the public getter and setter methods are common practices in the Java language. Many frameworks rely on this "bean pattern," and therefore, IDE also supports autogeneration of the getter/setter and constructor code.
Example
Vanilla code (without the Lombok feature):
public class EmployeeWithoutLombok {
private String empId;
private String firstName;
private String lastname;
private Date dob;
private String phoneNo;
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public String getPhoneNo() {
return phoneNo;
}
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
}
Code with Lombok annotations:
@Getter
@Setter
public class EmployeeWithLombok {
private String empId;
private String firstName;
private String lastname;
private Date dob;
private String phoneNo;
// Code required for getter and setter method, not needed to write.
}
@ToString Annotation
This annotation generates the toString()
method in the ".class" file at compile time. There is no need to write explicit code for the toString()
method in the POJO class. If we use the @ToString
annotation, then we no longer need to maintain the toString()
method whenever we enrich POJO properties.
Example
Vanilla code (without the Lombok feature):
public class EmployeeWithoutLombok {
private String empId;
private String firstName;
private String lastname;
private Date dob;
private String phoneNo;
// toString method
@Override
public String toString() {
return "EmployeeWithoutLombok [empId=" + empId + ", firstName="
+ firstName + ", lastname=" + lastname + ", dob=" + dob
+ ", phoneNo=" + phoneNo + "]";
}
}
Code with Lombok:
@ToString
public class EmployeeWithoutLombok {
private String empId;
private String firstName;
private String lastname;
private Date dob;
private String phoneNo;
}
@Builder
This annotation is used to generate the code required to have your class be instantiable using the builder pattern.
Example
Builder pattern without the @Builder
Lombok annotation:
public class EmployeeWithoutLombok {
private String empId;
private String firstName;
private String lastname;
private Date dob;
private String phoneNo;
public EmployeeWithoutLombok(String empId, String firstName,
String lastname, Date dob, String phoneNo) {
this.empId = empId;
this.firstName = firstName;
this.lastname = lastname;
this.dob = dob;
this.phoneNo = phoneNo;
}
public static class EmployeeBuilder {
private String empId;
private String firstName;
private String lastname;
private Date dob;
private String phoneNo;
public EmployeeBuilder withEmpId(String empId) {
this.empId = empId;
return this;
}
public EmployeeBuilder withFirstName(String firstName) {
this.firstName = firstName;
return this;
}
public EmployeeBuilder withLastName(String lastName) {
this.lastname = lastName;
return this;
}
public EmployeeBuilder withDob(Date dob) {
this.dob = dob;
return this;
}
public EmployeeBuilder withPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
return this;
}
public EmployeeWithoutLombok build() {
return new EmployeeWithoutLombok(empId, firstName, lastname, dob,
phoneNo);
}
}
@Override
public String toString() {
return "EmployeeWithoutLombok [empId=" + empId + ", firstName="
+ firstName + ", lastname=" + lastname + ", dob=" + dob
+ ", phoneNo=" + phoneNo + "]";
}
}
Builder pattern with Lombok:
@Builder
public class EmployeeWithLombok {
private String empId;
private String firstName;
private String lastname;
private Date dob;
private String phoneNo;
}
@Log4j, @Slf4j
These annotations are used to generate code related to the logger, as demonstrated in the following example.
Code without Lombok:
public class EmployeeServiceWithoutLombok implements EmployeeService{
private static final Logger logger = Logger.getLogger(EmployeeServiceWithoutLombok.class);
@Override
public void method1() {
logger.info("In method1 ");
}
}
Code with Lombok annotations:
@Log4j
public class EmployeeServiceWithLombok implements EmployeeService{
@Override
public void method1() {
log.info("In method1 ");
}
}
Conclusion
Reducing boilerplate code results in better readability; less code means fewer errors. Project Lombok is becoming common in almost all major organizations and projects.
The examples found in this article are available on GitHub.
Additionally, the official Lombok document can be found here.