Class annotation used to assist in the creation of map constructors in classes.
It allows you to write classes in this shortened form:
import groovy.transform.*The@TupleConstructor
class Person { String first, last }@CompileStatic
// optional@ToString(includeSuperProperties=true)
@MapConstructor
(pre={ super(args?.first, args?.last); args = args ?: [:] }, post = { first = first?.toUpperCase() }) class Author extends Person { String bookName } assert new Author(first: 'Dierk', last: 'Koenig', bookName: 'ReGinA').toString() == 'Author(ReGinA, DIERK, Koenig)' assert new Author().toString() == 'Author(null, null, null)'
@MapConstructor
annotation instructs the compiler to execute an
AST transformation which adds the necessary constructor method to your class.
A map constructor is created which sets properties, and optionally fields and super properties if the property/field name is a key within the map.
For the above example, the generated constructor will be something like:
public Author(java.util.Map args) { super(args?.first, args?.last) args = args ? args : [:] if (args.containsKey('bookName')) { this.bookName = args['bookName'] } first = first?.toUpperCase() }
Type | Name and Description |
---|---|
boolean |
allNames Whether to include all fields and/or properties within the constructor, including those with names that are considered internal. |
String[] |
excludes List of field and/or property names to exclude from the constructor. |
boolean |
includeFields Include fields in the constructor. |
boolean |
includeProperties Include properties in the constructor. |
boolean |
includeSuperProperties Include properties from super classes in the constructor. |
String[] |
includes List of field and/or property names to include within 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. |
Whether to include all fields and/or properties within the constructor, including those with names that are considered internal. @default false
List 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 {}
Include fields in the constructor. @default false
Include properties in the constructor. @default true
Include properties from super classes in the constructor. @default false
List 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. The default value is a special marker value indicating that no includes are defined; all fields and/or properties are included if 'includes' remains undefined and 'excludes' is explicitly or implicitly an empty list. @default {Undefined.STRING}
A Closure containing statements which will be appended to the end of the generated constructor. Useful for validation steps or tweaking the populated fields/properties. @default .CLASS.class
A Closure containing statements which will be prepended to the generated constructor. The first statement within the Closure may be "super(someArgs)" in which case the no-arg super constructor won't be called. @default .CLASS.class
By default, properties are set directly using their respective field.
By setting 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.
@default false