From 14af3bec3b601d52c6a34710a63e22fceebf8dde Mon Sep 17 00:00:00 2001
From: Enrique da Costa Cambio @Builder
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();
- @Builder
can be placed on a class, or on a constructor, or on a static method. While the "on a class" and "on a constructor"
- mode are the most common use-case, @Builder
is most easily explained with the "static method" use-case.
+ @Builder
can be placed on a class, or on a constructor, or on a method. While the "on a class" and "on a constructor"
+ mode are the most common use-case, @Builder
is most easily explained with the "method" use-case.
- A static method annotated with @Builder
(from now on called the target) causes the following 7 things to be generated:
FooBuilder
, with the same type arguments as the static method (called the builder).@Builder
(from now on called the target) causes the following 7 things to be generated:FooBuilder
, with the same type arguments as the method (called the builder).build()
method which calls the static method, passing in each field. It returns the same type that the
+ build()
method which calls the method, passing in each field. It returns the same type that the
target returns.toString()
implementation.builder()
method, which creates a new instance of the builder.Person.builder().job("Mythbusters").job("Unchained Reaction").build();
would result in the List<String> jobs
field to have 2 strings in it. To get this behaviour, the field/parameter needs to be annotated with @Singular
. The feature has its own documentation.
- Now that the "static method" mode is clear, putting a @Builder
annotation on a constructor functions similarly; effectively,
+ Now that the "method" mode is clear, putting a @Builder
annotation on a constructor functions similarly; effectively,
constructors are just static methods that have a special syntax to invoke them: Their 'return type' is the class they construct, and their
type parameters are the same as the type parameters of the class itself.
@@ -61,8 +61,8 @@
The name of the builder class is FoobarBuilder
, where Foobar is the simplified, title-cased form of the return type of the
target - that is, the name of your type for @Builder
on constructors and types, and the name of the return type for @Builder
- on static methods. For example, if @Builder
is applied to a class named com.yoyodyne.FancyList<T>
, then the builder name will be
- FancyListBuilder<T>
. If @Builder
is applied to a static method that returns void
, the builder will be named
+ on methods. For example, if @Builder
is applied to a class named com.yoyodyne.FancyList<T>
, then the builder name will be
+ FancyListBuilder<T>
. If @Builder
is applied to a method that returns void
, the builder will be named
VoidBuilder
.
The configurable aspects of builder are:
- By annotating one of the parameters (if annotating a static method or constructor with @Builder
) or fields (if annotating a class with @Builder
) with the
+ By annotating one of the parameters (if annotating a method or constructor with @Builder
) or fields (if annotating a class with @Builder
) with the
@Singular
annotation, lombok will treat that builder node as a collection, and it generates 2 'adder' methods instead of a 'setter' method. One which adds a single element to the collection, and one
which adds all elements of another collection to the collection. No setter to just set the collection (replacing whatever was already added) will be generated. These 'singular' builders
are very complicated in order to guarantee the following properties:
--
cgit