@FieldDefaults
New default field modifiers for the 21st century.
@FieldDefaults was introduced as experimental feature in lombok v0.11.4.
Experimental
Experimental because:- Would be nice if you could stick this on the package-info.java package to set the default for all classes in that package.
-
[UPDATE 2022-02-04] Currently simply having a
lombok.config
entry oflombok.fieldDefaults.defaultPrivate = true
(or, analogously,defaultFinal
) is enough to modify every source file that is affected by that configuration, even if said source file has absolutely no trace whatsoever of lombok anything inside it. We're not quite sure if this is a good idea. Our current point of view is that this is too much magic, and there is an alternative plan: meta-annotations. Until at least the meta-annotations idea has been explored and discarded, this feature will not be leaving experimental in its current state. Most likely, if it ever does, thelombok.FieldDefaults
annotation will be required, though, you may set it via the to be built meta-annotation.
Overview
The @FieldDefaults
annotation can add an access modifier (public
, private
, or protected
) to each field in the annotated class or enum. It can also add final
to each field in the annotated class or enum.
To add final
to each (instance) field, use @FieldDefaults(makeFinal=true)
. Any non-final field which must remain nonfinal can be annotated with @NonFinal
(also in the lombok.experimental
package).
To add an access modifier to each (instance) field, use @FieldDefaults(level=AccessLevel.PRIVATE)
. Any field that does not already have an access modifier (i.e. any field that looks like package private access) is changed to have the appropriate access modifier. Any package private field which must remain package private can be annotated with @PackagePrivate
(also in the lombok.experimental
package).
With Lombok
import lombok.AccessLevel;
|
Vanilla Java
public class FieldDefaultsExample {
|
Supported configuration keys:
-
lombok.fieldDefaults.flagUsage
= [warning
|error
] (default: not set) -
Lombok will flag any usage of
@FieldDefaults
as a warning or error if configured. -
lombok.fieldDefaults.defaultPrivate
= [true
|false
] (default: false) -
(Since 1.16.8) If set to
true
, every field in every class or enum anywhere in the sources being compiled will be marked asprivate
unless it has an explicit access modifier or the@PackagePrivate
annotation, or an explicit@FieldDefaults
annotation is present to override this config key. -
lombok.fieldDefaults.defaultFinal
= [true
|false
] (default: false) -
(Since 1.16.8) If set to
true
, every field in every class or enum anywhere in the sources being compiled will be marked asfinal
unless it has the@NonFinal
annotation, or an explicit@FieldDefaults
annotation is present to override this config key.
Small print
Like other lombok handlers that touch fields, any field whose name starts with a dollar ($
) symbol is skipped entirely. Such a field will not be modified at all.