@Documented
@Retention(value=SOURCE)
@Target(value=TYPE)
public @interface MapConstructor
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() }
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.
|
java.lang.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.
|
java.lang.String[] |
includes
List of field and/or property names to include within the constructor.
|
boolean |
includeSuperProperties
Include properties from super classes in the constructor.
|
java.lang.Class |
post
A Closure containing statements which will be appended to the end of the generated constructor.
|
java.lang.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.
|
public abstract java.lang.String[] excludes
public abstract java.lang.String[] includes
public abstract boolean includeFields
public abstract boolean includeProperties
public abstract boolean includeSuperProperties
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 java.lang.Class pre