Project Lombok - @Data Annotation Example


In this post, I will show you how to reduce boilerplate code for the below methods using @Data annotation.
  1. getters for all fields
  2. setters for all non-final fields,
  3. toString method
  4. equals and hashCode implementations that involve the fields of the class
  5. constructor that initializes all final fields

Video

This tutorial is explained in the below Youtube Video. Subscribe to my youtube channel to learn more about Spring boot at Java Guides - YouTube Channel.

@Data Annotation Overview

@Data is a convenient shortcut annotation that bundles the features of @ToString, @EqualsAndHashCode, @Getter / @Setter, and @RequiredArgsConstructor together.

Project Lombok Maven

Create a simple maven project using - How to Create a Simple Maven Project in Eclipse article.
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>1.18.4</version>
    <scope>provided</scope>
</dependency>

Adding the Lombok Plugin in IDE (Eclipse)

Follow below steps to install Lombok in eclipse for Windows:
  • Downloaded 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 window as shown in the picture below, install and quit the installer and restart eclipse.

Without Project Lombok

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 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());
    }
}

Comments

  1. Very informative article, Which you have shared here about the Data Annotation. After reading your article I got very much information and it is very useful for us. unbiased is the best Data annotation platform.

    ReplyDelete

Post a Comment