@Builder Lombok Annotation Example


In this post, I will show you how to use @Builder annotation to produce complex builder APIs for your classes.@Builder annotation lets you automatically produce the code required to have your class be instantiable.
 
Let's create a step-by-step example to demonstrate generating builder APIs for the User class.

What is the @Builder Annotation? 

Lombok's @Builder annotation produces a builder API for your class, simplifying object instantiation. It's an alternative to manually coding a builder, which often involves a lot of repetitive code. When you annotate a class, constructor, or method with @Builder, Lombok will automatically create an inner static Builder class with methods for setting properties and a build() method to create an instance of the containing class.

Project Lombok Maven

  1. Create a simple maven project using the - How to Create a Simple Maven Project in Eclipse article.
  2. 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.

With Project Lombok

With Project Lombok, we have an easy way to create the Builder object by just using @Builder annotation as below:
package net.javaguides.lombok.builder;

import lombok.Builder;

import lombok.ToString;

@Builder
@ToString
public class User {
    private long id;
    private String firstName;
    private String lastName;
    private int age;
    private String gender;
}

Testing

As you installed the Lombok plugin in Eclipse so we can test Project Lombok annotations:
package net.javaguides.lombok.builder;

import net.javaguides.lombok.builder.User.UserBuilder;

public class LombokBuilderTest {
    public static void main(String[] args) {
        // create builder class and add parameters to it.
        UserBuilder builder = new UserBuilder();

        builder.id(100 L);
        builder.firstName("Ramesh");
        builder.lastName("Fadatare");
        builder.age(28);
        builder.gender("Male");
        builder.build();
        System.out.println();
        System.out.println(builder.toString());
        System.out.println();
    }
}

Generated UserBuilder Class

Using @Builder annotation, when generating the class, a new Builder class will be added. With the above example, if you check the User class in folder /target/classes/net/javaguides/lombok, you will see the content below:
package net.javaguides.lombok.builder;

public class User
{
  private long id;
  private String firstName;
  private String lastName;
  private int age;
  private String gender;
  
  public static class UserBuilder
  {
    private long id;
    private String firstName;
    private String lastName;
    private int age;
    private String gender;
    
    public String toString()
    {
      return "User.UserBuilder(id=" + this.id + ", firstName=" + this.firstName + ", lastName=" + this.lastName + ", age=" + this.age + ", gender=" + this.gender + ")";
    }
    
    public User build()
    {
      return new User(this.id, this.firstName, this.lastName, this.age, this.gender);
    }
    
    public UserBuilder gender(String gender)
    {
      this.gender = gender;return this;
    }
    
    public UserBuilder age(int age)
    {
      this.age = age;return this;
    }
    
    public UserBuilder lastName(String lastName)
    {
      this.lastName = lastName;return this;
    }
    
    public UserBuilder firstName(String firstName)
    {
      this.firstName = firstName;return this;
    }
    
    public UserBuilder id(long id)
    {
      this.id = id;return this;
    }
  }
  
  User(long id, String firstName, String lastName, int age, String gender)
  {
    this.id = id;this.firstName = firstName;this.lastName = lastName;this.age = age;this.gender = gender;
  }
  
  public static UserBuilder builder()
  {
    return new UserBuilder();
  }
  
  public String toString()
  {
    return "User(id=" + this.id + ", firstName=" + this.firstName + ", lastName=" + this.lastName + ", age=" + this.age + ", gender=" + this.gender + ")";
  }
}
This is a very simple example, you can check out the https://projectlombok.org/features/Builder link to create more complex builder APIs for your Java classes.

Why Use @Builder? 

Fluent API: Provides a fluent and intuitive API for creating objects. 

Immutable Objects: Works well with immutable classes where fields are marked final. 

Reduced Boilerplate: Reduces the need to write repetitive builder logic. 

Comments

Post a Comment

Leave Comment