Annotation Interface Field


Controls specific behaviour for certain fields.

This can be used to explicitly name the members of a collection. By default, the member name of collection is the name of the collection minus a trailing 's', i.e. environments :: environment. The member name is used as name for the generation of adder methods.

Using @Field, this can be explicitly overridden, for example for values with different plural rules. For example, the field libraries would by default contain the wrong elements name librarie, which could be changed:

@Field(member = 'library') Set<String> libraries

Note that the member names must be unique across all collections of a DSL hierarchy.

In addition to fields, setter like methods (i.e. methods with a single parameter) can also be annotated with @Field, making them 'virtual fields'. For virtual fields, the same dsl methods are generated as for actual fields. The name of the methods is the same as the method name. The annotated method is automatically converted into a Mutator method.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    Marker interface used to designate a field to use the field name of the owner as key.
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Maps names to classes.
    Create converter methods for this field.
    Allows to set the base type for the given field.
    Allows to set the key for a single Keyed Object field to a value defined by the owner.
    Closure that is used to derive the key from the value.
    Name of the inner methods for collections.
     
  • Element Details

    • value

      FieldType value
      Default:
      DEFAULT
    • members

      String members
      Name of the inner methods for collections. If empty (default), use field name stripped of a trailing 's' (i.e. if the field is called environments, the elements are called environment by default. If the field name does not end with an 's', the field name is used as is.
      Default:
      ""
    • alternatives

      Class<?> alternatives
      Maps names to classes. If used, this value must be a closure which contains only a map with literal String to Class mappings, i.e. @DSL(alternatives = {child: ChildElement, sub: SubElement}).
      Default:
      groovy.transform.Undefined.class
    • keyMapping

      Class<?> keyMapping
      Closure that is used to derive the key from the value. This is only valid for Map types. The closure gets a single parameter of the value type and must return a value of the key type.
      Default:
      groovy.transform.Undefined.class
    • key

      Class<?> key
      Allows to set the key for a single Keyed Object field to a value defined by the owner. This member can contain either Closure executed against the owner object or the special entry Field.FieldName, which takes the name of the field.
      Default:
      groovy.transform.Undefined.class
    • converters

      Class[] converters

      Create converter methods for this field. Converter methods have the same name as regular setter / adders, but different parameters. A converter is a closure with zero or more explicit parameters that is called to create the target type (or the element type for collections / maps). Note that for maps of simple types, a key parameter is added to the adder as well.

      Example:

      
       @DSL class Foo {
         @Field(converters = [
           {long value -> new Date(value)},
           {int date, int month, int year -> new Date(year, month, date)}
         ])
         Date birthday
       }
       

      Creates additional methods:

      
       Date birthday(long value)
       Date birthday(int date, int month, year)
       

      The closures must return an instance of the field (or element) type.

      Example:

      
       @DSL class Foo {
         @Field(annotations = [])
         Date birthday
       }
       
      Default:
      {}
    • defaultImpl

      Class<?> defaultImpl
      Allows to set the base type for the given field. DSL methods will be generated for the base type instead of the actual type. This is useful for interfaces or abstract classes.
      Default:
      groovy.transform.Undefined.class