When you create a Java domain class with JavaBeans pattern, in other words, with all encapsulated attributes and the default constructor, Don’t you think there is unnecessary code in you java class?
For this problem, I present you the Project Lombok that help us to clean our code and give us annotations that removes the necessity of create methods like getters, setters, constructors, hash, equals, etc.
Lombok in action!
Person class without Lombok:
import java.time.LocalDate;
import java.util.Objects;
public class Person {
private String name;
private LocalDate birthDate;
private String email;
public Person(String name, LocalDate birthDate, String email) {
this.name = name;
this.birthDate = birthDate;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LocalDate getBirthDate() {
return birthDate;
}
public void setBirthDate(LocalDate birthDate) {
this.birthDate = birthDate;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return Objects.equals(name, person.name) &&
Objects.equals(birthDate, person.birthDate) &&
Objects.equals(email, person.email);
}
@Override
public int hashCode() {
return Objects.hash(name, birthDate, email);
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", birthDate=" + birthDate +
", email='" + email + '\'' +
'}';
}
}
Person with Project Lombok:
import lombok.AllArgsConstructor;
import lombok.Data;
import java.time.LocalDate;
@Data
@AllArgsConstructor
public class Person {
private String name;
private LocalDate birthDate;
private String email;
}
Here we can see, a file with 65 lines was reduced to 14 lines. Incredible, Don’t you think?
Lombok Configuration
To use Lombok in your project, you will need to configure your IDE and add a dependency in your pom.xml or build.graddle of your project.
Configure your IDE:
- IntelliJ IDEA: https://projectlombok.org/setup/intellij
- Eclipse: https://projectlombok.org/setup/eclipse
- NetBeans: https://projectlombok.org/setup/netbeans
- Visual Studio Code: https://projectlombok.org/setup/vscode
To include Lombok dependency, add it to your <dependencies> block if you are using Maven:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<scope>provided</scope>
</dependency>
Or add it if you are using Graddle:
repositories {
mavenCentral()
}
dependencies {
compileOnly 'org.projectlombok:lombok:1.18.6'
annotationProcessor 'org.projectlombok:lombok:1.18.6'
}
Lombok Annotations
All Lombok annotations, you can find here.
Conclusion
I really like Project Lombok and I think that it will be incorporated in a future Java version. Project Lombok creates the code when the project is compiled, so if you aren’t use the code that Lombok creates, your compiled java class will have unnecessary code instead your Java file.
For the comments, I would like to know if you use Lombok or if will
you start to use? See you next time!