All Downloads are FREE. Search and download functionalities are using the official Maven repository.

groovy.transform.ToString Maven / Gradle / Ivy

There is a newer version: 3.0.22
Show newest version
/*
 * Copyright 2008-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package groovy.transform;

import org.codehaus.groovy.transform.GroovyASTTransformationClass;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Class annotation used to assist in the creation of {@code toString()} methods in classes.
 * The {@code @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: *

 * {@code @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: *
 * {@code @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: *
 * {@code @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
 * {@code @ToString} class NamedThing {
 *     String name
 * }
 * {@code @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))
 * 
* {@code @ToString} can also be used in conjunction with {@code @Canonical} and {@code @Immutable}. *

* If you want to omit fields or properties referring to null, you can use the ignoreNulls flag: *

 * import groovy.transform.ToString
 * {@code @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
 * {@code @ToString(includePackage = false)} class NamedThing {
 *     String name
 * }
 * println new NamedThing(name: "Lassie")
 * 
* Which results in: *
 * NamedThing(name: Lassie)
 * 
* If the includePackage flag is {@code true} (the default), then the output will be: *
 * my.company.NamedThing(name: Lassie)
 * 
* * @author Paul King * @author Andre Steingress * @see groovy.transform.Immutable * @see groovy.transform.Canonical * @since 1.8.0 */ @java.lang.annotation.Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) @GroovyASTTransformationClass("org.codehaus.groovy.transform.ToStringASTTransformation") public @interface ToString { /** * 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. */ String[] excludes() default {}; /** * 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. */ String[] includes() default {}; /** * Whether to include super in generated toString. */ boolean includeSuper() default false; /** * Whether to include names of properties/fields in generated toString. */ boolean includeNames() default false; /** * Include fields as well as properties in generated toString. */ boolean includeFields() default false; /** * Don't display any fields or properties with value null. */ boolean ignoreNulls() default false; /** * Whether to include the fully-qualified class name (i.e. including * the package) or just the simple class name in the generated toString. * @since 2.0.6 */ boolean includePackage() default true; /** * Whether to cache toString() calculations. You should only set this to true if * you know the object is immutable (or technically mutable but never changed). * @since 2.1.0 */ boolean cache() default false; }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy