@Documented @Retention(value=SOURCE) @Target(value=TYPE) public @interface MapConstructor
@ImmutableBase
, then the generated
constructor will contain additional code needed for immutable 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() }
Custom visibility:
@MapConstructor
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:
@MapConstructor
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).Known limitations/special cases:
Map
unless a single property (or field)
is included and the type of that property (or field) is Object, AbstractMap, Map or HashMap.
In this case, the generated constructor will be of type LinkedHashMap
.
This allows the possibility of also adding a tuple constructor without conflict, although
no such constructor is added automatically. You can disable this behavior by setting
the specialNamedArgHandling annotation attribute to false. This means that for the special
case mentioned above, you will not be able to also add a tuple constructor with a single Map
argument but you can supply any kind of map as your argument. We'd also recommend not
having both a map constructor and a tuple constructor with a single Object, AbstractMap or
HashMap since it can cause confusion as to which will be called.
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.
|
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.
|
String[] |
includes
List of field and/or property names to include within the constructor.
|
boolean |
includeStatic
Whether to include static properties in the constructor.
|
boolean |
includeSuperFields
Include fields from super classes in the constructor.
|
boolean |
includeSuperProperties
Include properties from super classes in the constructor.
|
boolean |
noArg
In addition to the map constructor, provide a no-arg constructor which calls the map constructor with an empty map.
|
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 |
specialNamedArgHandling
If true, change the type of the map constructor argument from Map to LinkedHashMap only for the case where
the class has a single property (or field) with a Map-like type.
|
boolean |
useSetters
By default, Groovy 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 allProperties
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 includeStatic
public abstract boolean allNames
public abstract boolean noArg
public abstract boolean specialNamedArgHandling
public abstract String visibilityId
public abstract Class pre
public abstract Class post