Builder AST transform to create a builder helper class
for the fluent and type-safe creation of instances of a specified class.
It is modelled roughly on the design outlined here:
http://michid.wordpress.com/2008/08/13/type-safe-builder-pattern-in-java/
You define classes which use the type-safe initializer pattern as follows:
import groovy.transform.builder.* import groovy.transform.*While it isn't required to do so, the benefit of this builder strategy comes in conjunction with static type-checking or static compilation. Typical usage is as follows:@ToString@Builder(builderStrategy=InitializerStrategy) class Person { String firstName String lastName int age }
@CompileStatic
def main() {
println new Person(Person.createInitializer().firstName("John").lastName("Smith").age(21))
}
which prints:
Person(John, Smith, 21)If you don't initialise some of the properties, your code won't compile, e.g. if the method body above was changed to this:
println new Person(Person.createInitializer().firstName("John").lastName("Smith"))
then the following compile-time error would result:
[Static type checking] - Cannot find matching method Person#<init>(Person$PersonInitializer <groovy.transform.builder.InitializerStrategy$SET, groovy.transform.builder.InitializerStrategy$SET, groovy.transform.builder.InitializerStrategy$UNSET>). Please check if the declared type is correct and if the method exists.
The message is a little cryptic, but it is basically the static compiler telling us that the third parameter, age in our case, is unset.
You can also add this annotation to your predefined constructors. These will be made private and an initializer will be set up
to call your constructor. Any parameters to your constructor become the properties expected by the initializer.
If you use such a builder on a constructor as well as on the class or on more than one constructor, then it is up to you
to define unique values for 'builderClassName' and 'builderMethodName' for each annotation.
| Modifier and Type | Class and Description |
|---|---|
| public abstract static class | InitializerStrategy.SET
Internal phantom type used by the |
| public abstract static class | InitializerStrategy.UNSET
Internal phantom type used by the |
| Modifier and Type | Field and Description |
|---|---|
| private static final Expression | |
| private static final ClassNode |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| private static void | |
| public void | build(BuilderASTTransformation transform, AnnotatedNode annotatedNode, AnnotationNode anno)
Implements org. |
| private void | |
| private static List | |
| private static void | createBuildeeConstructors(BuilderASTTransformation transform, ClassNode buildee, ClassNode builder, List<FieldNode> fields, boolean needsConstructor, boolean useSetters)
|
| private static void | createBuildeeMethods(ClassNode buildee, MethodNode mNode, ClassNode builder, List<FieldNode> fields)
|
| private static void | |
| private void | createBuilderForAnnotatedClass(BuilderASTTransformation transform, ClassNode buildee, AnnotationNode anno, boolean useSetters, boolean allNames, boolean force)
|
| private void | createBuilderForAnnotatedMethod(BuilderASTTransformation transform, MethodNode mNode, AnnotationNode anno, boolean useSetters)
|
| private static MethodNode | createBuilderMethod(String buildMethodName, ClassNode builder, int numFields, String builderMethodName)
|
| private MethodNode | |
| private static MethodNode | |
| private static FieldNode | |
| private static ConstructorNode | |
| private static ClassNode | |
| private static List | |
| private static String | |
| private static Parameter[] | |
| private static void | |
| private static GenericsType | |
| private static void | |
| private static GenericsType[] | |
| private static GenericsType[] |
| DEFAULT_INITIAL_VALUE | back to summary |
|---|---|
| private static final Expression DEFAULT_INITIAL_VALUE | |
| TUPLECONS_TYPE | back to summary |
|---|---|
| private static final ClassNode TUPLECONS_TYPE | |
| InitializerStrategy | back to summary |
|---|---|
| public InitializerStrategy() | |
| addFields | back to summary |
|---|---|
| private static void addFields(ClassNode buildee, List<FieldNode> filteredFields, ClassNode builder) | |
| build | back to summary |
|---|---|
| public void build(BuilderASTTransformation transform, AnnotatedNode annotatedNode, AnnotationNode anno) Implements org. | |
| buildCommon | back to summary |
|---|---|
| private void buildCommon(ClassNode buildee, AnnotationNode anno, List<FieldNode> fieldNodes, ClassNode builder) | |
| convertParamsToFields | back to summary |
|---|---|
| private static List | |
| createBuildeeConstructors | back to summary |
|---|---|
| private static void createBuildeeConstructors(BuilderASTTransformation transform, ClassNode buildee, ClassNode builder, List<FieldNode> fields, boolean needsConstructor, boolean useSetters) | |
| createBuildeeMethods | back to summary |
|---|---|
| private static void createBuildeeMethods(ClassNode buildee, MethodNode mNode, ClassNode builder, List<FieldNode> fields) | |
| createBuilderConstructors | back to summary |
|---|---|
| private static void createBuilderConstructors(ClassNode builder, ClassNode buildee, List<FieldNode> fields) | |
| createBuilderForAnnotatedClass | back to summary |
|---|---|
| private void createBuilderForAnnotatedClass(BuilderASTTransformation transform, ClassNode buildee, AnnotationNode anno, boolean useSetters, boolean allNames, boolean force) | |
| createBuilderForAnnotatedMethod | back to summary |
|---|---|
| private void createBuilderForAnnotatedMethod(BuilderASTTransformation transform, MethodNode mNode, AnnotationNode anno, boolean useSetters) | |
| createBuilderMethod | back to summary |
|---|---|
| private static MethodNode createBuilderMethod(String buildMethodName, ClassNode builder, int numFields, String builderMethodName) | |
| createBuilderMethodForField | back to summary |
|---|---|
| private MethodNode createBuilderMethodForField(ClassNode builder, List<FieldNode> fields, String prefix, int fieldPos) | |
| createBuildMethod | back to summary |
|---|---|
| private static MethodNode createBuildMethod(ClassNode builder, String buildMethodName, List<FieldNode> fields) | |
| createFieldCopy | back to summary |
|---|---|
| private static FieldNode createFieldCopy(ClassNode buildee, FieldNode fNode) | |
| createInitializerConstructor | back to summary |
|---|---|
| private static ConstructorNode createInitializerConstructor(ClassNode buildee, ClassNode builder, List<FieldNode> fields) | |
| createInnerHelperClass | back to summary |
|---|---|
| private static ClassNode createInnerHelperClass(ClassNode buildee, String builderClassName, int fieldsSize) | |
| filterFields | back to summary |
|---|---|
| private static List | |
| getBuilderClassName | back to summary |
|---|---|
| private static String getBuilderClassName(ClassNode buildee, AnnotationNode anno) | |
| getParams | back to summary |
|---|---|
| private static Parameter[] getParams(List<FieldNode> fields, ClassNode cNode) | |
| initializeFields | back to summary |
|---|---|
| private static void initializeFields(List<FieldNode> fields, BlockStatement body, boolean useSetters) | |
| makePlaceholder | back to summary |
|---|---|
| private static GenericsType makePlaceholder(int i) | |
| renameMethod | back to summary |
|---|---|
| private static void renameMethod(ClassNode buildee, MethodNode mNode, String newName) | |
| setGenTypes | back to summary |
|---|---|
| private static GenericsType[] setGenTypes(int numFields) | |
| unsetGenTypes | back to summary |
|---|---|
| private static GenericsType[] unsetGenTypes(int numFields) | |
InitializerStrategy to indicate that a property has been set. It is used in conjunction with the generated parameterized type helper class.
| Access | Constructor and Description |
|---|---|
| public |
| SET | back to summary |
|---|---|
| public SET() | |
InitializerStrategy to indicate that a property remains unset. It is used in conjunction with the generated parameterized type helper class.
| Access | Constructor and Description |
|---|---|
| public |
| UNSET | back to summary |
|---|---|
| public UNSET() | |