In this post, we will see how to use @Data Lombok annotation with an example.
What is the @Data Annotation?
The @Data annotation is a part of the Lombok library, which automatically generates boilerplate code for your classes. When you apply this annotation to a class, Lombok will generate the following:
- Getter methods for all fields
- Setter methods for all non-final fields
- equals() and hashCode() methods
- toString() method
- A constructor for all final fields (if @NoArgsConstructor and @AllArgsConstructor aren't used)
Project Lombok Maven Dependency
Add the below dependency in your maven project pom.xml file:
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>latest-version</version>
<scope>provided</scope>
</dependency>
Adding the Lombok Plugin in IDE (Eclipse)
Follow the below steps to install Lombok in Eclipse for Windows:
- Downloaded the jar from https://projectlombok.org/download or use the jar which is downloaded from your maven build.
- Execute command in terminal: java -jar lombok.jar
- This command will open the window as shown in the picture below, install and quit the installer and restart Eclipse.
Without Project Lombok
Without @Data annotation, we need to write the below code to generate getter/setter methods, equals(), hashCode(), toString(), and all arguments constructor:
package net.javaguides.lombok;
import java.time.LocalDate;
public class User {
private long id;
private String firstName;
private String lastName;
private int age;
private LocalDate createdDate;
private LocalDate updatedDate;
private String gender;
public User(long id, String firstName, String lastName, int age, LocalDate createdDate, LocalDate updatedDate,
String gender) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.createdDate = createdDate;
this.updatedDate = updatedDate;
this.gender = gender;
}
public long getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public int getAge() {
return age;
}
public LocalDate getCreatedDate() {
return createdDate;
}
public LocalDate getUpdatedDate() {
return updatedDate;
}
public String getGender() {
return gender;
}
public void setId(long id) {
this.id = id;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setAge(int age) {
this.age = age;
}
public void setCreatedDate(LocalDate createdDate) {
this.createdDate = createdDate;
}
public void setUpdatedDate(LocalDate updatedDate) {
this.updatedDate = updatedDate;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((createdDate == null) ? 0 : createdDate.hashCode());
result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
result = prime * result + ((gender == null) ? 0 : gender.hashCode());
result = prime * result + (int)(id ^ (id >>> 32));
result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
result = prime * result + ((updatedDate == null) ? 0 : updatedDate.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (age != other.age)
return false;
if (createdDate == null) {
if (other.createdDate != null)
return false;
} else if (!createdDate.equals(other.createdDate))
return false;
if (firstName == null) {
if (other.firstName != null)
return false;
} else if (!firstName.equals(other.firstName))
return false;
if (gender == null) {
if (other.gender != null)
return false;
} else if (!gender.equals(other.gender))
return false;
if (id != other.id)
return false;
if (lastName == null) {
if (other.lastName != null)
return false;
} else if (!lastName.equals(other.lastName))
return false;
if (updatedDate == null) {
if (other.updatedDate != null)
return false;
} else if (!updatedDate.equals(other.updatedDate))
return false;
return true;
}
@Override
public String toString() {
return "User [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", age=" + age +
", createdDate=" + createdDate + ", updatedDate=" + updatedDate + ", gender=" + gender + "]";
}
}
With Project Lombok
With Project Lombok, we can remove all of these methods here. Just with @Data annotation in Project Lombok.package net.javaguides.lombok.data;
import java.time.LocalDate;
import lombok.Data;
/**
* @Data is a convenient shortcut annotation that bundles the features of
* @ToString, @EqualsAndHashCode, @Getter / @Setter and @RequiredArgsConstructor together.
*/
@Data
public class UserLombokModel {
private long id;
private String firstName;
private String lastName;
private int age;
private LocalDate createdDate;
private LocalDate updatedDate;
private String gender;
}
Testing
As you installed Lombok plugin in Eclipse so we can test using the main() method:
package net.javaguides.lombok.data;
import java.time.LocalDate;
public class LombokTest {
public static void main(String[] args) {
UserLombokModel lombokModel = new UserLombokModel();
// test setter methods
lombokModel.setId(100 L);
lombokModel.setFirstName("Ramesh");
lombokModel.setLastName("Fadatare");
lombokModel.setAge(28);
lombokModel.setGender("Male");
lombokModel.setCreatedDate(LocalDate.now());
lombokModel.setUpdatedDate(LocalDate.now());
UserLombokModel lombokModel2 = new UserLombokModel();
lombokModel2.setId(100 L);
lombokModel2.setFirstName("Ramesh");
lombokModel2.setLastName("Fadatare");
lombokModel2.setAge(28);
lombokModel2.setGender("Male");
lombokModel2.setCreatedDate(LocalDate.now());
lombokModel2.setUpdatedDate(LocalDate.now());
// equals method
System.out.println(lombokModel.equals(lombokModel2));
// hashCode method
System.out.println(lombokModel.hashCode());
System.out.println(lombokModel2.hashCode());
// toString method
System.out.println(lombokModel.toString());
}
}
Why Use @Data?
Reduced Boilerplate: No need to manually write getters, setters, and other common methods.
Consistency: Ensures consistent implementation of methods like equals() and hashCode().
Readability: The class remains clean and focused on its fields.
Maintainability: Reduces the potential for errors in manual implementations.
A Note of Caution
While @Data is extremely convenient, it's crucial to be aware of when to use it. Classes with specific requirements for methods like equals() or toString() may need a more nuanced approach. It's also crucial to recognize that adding or removing fields can impact methods generated by @Data.
Comments
Post a Comment
Leave Comment