How to use project Lombok

Isaac Tonyloi - SWE - Jul 24 - - Dev Community

Project Lombok is a Java library that helps reduce boilerplate code by automatically generating commonly used methods and annotations. It provides annotations that can simplify and reduce the verbosity of your Java classes, making your code more readable and maintainable.

Why Use Project Lombok?

  1. Reduce Boilerplate Code:

    • Lombok generates commonly used code like getters, setters, constructors, toString(), equals(), and hashCode() methods, which reduces repetitive code and clutter in your classes.
  2. Improve Readability:

    • By removing boilerplate code, Lombok makes your classes more concise and easier to read and understand.
  3. Maintainability:

    • With less boilerplate code, your codebase is easier to maintain and refactor.
  4. Consistent Coding Style:

    • Lombok ensures a consistent style for common methods and reduces the chances of errors or inconsistencies.

How to Use Project Lombok

Step 1: Add Lombok to Your Project

Maven:
Add the Lombok dependency to your pom.xml file:

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.22</version>
    <scope>provided</scope>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Gradle:
Add the Lombok dependency to your build.gradle file:

dependencies {
    compileOnly 'org.projectlombok:lombok:1.18.22'
    annotationProcessor 'org.projectlombok:lombok:1.18.22'
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Enable Lombok in Your IDE

Most IDEs like IntelliJ IDEA and Eclipse support Lombok, but you need to install the Lombok plugin to enable it.

IntelliJ IDEA:

  1. Go to File > Settings > Plugins.
  2. Search for Lombok and install it.
  3. Restart IntelliJ IDEA.

Eclipse:

  1. Go to Help > Eclipse Marketplace.
  2. Search for Lombok and install it.
  3. Restart Eclipse.

Step 3: Use Lombok Annotations in Your Code

Here are some commonly used Lombok annotations:

@Getter and @ Setter:
Generates getter and setter methods for the fields of your class.

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class User {
    private Long id;
    private String name;
    private String email;
}
Enter fullscreen mode Exit fullscreen mode

@ToString:
Generates a toString() method for your class.

import lombok.ToString;

@ToString
public class User {
    private Long id;
    private String name;
    private String email;
}
Enter fullscreen mode Exit fullscreen mode

@EqualsAndHashCode:
Generates equals() and hashCode() methods for your class.

import lombok.EqualsAndHashCode;

@EqualsAndHashCode
public class User {
    private Long id;
    private String name;
    private String email;
}
Enter fullscreen mode Exit fullscreen mode

@NoArgsConstructor, @AllArgsConstructor, and @RequiredArgsConstructor:
Generates constructors for your class.

import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;

@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
public class User {
    private Long id;
    private final String name;
    private final String email;
}
Enter fullscreen mode Exit fullscreen mode

@data:
A convenient shortcut that bundles the features of @ToString, @EqualsAndHashCode, @Getter on all fields, and @Setter on all non-final fields, and @RequiredArgsConstructor.

import lombok.Data;

@Data
public class User {
    private Long id;
    private String name;
    private String email;
}
Enter fullscreen mode Exit fullscreen mode

Example Class Using Lombok

Here is an example of a class before and after using Lombok:

Without Lombok:

public class User {
    private Long id;
    private String name;
    private String email;

    public User() {}

    public User(Long id, String name, String email) {
        this.id = id;
        this.name = name;
        this.email = email;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", email='" + email + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return Objects.equals(id, user.id) &&
                Objects.equals(name, user.name) &&
                Objects.equals(email, user.email);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, email);
    }
}
Enter fullscreen mode Exit fullscreen mode

With Lombok:

import lombok.Data;

@Data
public class User {
    private Long id;
    private String name;
    private String email;
}
Enter fullscreen mode Exit fullscreen mode

Summary

  • Project Lombok is used to reduce boilerplate code in Java applications by automatically generating commonly used methods and annotations.
  • Benefits: Reduces boilerplate code, improves readability, enhances maintainability, and ensures consistent coding style.
  • Common Annotations: @Getter, @Setter, @ToString, @EqualsAndHashCode, @NoArgsConstructor, @AllArgsConstructor, @RequiredArgsConstructor, and @Data.
  • Setup: Add Lombok dependency to your project, enable Lombok in your IDE, and use Lombok annotations in your code.

By integrating Lombok into your project, you can significantly simplify your code and focus more on business logic rather than writing repetitive boilerplate code.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .