This strategy is used with the
Builder
AST transform to populate a builder helper class
so that it can be used for the fluent creation of instances of a specified class. The specified class is not modified in any way and may be a Java class.
You use it by creating and annotating an explicit builder class which will be filled in by during
annotation processing with the appropriate build method and setters. An example is shown here:
import groovy.transform.builder.*
class Person {
String firstName
String lastName
}
@Builder
(builderStrategy=ExternalStrategy, forClass=Person)
class PersonBuilder { }
def person = new PersonBuilder().firstName("Robert").lastName("Lewandowski").build()
assert person.firstName == "Robert"
assert person.lastName == "Lewandowski"
The
prefix
annotation attribute, which defaults to the empty String for this strategy, can be used to create setters with a different naming convention, e.g. with
the
prefix
changed to 'set', you would use your setters as follows:
def p1 = new PersonBuilder().setFirstName("Robert").setLastName("Lewandowski").setAge(21).build()
or using a prefix of 'with':
def p2 = new PersonBuilder().withFirstName("Robert").withLastName("Lewandowski").withAge(21).build()
The properties to use can be filtered using either the 'includes' or 'excludes' annotation attributes for
@Builder
.
The
@Builder
'buildMethodName' annotation attribute can be used for configuring the build method's name, default "build".
The
@Builder
'builderMethodName' and 'builderClassName' annotation attributes aren't applicable for this strategy.
The
@Builder
'useSetters' annotation attribute is ignored by this strategy which always uses setters.