View Javadoc
1   /*
2    * Copyright (C) 2009-2018 The Project Lombok Authors.
3    * 
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   * 
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   * 
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20   * THE SOFTWARE.
21   */
22  package lombok;
23  
24  import java.lang.annotation.ElementType;
25  import java.lang.annotation.Retention;
26  import java.lang.annotation.RetentionPolicy;
27  import java.lang.annotation.Target;
28  
29  /**
30   * Generates an implementation for the {@code toString} method inherited by all objects, consisting of printing the values of relevant fields.
31   * <p>
32   * Complete documentation is found at <a href="https://projectlombok.org/features/ToString">the project lombok features page for &#64;ToString</a>.
33   */
34  @Target(ElementType.TYPE)
35  @Retention(RetentionPolicy.SOURCE)
36  public @interface ToString {
37  	/**
38  	 * Include the name of each field when printing it.
39  	 * <strong>default: true</strong>
40  	 * 
41  	 * @return Whether or not to include the names of fields in the string produced by the generated {@code toString()}.
42  	 */
43  	boolean includeFieldNames() default true;
44  	
45  	/**
46  	 * Any fields listed here will not be printed in the generated {@code toString} implementation.
47  	 * Mutually exclusive with {@link #of()}.
48  	 * <p>
49  	 * Will soon be marked {@code @Deprecated}; use the {@code @ToString.Exclude} annotation instead.
50  	 * 
51  	 * @return A list of fields to exclude.
52  	 */
53  	String[] exclude() default {};
54  	
55  	/**
56  	 * If present, explicitly lists the fields that are to be printed.
57  	 * Normally, all non-static fields are printed.
58  	 * <p>
59  	 * Mutually exclusive with {@link #exclude()}.
60  	 * <p>
61  	 * Will soon be marked {@code @Deprecated}; use the {@code @ToString.Include} annotation together with {@code @ToString(onlyExplicitlyIncluded = true)}.
62  	 * 
63  	 * @return A list of fields to use (<em>default</em>: all of them).
64  	 */
65  	String[] of() default {};
66  	
67  	/**
68  	 * Include the result of the superclass's implementation of {@code toString} in the output.
69  	 * <strong>default: false</strong>
70  	 * 
71  	 * @return Whether to call the superclass's {@code toString} implementation as part of the generated toString algorithm.
72  	 */
73  	boolean callSuper() default false;
74  	
75  	/**
76  	 * Normally, if getters are available, those are called. To suppress this and let the generated code use the fields directly, set this to {@code true}.
77  	 * <strong>default: false</strong>
78  	 * 
79  	 * @return If {@code true}, always use direct field access instead of calling the getter method.
80  	 */
81  	boolean doNotUseGetters() default false;
82  	
83  	/**
84  	 * Only include fields and methods explicitly marked with {@code @ToString.Include}.
85  	 * Normally, all (non-static) fields are included by default.
86  	 * 
87  	 * @return If {@code true}, don't include non-static fields automatically (default: {@code false}).
88  	 */
89  	boolean onlyExplicitlyIncluded() default false;
90  	
91  	/**
92  	 * If present, do not include this field in the generated {@code toString}.
93  	 */
94  	@Target(ElementType.FIELD)
95  	@Retention(RetentionPolicy.SOURCE)
96  	public @interface Exclude {}
97  	
98  	/**
99  	 * Configure the behaviour of how this member is rendered in the {@code toString}; if on a method, include the method's return value in the output.
100 	 */
101 	@Target({ElementType.FIELD, ElementType.METHOD})
102 	@Retention(RetentionPolicy.SOURCE)
103 	public @interface Include {
104 //		/** If true and the return value is {@code null}, omit this member entirely from the {@code toString} output. */
105 //		boolean skipNull() default false; // -- We'll add it later, it requires a complete rework on the toString code we generate.
106 		
107 		/**
108 		 * Higher ranks are printed first. Members of the same rank are printed in the order they appear in the source file.
109 		 * 
110 		 * @return ordering within the generating {@code toString()}; higher numbers are printed first.
111 		 */
112 		int rank() default 0;
113 		
114 		/**
115 		 * Defaults to the field / method name of the annotated member.
116 		 * If the name equals the name of a default-included field, this member takes its place.
117 		 * 
118 		 * @return The name to show in the generated {@code toString()}. Also, if this annotation is on a method and the name matches an existing field, it replaces that field.
119 		 */
120 		String name() default "";
121 	}
122 }