Groovy Documentation

## groovy.transform
[Java] Annotation Type ToString

    java.lang.Object
      groovy.transform.ToString

    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.TYPE})
    @GroovyASTTransformationClass("org.codehaus.groovy.transform.ToStringASTTransformation")
    public @interface ToString

Class annotation used to assist in the creation of `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.ToString
     @ToString class NamedThing {
         String name
     }
     @ToString(includeNames=true,includeSuper=true)
     class AgedThing extends NamedThing {
         int age
     }
     println new AgedThing(name:'Lassie', age:5)

Then the output will be:

     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()

Authors:
  Paul King
  Andre Steingress

See Also:
  Immutable
  Canonical

Since:
  1.8.0

Optional Element Summary
`java.lang.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` | `includeSuper` Whether to include super in generated toString.
`java.lang.String[]` | `includes` List of field and/or property names to include within the generated toString.

Method Summary

Methods inherited from class java.lang.Object
java.lang.Object#wait(long), java.lang.Object#wait(long, int), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll()

Element Detail

### excludes

    public java.lang.String[] excludes

  List of field and/or property names to exclude from generated toString. 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. @default {}

### ignoreNulls

    public boolean ignoreNulls

  Don't display any fields or properties with value `null`. @default false

### includeFields

    public boolean includeFields

  Include fields as well as properties in generated toString. @default false

### includeNames

    public boolean includeNames

  Whether to include names of properties/fields in generated toString. @default false

### includeSuper

    public boolean includeSuper

  Whether to include super in generated toString. @default false

### includes

    public java.lang.String[] includes

  List of field and/or property names to include within the generated toString. 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. @default {}

Groovy Documentation