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.
| Modifier and Type | Field and Description |
|---|---|
| private static final Expression |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | build(BuilderASTTransformation transform, AnnotatedNode annotatedNode, AnnotationNode anno)
Implements org. |
| private MethodNode | createBuilderMethodForField(ClassNode builderClass, BuilderASTTransformation.
|
| private static MethodNode | createBuildMethod(BuilderASTTransformation transform, AnnotationNode anno, ClassNode sourceClass, List<BuilderASTTransformation.
|
| private static FieldNode | createFieldCopy(ClassNode builderClass, BuilderASTTransformation.
|
| private static Expression | initializeInstance(ClassNode sourceClass, List<BuilderASTTransformation.
|
| DEFAULT_INITIAL_VALUE | back to summary |
|---|---|
| private static final Expression DEFAULT_INITIAL_VALUE | |
| ExternalStrategy | back to summary |
|---|---|
| public ExternalStrategy() | |
| build | back to summary |
|---|---|
| public void build(BuilderASTTransformation transform, AnnotatedNode annotatedNode, AnnotationNode anno) Implements org. | |
| createBuilderMethodForField | back to summary |
|---|---|
| private MethodNode createBuilderMethodForField(ClassNode builderClass, BuilderASTTransformation. | |
| createBuildMethod | back to summary |
|---|---|
| private static MethodNode createBuildMethod(BuilderASTTransformation transform, AnnotationNode anno, ClassNode sourceClass, List<BuilderASTTransformation. | |
| createFieldCopy | back to summary |
|---|---|
| private static FieldNode createFieldCopy(ClassNode builderClass, BuilderASTTransformation. | |
| initializeInstance | back to summary |
|---|---|
| private static Expression initializeInstance(ClassNode sourceClass, List<BuilderASTTransformation. | |