Using Lombok to Exclude Fields from the Builder Pattern with @Builder- A Comprehensive Tutorial
Lombok, the popular Java library, streamlines code generation, making it easier for developers to write efficient code. Although the @Builder annotation in Lombok does not have an ‘exclude’ attribute for its Builder pattern, there are effective workarounds to control field inclusion. This tutorial delves into these alternatives, including the use of custom constructors or factory methods, complemented by practical examples.
Setting Up Lombok
To begin, you need to add Lombok as a dependency to your Java project. If you’re using Maven, add the following dependency to your pom.xml
:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version> <!-- latest version -->
<scope>provided</scope>
</dependency>
For Gradle, add this to your build.gradle
:
dependencies {
compileOnly 'org.projectlombok:lombok:1.18.28' // latest version
annotationProcessor 'org.projectlombok:lombok:1.18.28'
}
Creating a Class with Selectively Included Fields Using Custom Constructors or Factory Methods
Suppose you have a class, Person
, and you want to selectively include fields in the Builder pattern. Instead of using an exclude attribute, you can use the @Builder annotation on a constructor or a static factory method. This way, you include only the fields you need in the builder:
import lombok.Builder;
public class Person {
private String firstName;
private String lastName;
private String email;
private int age;
@Builder
private Person(String firstName, String lastName, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
}
In this example, the email field is not included in the constructor annotated with @Builder, thus it’s excluded from the builder process.
Using the Customized Builder
Now that you’ve defined your class with excluded fields, you can use the generated Builder class to create instances. Lombok generates a toBuilder()
method that allows you to create a new Builder instance based on an existing object. Here’s how you can use it:
public class Main {
public static void main(String[] args) {
Person person = Person.builder()
.firstName("John")
.lastName("Doe")
.age(30)
.build();
Person modifiedPerson = person.toBuilder()
.firstName("Jane")
.build();
System.out.println("Original Person: " + person);
System.out.println("Modified Person: " + modifiedPerson);
}
}
In this example, we first create a Person
instance using the Builder pattern. Then, we create a modified version of the Person
object using the toBuilder()
method and excluding the firstName
field.
Conclusion
While Lombok’s @Builder annotation lacks a direct exclude feature, using it with custom constructors or static factory methods allows for selective field inclusion. This technique ensures cleaner code and greater control, particularly for classes with optional or less important fields. By applying the methods outlined in this tutorial, you can effectively manage field inclusion in the Builder pattern using Lombok in your Java projects.
Related Posts:
There is no `exclude` attribute in Lombok’s `@Builder`[1]. Fields can be excluded by placing `@Builder` on a constructor containing just the wanted fields[2].
[1] https://github.com/projectlombok/lombok/blob/v1.18.28/src/core/lombok/Builder.java
[2] https://stackoverflow.com/a/30725879/833399
Hi Wacker,
You’re absolutely correct. Lombok’s @Builder annotation does not include an exclude attribute. The technique you’ve mentioned, placing the @Builder annotation on a constructor that only contains the desired fields, is indeed a practical workaround to selectively include fields in the Builder pattern. This approach provides a way to effectively exclude fields without needing an explicit ‘exclude’ feature. By doing this, the fields not present in the constructor are implicitly excluded from the builder process, giving developers control over which fields are included in the generated builder. Post had been updated.