@SuperBuilder
Bob now knows his ancestors: Builders with fields from superclasses, too.
@SuperBuilder was introduced as experimental feature in lombok v1.18.2.
@SuperBuilder's toBuilder feature and limited support for customization was added with lombok v1.18.4.
@SuperBuilder customization possibilities were extended with lombok v1.18.14.
Overview
The @SuperBuilder annotation produces complex builder APIs for your classes.
In contrast to @Builder, @SuperBuilder also works with fields from superclasses.
However, it only works for types.
Most importantly, it requires that all superclasses also have the @SuperBuilder annotation.
@SuperBuilder lets you automatically produce the code required to have your class be instantiable with code such as:
Person.builder().name("Adam Savage").city("San Francisco").job("Mythbusters").job("Unchained Reaction").build();
@SuperBuilder can generate so-called 'singular' methods for collection parameters/fields. For details, see the @Singular documentation in @Builder.
@SuperBuilder generates a protected constructor on the class that takes a builder instance as a parameter. This constructor sets the fields of the new instance to the values from the builder.
@SuperBuilder is not compatible with @Builder.
You can use @SuperBuilder(toBuilder = true) to also generate an instance method in your class called toBuilder(); it creates a new builder that starts out with all the values of this instance.
Using toBuilder requires that all superclasses also have toBuilder = true.
You can put the @Builder.ObtainVia annotation on the fields to indicate alternative means by which the value for that field/parameter is obtained from this instance.
For example, you can specify a method to be invoked: @Builder.ObtainVia(method = "calculateFoo").
To ensure type-safety, @SuperBuilder generates two inner builder classes for each annotated class, one abstract and one concrete class named FoobarBuilder and FoobarBuilderImpl (where Foobar is the name of the annotated class).
You can customize most of the code generated by @SuperBuilder, except for internal methods (e.g. self()).
You have to make sure that the builder class declaration headers match those that would have been generated by lombok.
Due to the heavy generics usage, we strongly advice to take the uncustomized delomboked code as a reference when customizing @SuperBuilder.
The configurable aspects of builder are:
-
The build() method's name (default:
"build") -
The builder() method's name (default:
"builder") -
If you want
toBuilder()(default: no) -
(discouraged) If you want your builder's 'set' methods to have a prefix, i.e.
Person.builder().setName("Jane").build()instead ofPerson.builder().name("Jane").build()and what it should be.
@SuperBuilder(buildMethodName = "execute", builderMethodName = "helloWorld", toBuilder = true, setterPrefix = "set")Looking to use your superbuilder with Jackson, the JSON/XML tool? We have you covered: Check out the @Jacksonized feature.
Supported configuration keys:
-
lombok.builder.className= [a java identifier with an optional star to indicate where the return type name goes] (default:*Builder) -
This is the name of the generated builder class; any star in the name is replaced with the relevant return type. Note that the parent class must also have the same setting (the entire type hierarchy annoated with
@SuperBuilderneeds the same setting). -
lombok.superBuilder.flagUsage= [warning|error] (default: not set) -
Lombok will flag any usage of
@SuperBuilderas a warning or error if configured. -
lombok.singular.useGuava= [true|false] (default: false) -
If
true, lombok will use guava'sImmutableXxxbuilders and types to implementjava.utilcollection interfaces, instead of creating implementations based onCollections.unmodifiableXxx. You must ensure that guava is actually available on the classpath and buildpath if you use this setting. Guava is used automatically if your field/parameter has one of the guavaImmutableXxxtypes. -
lombok.singular.auto= [true|false] (default: true) -
If
true(which is the default), lombok automatically tries to singularize your identifier name by assuming that it is a common english plural. Iffalse, you must always explicitly specify the singular name, and lombok will generate an error if you don't (useful if you write your code in a language other than english).
Small print
The generated builder code heavily relies on generics to avoid class casting when using the builder.
For remarks on @Singular, see the @Builder documentation's small print.
Various well known annotations about nullity cause null checks to be inserted and will be copied to parameter of the builder's 'setter' method. See Getter/Setter documentation's small print for more information.