Package groovy.transform.builder
Class InitializerStrategy
- java.lang.Object
-
- org.codehaus.groovy.transform.BuilderASTTransformation.AbstractBuilderStrategy
-
- groovy.transform.builder.InitializerStrategy
-
- All Implemented Interfaces:
BuilderASTTransformation.BuilderStrategy
public class InitializerStrategy extends BuilderASTTransformation.AbstractBuilderStrategy
This strategy is used with theBuilderAST 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 }
which prints:@CompileStaticdef main() { println new Person(Person.createInitializer().firstName("John").lastName("Smith").age(21)) }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:
The message is a little cryptic, but it is basically the static compiler telling us that the third parameter,[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.agein 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classInitializerStrategy.SETInternal phantom type used by theInitializerStrategyto indicate that a property has been set.static classInitializerStrategy.UNSETInternal phantom type used by theInitializerStrategyto indicate that a property remains unset.-
Nested classes/interfaces inherited from class org.codehaus.groovy.transform.BuilderASTTransformation.AbstractBuilderStrategy
BuilderASTTransformation.AbstractBuilderStrategy.PropertyInfo
-
-
Constructor Summary
Constructors Constructor Description InitializerStrategy()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbuild(BuilderASTTransformation transform, AnnotatedNode annotatedNode, AnnotationNode anno)-
Methods inherited from class org.codehaus.groovy.transform.BuilderASTTransformation.AbstractBuilderStrategy
checkKnownField, checkKnownProperty, getFields, getIncludeExclude, getPropertyInfoFromBeanInfo, getPropertyInfoFromClassNode, getPropertyInfoFromClassNode, getPropertyInfoFromClassNode, getPropertyInfos, getSetterName, unsupportedAttribute, unsupportedAttribute
-
-
-
-
Method Detail
-
build
public void build(BuilderASTTransformation transform, AnnotatedNode annotatedNode, AnnotationNode anno)
-
-