Lombok: The Java Plugin that Substantially Reduces Boilerplate
Nov 24th, 2020
Java's Required Boilerplate Code
Java is notorious for the huge amount of boilerplate code required for simple things, especially everyday Java POJO beans.
POJO
POJO is an acronym for 'Plain Old Java Object'.
These objects typically exist to store or transport data. We sometimes call these objects beans.
Typical POJO Bean Boilerplate Code
Let's look at a real-world example, a user bean:
public class User{
private Long id;
private String username;
private String email;
private String name;
}
This POJO has four fields:
- id
- username
- name
Let's look at the typical code we would need for this POJO bean.
Setters
Setters are methods that allow us to assign values to bean properties.
public void setName(String name){
this.name = name;
}
Getters
Getters are methods that allow us to retrieve values from our POJO beans.
public String getName(){
return this.name;
}
Constructors
Constructors are methods that allow use to construct new instances of a Java object with given data.
Default No-Args Constructor
In Java, the default constructor is one that takes no arguments, and creates an empty default bean.
If you don't define any constructors, the compiler will automatically create the default constructor.
public User(){
}
All-Args Constructor
In Java, the all-args constructor takes an argument for every field in the bean.
public User(Long id, String username, String email, String name){
this.id = id;
this.username;
this.email = email;
this.name = name;
}
Required-Args Constructor
You can define a required-args constuctor if you only need a few properties at initialization.
public User(Long id){
this.id = id;
}
Lombok
Enter Lombok, a Java build plugin that generates all this code at compile time, reducing the countless hours required to engineer all this boilerplate code for our POJO beans. Lombok is configured by adding annotations to your POJO beans.
Lombok does many useful things, but we will only focus on the types of boilerplate defined above.
Adding Lombok to a Project
For maven, add this to your pom.xml
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
For gradle, add this to your build.gradle file
providedCompile group: 'org.projectlombok', name: 'lombok'
Lombok Annotations
@Setter
The @Setter annotation auto-generates a setter for a POJO bean field
private String name;
public void setName(String name){
this.name = name;
}
becomes
@Setter
private String name;
@Getter
The @Getter annotation auto-generates a getter for a POJO bean field
private String name;
public String getName(){
return this.name;
}
becomes
@Getter
private String name;
@Data
The @Data annotation auto-generates a setter and getter for every POJO bean field.
public class Bean {
private String stringField;
public String getStringField(){
return this.stringField;
}
public void setStringField(String stringField) {
this.stringField = stringField;
}
}
becomes
@Data
public class Bean {
private String stringField;
}
@AllArgsConstructor
The @AllArgsConstructor annotration automatically generates an all-args constructor
public class Bean {
private String stringField;
public Bean(String stringField) {
this.stringField = stringField;
}
}
becomes
@AllArgsConstructor
public class Bean {
private String stringField;
}
@NoArgsConstructor
Similar to @AllArgsConstructor, @NoArgsConstructor generates a default constructor.
Note that you will only need this if you have defined a different constructor, this is because the Java compiler automatically generates the no-args constructor when no constructor is defined.
Conclusion
Java requires a lot of boilerplate code for our very common everyday POJO beans. Lombok is a java build plugin that generates boilerplate code at compile-time.
By switching multiple lines of code with single annotations, Lombok is sure to save you much time. If you are not using Lombok now, it is about time to start.
Comments