aboutsummaryrefslogtreecommitdiff
path: root/website/templates/features/constructor.html
blob: f9ee775d2510772e12edae13f100c5a090549d6f (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
<#import "_features.html" as f>

<@f.scaffold title="@NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor"
	logline="Constructors made to order: Generates constructors that take no arguments, one argument per final / non-null field, or one argument for every field.">

	<@f.overview>
		<p>
			This set of 3 annotations generate a constructor that will accept 1 parameter for certain fields, and simply assigns this parameter to the field.
		</p><p>
			<code>@NoArgsConstructor</code> will generate a constructor with no parameters. If this is not possible (because of final fields), a compiler error will result instead, unless <code>@NoArgsConstructor(force = true)</code> is used, then all final fields are initialized with <code>0</code> / <code>false</code> / <code>null</code>. For fields with constraints, such as <code>@NonNull</code> fields, <em>no</em> check is generated,so be aware that these constraints will generally not be fulfilled until those fields are properly initialized later. Certain java constructs, such as hibernate and the Service Provider Interface require a no-args constructor. This annotation is useful primarily in combination with either <code>@Data</code> or one of the other constructor generating annotations.
		</p><p>
			<code>@RequiredArgsConstructor</code> generates a constructor with 1 parameter for each field that requires special handling. All non-initialized <code>final</code> fields get a parameter, as well as any fields that are marked as <code>@NonNull</code> that aren't initialized where they are declared. For those fields marked with <code>@NonNull</code>, an explicit null check is also generated. The constructor will throw a <code>NullPointerException</code> if any of the parameters intended for the fields marked with <code>@NonNull</code> contain <code>null</code>. The order of the parameters match the order in which the fields appear in your class.
		</p><p>
			<code>@AllArgsConstructor</code> generates a constructor with 1 parameter for each field in your class. Fields marked with <code>@NonNull</code> result in null checks on those parameters.
		</p><p>
			Each of these annotations allows an alternate form, where the generated constructor is always private, and an additional static factory method that wraps around the private constructor is generated. This mode is enabled by supplying the <code>staticName</code> value for the annotation, like so: <code>@RequiredArgsConstructor(staticName="of")</code>. Such a static factory method will infer generics, unlike a normal constructor. This means your API users get write <code>MapEntry.of("foo", 5)</code> instead of the much longer <code>new MapEntry&lt;String, Integer&gt;("foo", 5)</code>.
		</p><p>
			To put annotations on the generated constructor, you can use <code>onConstructor=@__({@AnnotationsHere})</code>, but be careful; this is an experimental feature. For more details see the documentation on the <a href="/features/experimental/onX">onX</a> feature.
		</p><p>
			Static fields are skipped by these annotations.
		</p><p>
			Unlike most other lombok annotations, the existence of an explicit constructor does not stop these annotations from generating their own constructor. This means you can write your own specialized constructor, and let lombok generate the boilerplate ones as well. If a conflict arises (one of your constructors ends up with the same signature as one that lombok generates), a compiler error will occur.
		</p>
	</@f.overview>

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

	<@f.confKeys>
		<dt>
			<code>lombok.anyConstructor.addConstructorProperties</code> = [<code>true</code> | <code>false</code>] (default: <code>false</code>)
		</dt><dd>
			If set to <code>true</code>, then lombok will add a <code>@java.beans.ConstructorProperties</code> to generated constructors.
		</dd><dt>
			<code>lombok.</code>[<code>allArgsConstructor</code>|<code>requiredArgsConstructor</code>|<code>noArgsConstructor</code>]<code>.flagUsage</code> = [<code>warning</code> | <code>error</code>] (default: not set)
		</dt><dd>
			Lombok will flag any usage of the relevant annotation (<code>@AllArgsConstructor</code>, <code>@RequiredArgsConstructor</code> or <code>@NoArgsConstructor</code>) as a warning or error if configured.
		</dd><dt>
			<code>lombok.anyConstructor.flagUsage</code> = [<code>warning</code> | <code>error</code>] (default: not set)
		</dt><dd>
			Lombok will flag any usage of any of the 3 constructor-generating annotations as a warning or error if configured.
		</dd><dt>
			<code>lombok.copyableAnnotations</code> = [<em>A list of fully qualified types</em>] (default: empty list)
		</dt><dd>
			Lombok will copy any of these annotations from the field to the constructor parameter, the setter parameter, and the getter method. Note that lombok ships with a bunch of annotations 'out of the box' which are known to be copyable: All popular nullable/nonnull annotations.
		</dd><dt>
			<code>lombok.noArgsConstructor.extraPrivate</code> = [<code>true</code> | <code>false</code>] (default: false)
		</dt><dd>
			If <code>true</code>, lombok will generate a private no-args constructor for any <code>@Value</code> or <code>@Data</code> annotated class, which sets all fields to default values (null / 0 / false).
		</dd>
	</@f.confKeys>

	<@f.smallPrint>
		<p>
			Even if a field is explicitly initialized with <code>null</code>, lombok will consider the requirement to avoid null as fulfilled, and will <em>NOT</em> consider the field as a 'required' argument. The assumption is that if you explicitly assign <code>null</code> to a field that you've also marked as <code>@NonNull</code> signals you must know what you're doing.
		</p><p>
			The <code>@java.beans.ConstructorProperties</code> annotation is never generated for a constructor with no arguments. This also explains why <code>@NoArgsConstructor</code> lacks the <code>suppressConstructorProperties</code> annotation method. The generated static factory methods also do not get <code>@ConstructorProperties</code>, as this annotation can only be added to real constructors.
		</p><p>
			<code>@XArgsConstructor</code> can also be used on an enum definition. The generated constructor will always be private, because non-private constructors aren't legal in enums. You don't have to specify <code>AccessLevel.PRIVATE</code>.
		</p><p>
			Various well known annotations about nullity cause null checks to be inserted and will be copied to the parameter. See <a href="/features/GetterSetter">Getter/Setter</a> documentation's small print for more information.
		</p><p>
			The <code>flagUsage</code> configuration keys do not trigger when a constructor is generated by <code>@Data</code>, <code>@Value</code> or any other lombok annotation.
		</p>
	</@f.smallPrint>
</@f.scaffold>