How to Use the @Builder Annotation of Project Lombok

How to Use the @Builder Annotation of Project Lombok

This article explains how to make use of the @Builder annotation of Project Lombok.

This post will focus on how to make use of the @Builder annotation of Project Lombok. The Lombok Project is a java library that helps a developer generate boilerplate code. By simply adding the Lombok library to your IDE and build path the Lombok library will auto-generate the Java bytecode, as per the annotations, into the class files.

The @Builder annotation produces complex builder APIs for the class. The @Builder annotation can be placed on a class, or a constructor or a method.

Article Series

This article forms part of a multi-part series on how to use the Project Lombok to generate boilerplate code in java projects.

Define Maven Dependencies

The following dependencies should be included in the pom.xml file.

<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.18.8</version>
  <scope>provided</scope>
</dependency>

Example: @Builder

The @Builder annotation produces complex builder APIs for the class. The @Builder annotation can be placed on a class, or on a constructor, or on a method.

Original Class

@Builder
public class BuilderExample01 {

    private long id;
    private String firstName;
    private String lastName;

    public static void main(String[] args) {
        BuilderExample01 example01 = null;

        example01 = BuilderExample01.builder()
                .id(1)
                .firstName("Joe")
                .lastName("Soap")
                .build();
    }
}

To truly appreciate the magic of the Lombok library, you should compile the BuilderExample01 class by making use of maven to build your project. As part of the compile process, Lombok will generate the boilerplate code depending on the type of annotation you used. To see the boilerplate code, you should decompile BuilderExample01.class file. An easy way to do this is to make use of the following URL:

Java Decompiler: http://www.javadecompilers.com

Decompiled Class

public class BuilderExample01 {
    private long id;
    private String firstName;
    private String lastName;
    
    public static void main(final String[] args) {
        BuilderExample01 example01 = null;
        example01 = builder().id(1L).firstName("Joe").lastName("Soap").build();
    }
    
    BuilderExample01(final long id, final String firstName, final String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }
    
    public static BuilderExample01.BuilderExample01Builder builder() {
        return new BuilderExample01.BuilderExample01Builder();
    }
}

The BuilderExample01Builder class is generated as part of the implementation of the builder pattern. The builder contains all of the fields that exist in the BuilderExample01 class.

public static class BuilderExample01Builder {
    private long id;
    private String firstName;
    private String lastName;
    
    BuilderExample01Builder() {
    }
    
    public BuilderExample01Builder id(final long id) {
        this.id = id;
        return this;
    }
    
    public BuilderExample01Builder firstName(final String firstName) {
        this.firstName = firstName;
        return this;
    }
    
    public BuilderExample01Builder lastName(final String lastName) {
        this.lastName = lastName;
        return this;
    }
    
    public BuilderExample01 build() {
        return new BuilderExample01(this.id, this.firstName, this.lastName);
    }
    
    @Override
    public String toString() {
        return "BuilderExample01.BuilderExample01Builder(id=" + this.id + ", firstName=" + this.firstName + ", lastName=" + this.lastName + ")";
    }
}

Summary

Congratulations !!! You have successfully used the @Builder annotation of Project Lombok. Please look out for more examples on how to make use of Project Lombok to simplify your Java coding experience. Follow me on any of the different social media platforms and feel free to leave comments.