@Documented @Retention(value=SOURCE) @Target(value=TYPE) public @interface TupleConstructor
@ImmutableBase
, then the generated
constructor will contain additional code needed for immutable classes.
Should be used with care with other annotations which create constructors - see "Known
Limitations" for more details.
It allows you to write classes in this shortened form:
@groovy.transform.TupleConstructor
class Customer {
String first, last
int age
Date since
Collection favItems
}
def c1 = new Customer(first:'Tom', last:'Jones', age:21, since:new Date(), favItems:['Books', 'Games'])
def c2 = new Customer('Tom', 'Jones', 21, new Date(), ['Books', 'Games'])
def c3 = new Customer('Tom', 'Jones')
The @TupleConstructor
annotation instructs the compiler to execute an
AST transformation which adds the necessary constructor method to your class.
A tuple constructor is created with a parameter for each property (and optionally field and super properties). The default order is properties, pseudo/JavaBean properties and then fields for parent classes first (if includeSuperXxx annotation attributes are used). A default value is provided (using Java's default values) for all parameters in the constructor. Groovy's normal conventions then allows any number of parameters to be left off the end of the parameter list including all of the parameters - giving a no-arg constructor which can be used with the map-style naming conventions.
The order of parameters is given by the properties of any super classes (if includeSuperProperties
is set)
with the most super first followed by the properties of the class followed
by the fields of the class (if includeFields
is set). Within each grouping the order
is as attributes appear within the respective class.
More examples:
//-------------------------------------------------------------------------- import groovy.transform.TupleConstructor @TupleConstructor() class Person { String name List likes private boolean active = false } def person = new Person('mrhaki', ['Groovy', 'Java']) assert person.name == 'mrhaki' assert person.likes == ['Groovy', 'Java'] person = new Person('mrhaki') assert person.name == 'mrhaki' assert !person.likes
//-------------------------------------------------------------------------- // includeFields in the constructor creation. import groovy.transform.TupleConstructor @TupleConstructor(includeFields=true) class Person { String name List likes private boolean active = false boolean isActivated() { active } } def person = new Person('mrhaki', ['Groovy', 'Java'], true) assert person.name == 'mrhaki' assert person.likes == ['Groovy', 'Java'] assert person.activated
//-------------------------------------------------------------------------- // use force attribute to force creation of constructor // even if we define our own constructors. import groovy.transform.TupleConstructor @TupleConstructor(force=true) class Person { String name List likes private boolean active = false Person(boolean active) { this.active = active } boolean isActivated() { active } } def person = new Person('mrhaki', ['Groovy', 'Java']) assert person.name == 'mrhaki' assert person.likes == ['Groovy', 'Java'] assert !person.activated person = new Person(true) assert person.activated
//-------------------------------------------------------------------------- // include properties and fields from super class. import groovy.transform.TupleConstructor @TupleConstructor(includeFields=true) class Person { String name List likes private boolean active = false boolean isActivated() { active } } @TupleConstructor(callSuper=true, includeSuperProperties=true, includeSuperFields=true) class Student extends Person { List courses } def student = new Student('mrhaki', ['Groovy', 'Java'], true, ['IT']) assert student.name == 'mrhaki' assert student.likes == ['Groovy', 'Java'] assert student.activated assert student.courses == ['IT']
Custom visibility:
@TupleConstructor
annotation generates a public constructor unless an applicable
VisibilityOptions
annotation is also present. It can be useful to change the visibility
if you want to also create a builder or provide your own static factory method for object creation.
You can make the constructor private and access it from the builder or your factory method. (Note:
you'll probably want to use @CompileStatic
in conjunction with such an approach since
dynamic Groovy currently gives the ability to access even private constructors.)visibilityId
attribute can be specified. If present, it must match the optional
id
attribute of an applicable VisibilityOptions
annotation. This can be useful
if multiple VisibilityOptions
annotations are needed.Custom property handling:
@TupleConstructor
annotation supports customization using @PropertyOptions
which allows a custom property handler to be defined. This is most typically used behind the scenes
by the @Immutable
meta-annotation but you can also define your own handler. If a custom
handler is present, it will determine the code generated when initializing any property (or field).Named-argument support:
@TupleConstructor
produces.defaults
annotation attribute is set to false
,
and no other map-based constructor are added then named-argument processing will not be available.LinkedHashMap
constructor will be created
in addition to the tuple constructor to support named parameters in the normal way. This won't be created
if the class is already annotated with @MapConstructor
or if the defaults
annotation attribute is set to false
.LinkedHashMap
or if there is
a single Object, AbstractMap, Map or HashMap property (or field), then no additional constructor
will be added and Groovy's normal map-style naming conventions will not be available.Known limitations/special cases:
@InheritConstructors
);
the order in which the particular transforms are processed becomes important in that case.
See the force
attribute for further details about customizing this behavior.@InheritConstructors
);
the order in which the particular transforms are processed becomes important in that case.
See the defaults
attribute for further details about customizing this behavior.PropertyOptions
,
VisibilityOptions
Modifier and Type | Optional Element and Description |
---|---|
boolean |
allNames
Whether to include all fields and/or properties within the constructor, including those with names that are
considered internal.
|
boolean |
allProperties
Whether to include all properties (as per the JavaBean spec) in the generated constructor.
|
boolean |
callSuper
Should super properties be called within a call to the parent constructor
rather than set as properties.
|
boolean |
defaults
Used to set whether default value processing is enabled (the default) or disabled.
|
String[] |
excludes
List of field and/or property names to exclude from the constructor.
|
boolean |
force
By default, this annotation becomes a no-op if you provide your own constructor.
|
boolean |
includeFields
Include fields in the constructor.
|
boolean |
includeProperties
Include properties in the constructor.
|
String[] |
includes
List of field and/or property names to include within the constructor.
|
boolean |
includeSuperFields
Include visible fields from super classes in the constructor.
|
boolean |
includeSuperProperties
Include properties from super classes in the constructor.
|
Class |
post
A Closure containing statements which will be appended to the end of the generated constructor.
|
Class |
pre
A Closure containing statements which will be prepended to the generated constructor.
|
boolean |
useSetters
By default, properties are set directly using their respective field.
|
String |
visibilityId
If specified, must match the "id" attribute in a VisibilityOptions annotation to enable a custom visibility.
|
public abstract String[] excludes
public abstract String[] includes
public abstract boolean includeProperties
public abstract boolean includeFields
public abstract boolean includeSuperProperties
public abstract boolean includeSuperFields
public abstract boolean callSuper
includeSuperProperties
.
Can't be true if using pre
with a super
first statement.public abstract boolean force
force=true
then the tuple constructor(s) will be added regardless of
whether existing constructors exist. It is up to you to avoid creating duplicate constructors.public abstract boolean defaults
public abstract boolean useSetters
useSetters=true
then a writable property will be set using its setter.
If turning on this flag we recommend that setters that might be called are
made null-safe wrt the parameter.public abstract boolean allNames
public abstract boolean allProperties
public abstract String visibilityId
public abstract Class pre
super(someArgs)
in which case the no-arg super constructor won't be called.public abstract Class post