Package groovy.transform
Annotation Type ToString
-
@Documented @Retention(RUNTIME) @Target(TYPE) public @interface ToStringClass annotation used to assist in the creation oftoString()methods in classes. The@ToStringannotation instructs the compiler to execute an AST transformation which adds the necessary toString() method. It allows you to write classes in this shortened form:
Which will have this output:@ToStringclass Customer { String first, last int age Date since = new Date() Collection favItems private answer = 42 } println new Customer(first:'Tom', last:'Jones', age:21, favItems:['Books', 'Games'])Customer(Tom, Jones, 21, Wed Jul 14 23:57:14 EST 2010, [Books, Games])
There are numerous options to customize the format of the generated output. E.g. if you change the first annotation to:
Then the output will be:@ToString(includeNames=true)Customer(first:Tom, last:Jones, age:21, since:Wed Jul 14 23:57:50 EST 2010, favItems:[Books, Games])
Or if you change the first annotation to:
Then the output will be:@ToString(includeNames=true,includeFields=true,excludes="since,favItems")Customer(first:Tom, last:Jones, age:21, answer:42)
If you have this example:import groovy.transform.ToString
Then the output will be:@ToStringclass NamedThing { String name }@ToString(includeNames=true,includeSuper=true) class AgedThing extends NamedThing { int age } println new AgedThing(name:'Lassie', age:5)AgedThing(age:5, super:NamedThing(Lassie))
@ToStringcan also be used in conjunction with@Canonicaland@Immutable.
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description java.lang.StringexcludesComma separated list of field and/or property names to exclude from generated toString.booleanincludeFieldsInclude fields as well as properties in generated toStringbooleanincludeNamesWhether to include names of properties/fields in generated toStringjava.lang.StringincludesComma separated list of field and/or property names to include within the generated toString.booleanincludeSuperWhether to include super in generated toString
-