@Accessors
A more fluent API for getters and setters.
@Accessors
was introduced as experimental feature in lombok v0.11.0.
The lombok.config option lombok.accessors.capitalization
= [basic
| beanspec
] was added in lombok v1.18.24.
FUNCTIONAL CHANGE: @Accessors
now 'cascades'; any options not set on a field-level @Accessors
annotation will get inherited from an
@Accessors
annotation on the class (and any options not set on those, from the enclosing class). Finally, anything set in lombok.config
will be used as default. (lombok v1.18.24)
NEW FEATURE: @Accessors(makeFinal = true)
will create final
getters, setters, and with-ers. There's also
lombok.config
key lombok.accessors.makeFinal
for the same effect. (lombok v1.18.24)
Experimental
Experimental because:- We may want to roll these features into a more complete property support concept.
-
The
makeFinal
feature is recently released; awaiting community feedback.
- Open feature request: More control over naming accessors; for example to address creatively named boolean properties: Turn
boolean wasRunning
intoboolean wasRunning()
instead ofboolean isWasRunning()
, as well as more expansive prefix support.@Accessors
will be involved if this feature request is addressed. @Accessors
currently does not 'cascade' from field@Accessors
annotation to the class-level@Accessors
annotation, but it does 'cascade' tolombok.config
. Changing this is not difficult but backwards incompatible. It's not likely to break much existing code, but this needs to be decided on before the feature can move out of experimental status.
Overview
The @Accessors
annotation is used to configure how lombok generates and looks for getters, setters, and with-ers.
By default, lombok follows the bean specification for getters and setters: The getter for a field named pepper
is getPepper
for example. However, some might like to break with the bean specification in order to end up with nicer looking APIs. @Accessors
lets you do this.
Some programmers like to use a prefix for their fields, i.e. they write fPepper
instead of pepper
. We strongly discourage doing this, as you can't unit test the validity of your prefixes, and refactor scripts may turn fields into local variables or method names. Furthermore, your tools (such as your editor) can take care of rendering the identifier in a certain way if you want this information to be instantly visible. Nevertheless, you can list the prefixes that your project uses via @Accessors
as well.
@Accessors
has 4 options:
-
fluent
– A boolean. If true, the getter forpepper
is justpepper()
, and the setter ispepper(T newValue)
. Furthermore, unless specified,chain
defaults to true.
Default: false. -
chain
– A boolean. If true, generated setters returnthis
instead ofvoid
.
Default: false, unlessfluent=true
, then Default: true. -
makeFinal
– A boolean. If true, generated getters, setters, and with-ers are marked asfinal
.
Default: false. -
prefix
– A list of strings. If present, fields must be prefixed with any of these prefixes. Each field name is compared to each prefix in the list in turn, and if a match is found, the prefix is stripped out to create the base name for the field. It is legal to include an empty string in the list, which will always match. For characters which are letters, the character following the prefix must not be a lowercase letter, i.e.pepper
is not a match even to prefixp
, butpEpper
would be (and would mean the base name of this field isepper
).
The @Accessors
annotation is legal on types and fields; getters/setters/with-ers will look at the annotation on the field first, on the type the field is in second (and you have types in types,
each outer type is also checked), and finally for any properties not explicitly set, the appropriate lombok.config
setting is used.
With Lombok
import lombok.experimental.Accessors;
|
Vanilla Java
public class AccessorsExample {
|
Supported configuration keys:
-
lombok.accessors.chain
= [true
|false
] (default: false) -
If set to
true
, any field/class that either doesn't have an@Accessors
annotation, or it does, but that annotation does not have an explicit value for thechain
parameter, will act as if@Accessors(chain = true)
is present. -
lombok.accessors.fluent
= [true
|false
] (default: false) -
If set to
true
, any field/class that either doesn't have an@Accessors
annotation, or it does, but that annotation does not have an explicit value for thefluent
parameter, will act as if@Accessors(fluent = true)
is present. -
lombok.accessors.makeFinal
= [true
|false
] (default: false) -
If set to
true
, any field/class that either doesn't have an@Accessors
annotation, or it does, but that annotation does not have an explicit value for themakeFinal
parameter, will act as if@Accessors(makeFinal = true)
is present. -
lombok.accessors.prefix
+= a field prefix (default: empty list) -
This is a list property; entries can be added with the
+=
operator. Inherited prefixes from parent config files can be removed with the-=
operator. Any class that either doesn't have an@Accessors
annotation, or it does, but that annotation does not have an explicit value for theprefix
parameter, will act as if@Accessors(prefix = {prefixes listed in configuration})
is present. -
lombok.accessors.capitalization
= [basic
|beanspec
] (default: basic) -
Controls how tricky cases like
uShaped
(one lowercase letter followed by an upper/titlecase letter) are capitalized.basic
capitalizes that togetUShaped
, andbeanspec
capitalizes that togetuShaped
instead.
Both strategies are commonly used in the java ecosystem, thoughbeanspec
is more common. -
lombok.accessors.flagUsage
= [warning
|error
] (default: not set) -
Lombok will flag any usage of
@Accessors
as a warning or error if configured.
Small print
The nearest @Accessors
annotation is also used for the various methods in lombok that look for getters, such as @EqualsAndHashCode
.
If a prefix list is provided and a field does not start with one of them, that field is skipped entirely by lombok, and a warning will be generated.