#import "../_features.html" as f> <@f.scaffold title="@FieldNameConstants" logline="Name... that... field! String constants for your field's names."> <@f.history>
@FieldNameConstants was introduced as experimental feature in lombok v1.16.22.
@FieldNameConstants was redesigned in lombok v1.18.4.
The lombok.config option lombok.fieldNameConstants.uppercase = true
was added in lombok v1.18.8.
The @FieldNameConstants
annotation generates an inner type which contains 1 constant for each field in your class; either string constants (fields marked public static final
, of type java.lang.String
) or if you prefer, an enum type with 1 value for each field - write @FieldNameConstants(asEnum = true)
for the enum variant. @FieldNameConstants
is useful for various marshalling and serialization frameworks. The constant field (whether enum value or string constant) always has the exact same name as the field, capitalization and all, unless you set the lombok.fieldNameConstants.uppercase = true
option in your lombok.config
file; in that case lombok will try to UPPER_CASE
the name.
The generated inner type is by default called Fields
and is public
. You can modify this via @FieldNameConstants(innerTypeName = "FieldNames", level = AccessLevel.PACKAGE)
for example. The default inner type name can also be modified via configuration key lombok.fieldNameConstants.innerTypeName
. The generated fields are always public
.
Must be applied to classes (or enums, though you'd rarely want to do that). By default includes all non-transient, non-static fields. You can use @FieldNameConstants.Include
in fields + @FieldNameConstants(onlyExplicitlyIncluded = true)
, or @FieldNameConstants.Exclude
for more fine-grained control.
lombok.fieldNameConstants.flagUsage
= [warning
| error
] (default: not set)
@FieldDefaults
as a warning or error if configured.
lombok.fieldNameConstants.innerTypeName
= a string (default: 'Fields')
lombok.fieldNameConstants.uppercase
= [true
| false
] (default: false)
true
, attempt to uppercase the generated fields.
Starting with lombok v1.18.6, lombok will silently skip generating anything that already exists. You can define the inner Fields
enum/class yourself,
in which case lombok will add all the enum constants / public static final fields you haven't written yourself.
From lombok v1.16.22 to lombok v1.18.2, this feature generated constants inside the type directly; the name of these fields would for example turn field exampleFieldName
into public static final String FIELD_EXAMPLE_FIELD_NAME = "exampleFieldName";
. The prefix and suffix (here, FIELD_
, and the empty string) were configurable. Starting with lombok v1.18.4 this feature has been redesigned into generating an inner type as described above.
Any parameters of lombok annotations that take strings need to be supplied actual string literals; you cannot have references to constants like those generated by @FieldNameConstants
. If you'd like to use @FieldNameConstants
to for example fill in the of
and/or exclude
parameters of @ToString
and similar lombok annotations, use the @ToString.Include
/ @ToString.Exclude
etc system instead; these are described at the feature pages for those features.
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. Static fields are also skipped.
MapStruct interop: When making references to field name constants inside MapStruct's @Mapping
, and you use that annotation more than once on a node, you must manually wrap these in the @Mappings
'container annotation'. Like so: @Mappings({@Mapping(target = Entity.Fields.entityProperty, source = "dtoProperty")})
.