Annotation Type TupleConstructor
InheritConstructors
.
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). 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 with most super first
(if includeSuperProperties
is set) 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']
Limitations:
- Groovy's normal map-style naming conventions will not be available if the first property (or field)
has type
LinkedHashMap
or if there is a single Map, AbstractMap or HashMap property (or field)
- Since:
- 1.8.0
- Author:
- Paul King
-
Optional Element Summary
Modifier and TypeOptional ElementDescriptionboolean
Should super properties be called within a call to the parent constructor.String[]
List of field and/or property names to exclude from the constructor.boolean
By default, this annotation becomes a no-op if you provide your own constructor.boolean
Include fields in the constructor.boolean
Include properties in the constructor.String[]
List of field and/or property names to include within the constructor.boolean
Include fields from super classes in the constructor.boolean
Include properties from super classes in the constructor.
-
Element Details
-
excludes
String[] excludesList of field and/or property names to exclude from the constructor. Must not be used if 'includes' is used. For convenience, a String with comma separated names can be used in addition to an array (using Groovy's literal list notation) of String values.- Default:
- {}
-
includes
String[] includesList of field and/or property names to include within the constructor. Must not be used if 'excludes' is used. For convenience, a String with comma separated names can be used in addition to an array (using Groovy's literal list notation) of String values.- Default:
- {}
-
includeFields
boolean includeFieldsInclude fields in the constructor.- Default:
- false
-
includeProperties
boolean includePropertiesInclude properties in the constructor.- Default:
- true
-
includeSuperFields
boolean includeSuperFieldsInclude fields from super classes in the constructor.- Default:
- false
-
includeSuperProperties
boolean includeSuperPropertiesInclude properties from super classes in the constructor.- Default:
- false
-
callSuper
boolean callSuperShould super properties be called within a call to the parent constructor. rather than set as properties- Default:
- false
-
force
boolean forceBy default, this annotation becomes a no-op if you provide your own constructor. By settingforce=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.- Default:
- false
-