Package groovy.transform
Annotation Type RecordOptions
Class annotation used to assist in the creation of record-like classes.
- Since:
- 4.0.0
- See Also:
-
Optional Element Summary
Modifier and TypeOptional ElementDescriptionboolean
Iftrue
, this adds a methodcomponents()
to the record which returns its components as a typed tupleTuple0
,Tuple1
...boolean
Iftrue
, this adds a methodcopyWith
which takes a Map of new property values and returns a new instance of the record class with these values set.boolean
Iftrue
, this adds a methodgetAt(int)
which given an integer n, returns the n'th component in the record.Mode to use when creating record type classes.boolean
Iftrue
, this adds a methodsize()
to the record which returns the number of components.boolean
Iftrue
, this adds a methodtoList()
to the record which returns the record's components as a list.boolean
Iftrue
, this adds a methodtoMap()
to the record.
-
Element Details
-
mode
RecordTypeMode modeMode to use when creating record type classes.- Default:
- AUTO
-
getAt
boolean getAtIftrue
, this adds a methodgetAt(int)
which given an integer n, returns the n'th component in the record. Example:import static groovy.test.GroovyAssert.shouldFail record Point(int x, int y, String color) {} def p = new Point(100, 200, 'green') assert p[0] == 100 assert p[1] == 200 assert p[2] == 'green' shouldFail(IllegalArgumentException) { p[-1] } // getAt also enables destructuring def (x, y, c) = p assert x == 100 assert y == 200 assert c == 'green'
If a methodgetAt(int)
already exists in the class, then this setting is ignored, and no additional method is generated.- Default:
- true
-
toList
boolean toListIftrue
, this adds a methodtoList()
to the record which returns the record's components as a list. Example:record Point(int x, int y, String color) {} def p = new Point(100, 200, 'green') assert p.toList() == [100, 200, 'green']
If a methodtoList()
already exists in the class, then this setting is ignored, and no additional method is generated.- Default:
- true
-
toMap
boolean toMapIftrue
, this adds a methodtoMap()
to the record. Example:record Point(int x, int y, String color) {} def p = new Point(100, 200, 'green') assert p.toMap() == [x:100, y:200, color:'green']
If a methodtoMap()
already exists in the class, then this setting is ignored, and no additional method is generated.- Default:
- true
-
size
boolean sizeIftrue
, this adds a methodsize()
to the record which returns the number of components. Example:record Point(int x, int y, String color) {} def p = new Point(100, 200, 'green') assert p.size() == 3
If a methodsize()
already exists in the class, then this setting is ignored, and no additional method is generated.- Default:
- true
-
copyWith
boolean copyWithIftrue
, this adds a methodcopyWith
which takes a Map of new property values and returns a new instance of the record class with these values set. Example:@groovy.transform.RecordType
(copyWith = true) class Person { String first, last } def tim = new Person('tim', 'yates') def alice = tim.copyWith(first:'alice') assert tim.toString() == 'Person[first=tim, last=yates]' assert alice.toString() == 'Person[first=alice, last=yates]'copyWith
that takes a single parameter already exists in the class, then this setting is ignored, and no method is generated.- Default:
- false
-
components
boolean componentsIftrue
, this adds a methodcomponents()
to the record which returns its components as a typed tupleTuple0
,Tuple1
... Example:import groovy.transform.*
The signature of the components method for this example is:@RecordOptions(components=true)
record Point(int x, int y, String color) {} def (x, y, c) = new Point(100, 200, 'green').components() assert x == 100 assert y.intdiv(2) == 100 assert c.toUpperCase() == 'GREEN'Tuple3<Integer, Integer, String> components()
This is suitable for destructuring inTypeChecked
scenarios. If a methodcomponents()
already exists in the class, then this setting is ignored, and no additional method is generated. It is a compile-time error if there are more components in the record than the largest TupleN class in the Groovy codebase.- Default:
- false
-