@Documented @Retention(value=RUNTIME) @Target(value=TYPE) public @interface ToString
toString()
methods in classes.
The @ToString
annotation instructs the compiler to execute an
AST transformation which adds the necessary toString() method.
It allows you to write classes in this shortened form:
@ToString
class 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'])
Which will have this output:
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:
@ToString(includeNames=true)
Then the output will be:
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:
@ToString(includeNames=true,includeFields=true,excludes="since,favItems")
Then the output will be:
Customer(first:Tom, last:Jones, age:21, answer:42)If you have this example:
import groovy.transform.ToStringThen the output will be:@ToString
class 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))
@ToString
can also be used in conjunction with @Canonical
and @Immutable
.
If you want to omit fields or properties referring to null, you can use the ignoreNulls flag:
import groovy.transform.ToString
@ToString(ignoreNulls = true)
class NamedThing {
String name
}
println new NamedThing(name: null)
Which results in:
NamedThing()
By default the fully-qualified class name is used as part of the generated toString. If you want to exclude the package, you can set the includePackage flag to false, e.g.:
package my.company
import groovy.transform.ToString
@ToString(includePackage = false)
class NamedThing {
String name
}
println new NamedThing(name: "Lassie")
Which results in:
NamedThing(name: Lassie)If the includePackage flag is
true
(the default), then the output will be:
my.company.NamedThing(name: Lassie)
Modifier and Type | Optional Element and Description |
---|---|
boolean |
cache
Whether to cache toString() calculations.
|
String[] |
excludes
List of field and/or property names to exclude from generated toString.
|
boolean |
ignoreNulls
Don't display any fields or properties with value null.
|
boolean |
includeFields
Include fields as well as properties in generated toString.
|
boolean |
includeNames
Whether to include names of properties/fields in generated toString.
|
boolean |
includePackage
Whether to include the fully-qualified class name (i.e. including
the package) or just the simple class name in the generated toString.
|
String[] |
includes
List of field and/or property names to include within the generated toString.
|
boolean |
includeSuper
Whether to include super in generated toString.
|
public abstract String[] excludes
public abstract String[] includes
public abstract boolean includeSuper
public abstract boolean includeNames
public abstract boolean includeFields
public abstract boolean ignoreNulls
public abstract boolean includePackage
public abstract boolean cache