Top Description Fields Constructors Methods
groovy.transform.builder

public Class DefaultStrategy

extends AbstractBuilderStrategy
Class Inheritance
Imports
groovy.transform.Undefined, org.codehaus.groovy.ast.AnnotatedNode, .AnnotationNode, .ClassNode, .ConstructorNode, .FieldNode, .InnerClassNode, .MethodNode, .Parameter, org.codehaus.groovy.ast.expr.Expression, org.codehaus.groovy.ast.stmt.BlockStatement, org.codehaus.groovy.transform.BuilderASTTransformation, java.util.ArrayList, .List, .Map

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).build()
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:
@groovy.transform.builder.Builder(prefix='set')
class Person {
    String firstName
    String lastName
    int age
}
def p2 = Person.builder().setFirstName("Robert").setLastName("Lewandowski").setAge(21).build()
or using a prefix of 'with' would result in usage like this:
def p3 = Person.builder().withFirstName("Robert").withLastName("Lewandowski").withAge(21).build()
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. The 'useSetters' annotation attribute for the @Builder transform is ignored by this strategy which always uses setters.

Field Summary

Modifier and TypeField and Description
private static final Expression
private static final int

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
public void
public void
private static ClassNode
private static void
private static MethodNode
private MethodNode
private static MethodNode
private static MethodNode
private static FieldNode
private static FieldNode
createFieldCopy(ClassNode buildee, String fieldName, ClassNode fieldType)

private static ClassNode
getCorrectedType(ClassNode buildee, ClassNode fieldType, ClassNode declaringClass)

private static String
private static String
private static Expression
Inherited from org.codehaus.groovy.transform.BuilderASTTransformation.AbstractBuilderStrategy:
checkKnownFieldcheckKnownPropertygetFieldsgetIncludeExcludegetPropertyInfoFromBeanInfogetPropertyInfoFromClassNodegetPropertyInfoFromClassNodegetPropertyInfoFromClassNodegetPropertyInfosgetSetterNameunsupportedAttributeunsupportedAttribute

Field Detail

DEFAULT_INITIAL_VALUEback to summary
private static final Expression DEFAULT_INITIAL_VALUE
PUBLIC_STATICback to summary
private static final int PUBLIC_STATIC

Constructor Detail

DefaultStrategyback to summary
public DefaultStrategy()

Method Detail

buildback to summary
public void build(BuilderASTTransformation transform, AnnotatedNode annotatedNode, AnnotationNode anno)

Implements org.codehaus.groovy.transform.BuilderASTTransformation.BuilderStrategy.build.

buildClassback to summary
public void buildClass(BuilderASTTransformation transform, ClassNode buildee, AnnotationNode anno)
buildMethodback to summary
public void buildMethod(BuilderASTTransformation transform, MethodNode mNode, AnnotationNode anno)
createBuilderback to summary
private static ClassNode createBuilder(AnnotationNode anno, ClassNode buildee)
createBuilderFactoryMethodback to summary
private static void createBuilderFactoryMethod(AnnotationNode anno, ClassNode buildee, ClassNode builder)
createBuilderMethodback to summary
private static MethodNode createBuilderMethod(AnnotationNode anno, ClassNode builder)
createBuilderMethodForPropback to summary
private MethodNode createBuilderMethodForProp(ClassNode builder, BuilderASTTransformation.AbstractBuilderStrategy.PropertyInfo pinfo, String prefix)
createBuildMethodback to summary
private static MethodNode createBuildMethod(AnnotationNode anno, ClassNode buildee, List<BuilderASTTransformation.AbstractBuilderStrategy.PropertyInfo> props)
createBuildMethodForMethodback to summary
private static MethodNode createBuildMethodForMethod(AnnotationNode anno, ClassNode buildee, MethodNode mNode, Parameter[] params)
createFieldCopyback to summary
private static FieldNode createFieldCopy(ClassNode buildee, Parameter param)
createFieldCopyback to summary
private static FieldNode createFieldCopy(ClassNode buildee, String fieldName, ClassNode fieldType)
getCorrectedTypeback to summary
private static ClassNode getCorrectedType(ClassNode buildee, ClassNode fieldType, ClassNode declaringClass)
getFullNameback to summary
private static String getFullName(AnnotationNode anno, ClassNode buildee)
getPrefixback to summary
private static String getPrefix(AnnotationNode anno)
initializeInstanceback to summary
private static Expression initializeInstance(ClassNode buildee, List<BuilderASTTransformation.AbstractBuilderStrategy.PropertyInfo> props, BlockStatement body)