aboutsummaryrefslogtreecommitdiff
path: root/website/templates/features/ToString.html
blob: c740ec812ec16a41bcdb50d184a8c81f0328d4f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<#import "_features.html" as f>

<@f.scaffold title="@ToString" logline="No need to start a debugger to see your fields: Just let lombok generate a <code>toString</code> for you!">
	<@f.overview>
		<p>
			Annotating a class with <code>@ToString</code> will cause lombok to generate an implementation of the <code>toString()</code> method. You use configuration options to specify whether field names should be included but otherwise the format is fixed: the class name followed by parentheses containing fields separated by commas, e.g. <code>MyClass(foo=123, bar=234)</code>.
		</p><p>
			By setting the <code>includeFieldNames</code> parameter to <em>true</em> you can add some clarity (but also quite some length) to the output of the <code>toString()</code> method.
		</p><p>
			By default, all non-static fields will be printed. If you want to skip some fields, you can annotate these fields with <code>@ToString.Exclude</code>. Alternatively, you can specify exactly which fields you wish to be used by using <code>@ToString(onlyExplicitlyIncluded = true)</code>, then marking each field you want to include with <code>@ToString.Include</code>.
		</p><p>
			By setting <code>callSuper</code> to <em>true</em>, you can include the output of the superclass implementation of <code>toString</code> to the output. Be aware that the default implementation of <code>toString()</code> in <code>java.lang.Object</code> is pretty much meaningless, so you probably don't want to do this unless you are extending another class.
		</p><p>
			You can also include the output of a method call in your <code>toString</code>. Only instance (non-static) methods that take no arguments can be included. To do so, mark the method with <code>@ToString.Include</code>.
		</p><p>
			You can change the name used to identify the member with <code>@ToString.Include(name = "some other name")</code>, and you can change the order in which the members are printed via <code>@ToString.Include(rank = -1)</code>. Members without a rank are considered to have rank 0, members of a higher rank are printed first, and members of the same rank are printed in the same order they appear in the source file.
		</p>
	</@f.overview>

	<@f.snippets name="ToString" />

	<@f.confKeys>
		<dt>
			<code>lombok.toString.includeFieldNames</code> = [<code>true</code> | <code>false</code>] (default: true)
		</dt><dd>
			Normally lombok generates a fragment of the toString response for each field in the form of <code>fieldName = fieldValue</code>. If this setting is set to <code>false</code>, lombok will omit the name of the field and simply deploy a comma-separated list of all the field values. The annotation parameter '<code>includeFieldNames</code>', if explicitly specified, takes precedence over this setting.
		</dd><dt>
			<code>lombok.toString.doNotUseGetters</code> = [<code>true</code> | <code>false</code>] (default: false)
		</dt><dd>
			If set to <code>true</code>, lombok will access fields directly instead of using getters (if available) when generating <code>toString</code> methods. The annotation parameter '<code>doNotUseGetters</code>', if explicitly specified, takes precedence over this setting.
		</dd><dt>
			<code>lombok.toString.callSuper</code> = [<code>call</code> | <code>skip</code> | <code>warn</code>] (default: skip)
		</dt><dd>
			If set to <code>call</code>, lombok will generate calls to the superclass implementation of <code>toString</code> if your class extends something. If set to <code>skip</code> no such call is generated. If set to <code>warn</code> no such call is generated either, but lombok does generate a warning to tell you about it.
		</dd><dt>
			<code>lombok.toString.onlyExplicitlyIncluded</code> = [<code>true</code> | <code>false</code>] (default: false)
		</dt><dd>
			If set to <code>false</code> (default), all fields (unless <code>static</code>, name starts with a dollar, or otherwise excluded for obvious reasons) serve as the default set of things to include in the toString, modifiable by using the <code>@ToString.Exclude</code> and <code>@ToString.Include</code> options.
			If set to <code>true</code>, nothing is included unless explicitly marked with <code>@ToString.Include</code>.
		</dd><dt>
			<code>lombok.toString.flagUsage</code> = [<code>warning</code> | <code>error</code>] (default: not set)
		</dt><dd>
			Lombok will flag any usage of <code>@ToString</code> as a warning or error if configured.
		</dd>
	</@f.confKeys>

	<@f.smallPrint>
		<p>
			If there is <em>any</em> method named <code>toString</code> with no arguments, regardless of return type, no method will be generated, and instead a warning is emitted explaining that your <code>@ToString</code> annotation is doing nothing. You can mark any method with <code>@lombok.experimental.Tolerate</code> to hide them from lombok.
		</p><p>
			Arrays are printed via <code>Arrays.deepToString</code>, which means that arrays that contain themselves will result in <code>StackOverflowError</code>s. However, this behaviour is no different from e.g. <code>ArrayList</code>.
		</p><p>
			If a method is marked for inclusion and it has the same name as a field, it replaces the toString output for that field (the method is included, the field is excluded, and the method's output is printed in the place the field would be printed).
		</p><p>
			Prior to lombok 1.16.22, inclusion/exclusion could be done with the <code>of</code> and <code>exclude</code> parameters of the <code>@ToString</code> annotation. This old-style inclusion mechanism is still supported but will be deprecated in the future.
		</p><p>
			Having both <code>@ToString.Exclude</code> and <code>@ToString.Include</code> on a member generates a warning; the member will be excluded in this case.
		</p><p>
			We don't promise to keep the output of the generated <code>toString()</code> methods the same between lombok versions. You should never design your API so that other code is forced to parse your <code>toString()</code> output anyway!
		</p><p>
			By default, any variables that start with a $ symbol are excluded automatically. You can only include them by using the <code>@ToString.Include</code> annotation.
		</p><p>
			If a getter exists for a field to be included, it is called instead of using a direct field reference. This behaviour can be suppressed:<br />
			<code>@ToString(doNotUseGetters = true)</code>
		</p><p>
			<code>@ToString</code> can also be used on an enum definition.
		</p><p>
			If you have configured a nullity annotation flavour via <a href="configuration"><code>lombok.config</code></a> key <code>lombok.addNullAnnotations</code>, the method or return type (as appropriate for the chosen flavour) is annotated with a non-null annotation.
		</p>
	</@f.smallPrint>
</@f.scaffold>