Top Description Methods
groovy.transform

public @Interface MapConstructor

extends Annotation
Annotations
@Documented
@Retention:SOURCE
@Target:TYPE
@GroovyASTTransformationClass:MapConstructorASTTransformation
Imports
org.codehaus.groovy.transform.GroovyASTTransformationClass, java.lang.annotation.ElementType, .Retention, .RetentionPolicy, .Target

Class annotation used to assist in the creation of map constructors in classes. If the class is also annotated with @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.*

@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)'
The @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:

Custom property handling:

Known limitations/special cases:

Since
2.5.0
See Also
PropertyOptions, VisibilityOptions

Method Summary

Modifier and TypeMethod and Description
public boolean
allNames()

Whether to include all fields and/or properties within the constructor, including those with names that are considered internal.

public boolean
allProperties()

Whether to include all properties (as per the JavaBean spec) in the generated constructor.

public String[]
excludes()

List of field and/or property names to exclude from the constructor.

public boolean
includeFields()

Include fields in the constructor.

public boolean
includeProperties()

Include properties in the constructor.

public String[]
includes()

List of field and/or property names to include within the constructor.

public boolean
includeStatic()

Whether to include static properties in the constructor.

public boolean
includeSuperFields()

Include fields from super classes in the constructor.

public boolean
includeSuperProperties()

Include properties from super classes in the constructor.

public boolean
noArg()

In addition to the map constructor, provide a no-arg constructor which calls the map constructor with an empty map.

public Class<T>
post()

A Closure containing statements which will be appended to the end of the generated constructor.

public Class<T>
pre()

A Closure containing statements which will be prepended to the generated constructor.

public 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.

public boolean
useSetters()

By default, Groovy properties are set directly using their respective field.

public String
visibilityId()

If specified, must match the "id" attribute in a VisibilityOptions annotation to enable a custom visibility.

Inherited from java.lang.annotation.Annotation:
annotationTypeequalshashCodetoString

Method Detail

allNamesback to summary
public boolean allNames()

Whether to include all fields and/or properties within the constructor, including those with names that are considered internal.

allPropertiesback to summary
public boolean allProperties()

Whether to include all properties (as per the JavaBean spec) in the generated constructor. When true, Groovy treats any explicitly created setXxx() methods as property setters as per the JavaBean specification. JavaBean properties come after any Groovy properties but before any fields for a given class (unless 'includes' is used to determine the order).

excludesback to summary
public String[] excludes()

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.

includeFieldsback to summary
public boolean includeFields()

Include fields in the constructor. Fields come after any properties.

includePropertiesback to summary
public boolean includeProperties()

Include properties in the constructor.

includesback to summary
public String[] includes()

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.

includeStaticback to summary
public boolean includeStatic()

Whether to include static properties in the constructor.

includeSuperFieldsback to summary
public boolean includeSuperFields()

Include fields from super classes in the constructor. Groovy properties, JavaBean properties and fields (in that order) from superclasses come before the members from a subclass (unless 'includes' is used to determine the order).

includeSuperPropertiesback to summary
public boolean includeSuperProperties()

Include properties from super classes in the constructor. Groovy properties, JavaBean properties and fields (in that order) from superclasses come before the members from a subclass (unless 'includes' is used to determine the order).

noArgback to summary
public boolean noArg()

In addition to the map constructor, provide a no-arg constructor which calls the map constructor with an empty map.

postback to summary
public Class<T> post()

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.

preback to summary
public Class<T> pre()

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.

specialNamedArgHandlingback to summary
public 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. This allows both a map and a tuple constructor to be used side-by-side so long as care is taken about the types used when calling.

useSettersback to summary
public boolean useSetters()

By default, Groovy 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.

visibilityIdback to summary
public String visibilityId()

If specified, must match the "id" attribute in a VisibilityOptions annotation to enable a custom visibility.