This strategy is used with the
Builder
AST transform to create a builder helper class
for the fluent creation of instances of a specified class. It can be used at the class,
static method or constructor levels.
You use it as follows:
import groovy.transform.builder.*
@Builder
class Person {
String firstName
String lastName
int age
}
def person = Person.builder().firstName("Robert").lastName("Lewandowski").age(21)
assert person.firstName == "Robert"
assert person.lastName == "Lewandowski"
assert person.age == 21
The
prefix
annotation attribute can be used to create setters with a different naming convention. The default is the
empty string but you could change that to "set" as follows:
@Builder
(prefix='set')
class Person {
String firstName
String lastName
int age
}
def p2 = Person.builder().setFirstName("Robert").setLastName("Lewandowski").setAge(21)
or using a prefix of 'with' would result in usage like this:
def p3 = Person.builder().withFirstName("Robert").withLastName("Lewandowski").withAge(21)
You can also use the
@Builder
annotation in combination with this strategy on one or more constructor or
static method instead of or in addition to using it at the class level. An example with a constructor follows:
import groovy.transform.ToString
import groovy.transform.builder.Builder
@ToString
class Person {
String first, last
int born
@Builder
Person(String roleName) {
if (roleName == 'Jack Sparrow') {
first = 'Johnny'; last = 'Depp'; born = 1963
}
}
}
assert Person.builder().roleName("Jack Sparrow").build().toString() == 'Person(Johnny, Depp, 1963)'
In this case, the parameter(s) for the constructor or static method become the properties available
in the builder. For the case of a static method, the return type of the static method becomes the
class of the instance being created. For static factory methods, this is normally the class containing the
static method but in general it can be any class.
Note: if using more than one
@Builder
annotation, which is only possible when using static method
or constructor variants, it is up to you to ensure that any generated helper classes or builder methods
have unique names. E.g. we can modify the previous example to have three builders. At least two of the builders
in our case will need to set the 'builderClassName' and 'builderMethodName' annotation attributes to ensure
we have unique names. This is shown in the following example:
import groovy.transform.builder.*
import groovy.transform.*
@ToString
@Builder
class Person {
String first, last
int born
Person(){} // required to retain no-arg constructor
@Builder
(builderClassName='MovieBuilder', builderMethodName='byRoleBuilder')
Person(String roleName) {
if (roleName == 'Jack Sparrow') {
this.first = 'Johnny'; this.last = 'Depp'; this.born = 1963
}
}
@Builder
(builderClassName='SplitBuilder', builderMethodName='splitBuilder')
static Person split(String name, int year) {
def parts = name.split(' ')
new Person(first: parts[0], last: parts[1], born: year)
}
}
assert Person.splitBuilder().name("Johnny Depp").year(1963).build().toString() == 'Person(Johnny, Depp, 1963)'
assert Person.byRoleBuilder().roleName("Jack Sparrow").build().toString() == 'Person(Johnny, Depp, 1963)'
assert Person.builder().first("Johnny").last('Depp').born(1963).build().toString() == 'Person(Johnny, Depp, 1963)'
The 'forClass' annotation attribute for the
@Builder
transform isn't applicable for this strategy.