From e95680a76733c22ee5937a586ee50c703d5ba621 Mon Sep 17 00:00:00 2001
From: Reinier Zwitserloot @Singular
field/parameter because it is rather complicated. You can view a snippet here.
If also using setterPrefix = "with"
, the generated names are, for example, withName
(add 1 name), withNames
(add many names), and clearNames
(reset all names).
+
+ Ordinarily, the generated 'plural form' method (which takes in a collection, and adds each element in this collection) will check if a null
is passed and throws a NullPointerException
with an appropriate message. However, you can configure alternative behaviour. For example, for deserialization classes it can be useful to just do nothing (as if an empty collection was passed) instead: @Singular(nullBehavior = NullCollectionBehavior.IGNORE)
. If you want to change the kind of nullcheck lombok generates (for example, if you prefer the non-recommended IllegalArgumentException
instead, we suggest you configure this in one central place by making a lombok.config
entry instead; the intended use for specifying the behavior on the @Singular
annotation directly is to explicitly request the IGNORE
behaviour.
lombok.singular.auto
= [true
| false
] (default: true)
true
(which is the default), lombok automatically tries to singularize your identifier name by assuming that it is a common english plural. If false
, you must always explicitly specify the singular name, and lombok will generate an error if you don't (useful if you write your code in a language other than english).
+ lombok.singular.nullCollections
= [NullPointerException
| IllegalArgumentException
| Guava
| JDK
| ignore
] (default: NullPointerException
)
+ null
argument? Normally, lombok does an explicit null check with an appropriate message, but you can use this configuration to pick another flavour of nullcheck. The ignore
option makes lombok treat null as an empty collection: Do nothing. An appropriate nullity annotation will be placed on the generated plural form method's parameter if you configured the flavour of nullity annotations you want via lombok.config
key lombok.addNullAnnotations
.
If setting the access level to PROTECTED
, all methods generated inside the builder class are actually generated as public
; the meaning of the
protected
keyword is different inside the inner class, and the precise behaviour that PROTECTED
would indicate (access by any source in the same package is allowed, as well as any subclasses from the outer class, marked with @Builder
is not possible, and marking the inner members public
is as close as we can get.
+
+ If you have configured a nullity annotation flavour via lombok.config
key lombok.addNullAnnotations
, any plural-form generated builder methods for @Singular
marked properties (these plural form methods take a collection of some sort and add all elements) get a nullity annotation on the parameter. You get a non-null one normally, but if you have configured the behaviour on null
being passed in as collection to IGNORE
, a nullable annotation is generated instead.
@EqualsAndHashCode(doNotUseGetters = true)
- If the class (or an enclosing class) has either the @org.eclipse.jdt.annotation.NonNullByDefault
or the @javax.annotation.ParametersAreNonnullByDefault
annotation, the parameter of the generated equals
method will have the appropriate @Nullable
annotation; JDK8+ is required when you do this. (since 1.18.12).
+ If you have configured a nullity annotation flavour via lombok.config
key lombok.addNullAnnotations
, the parameter of both the generated equals
method as well as any canEqual
method is annotated with a nullable annotation. This is required if you use a @NonNullByDefault
style annotation in combination with strict nullity checking.
@ToString(doNotUseGetters = true)
@ToString
can also be used on an enum definition.
+
+ If you have configured a nullity annotation flavour via lombok.config
key lombok.addNullAnnotations
, the method or return type (as appropriate for the chosen flavour) is annotated with a non-null annotation.
withX(int x)
will not be generated if there's already a method withX(String... x)
even though it is technically possible to make the method. This caveat exists to prevent confusion. If the generation of a method is skipped for this reason, a warning is emitted instead. Varargs count as 0 to N parameters.
Various well known annotations about nullity cause null checks to be inserted and will be copied to the parameter. See Getter/Setter documentation's small print for more information. +
+ If you have configured a nullity annotation flavour via lombok.config
key lombok.addNullAnnotations
, the method or return type (as appropriate for the chosen flavour) is annotated with a non-null annotation.
config.stopBubbling = true
+ Lombok can add nullity annotations (usually called @NonNull
and @Nullable
) whenever it makes sense to do so; think of generated toString
and withX
methods (these never return null), or the parameter of a generated equals
method, which is allowed to be null, and requires such an annotation if you've set up your IDE for strict null checks as well as 'parameters are non-null by default'. There are many such libraries; you must tell lombok which one to use. By default, no such annotations are added. Enable this feature with:
+
lombok.addNullAnnotations = flavour
(flavours: javax
(=JSR305; not recommended), eclipse
, jetbrains
, netbeans
, androidx
, android.support
(deprecated within android), checkerframework
(recommended), findbugs
, spring
, jml
, or define your own via CUSTOM:fully.qualified.NonNullAnnotation:fully.qualified.NullableAnnotation
.
+
Lombok can add @javax.annotation.Generated
annotations to all generated nodes where possible. You can enable this with: