I am using Java lombok annotation @Getter
to generate getters for my POJO. I have a boolean
field by the name isAbc
.
The @Getter
annotation in this case generates a method by the name isAbc()
. Shouldn't it generate a method by the name isIsAbc()
?
The @Getter annotation is used to generate the default getter implementation for fields that are annotated with the annotation. This annotation can also be used at the class level in which Lombok will generate the getter methods for all fields.
Getters and Setters play an important role in retrieving and updating the value of a variable outside the encapsulating class. A setter updates the value of a variable, while a getter reads the value of a variable.
From Lombok documentation: You can always manually disable getter/setter generation for any field by using the special AccessLevel. NONE access level. This lets you override the behaviour of a @Getter, @Setter or @Data annotation on a class.
Read the 'small print' section on the lombok page https://projectlombok.org/features/GetterSetter.html
For boolean fields that start with is immediately followed by a title-case letter, nothing is prefixed to generate the getter name.
So the behavior you experience is as specified.
Note that the behavior is different for boolean
and Boolean
:
@Getter private boolean isGood; // => isGood() @Getter private boolean good; // => isGood() @Getter private Boolean isGood; // => getIsGood()
Lombok does not prefix with is
if the name already starts with is
followed by an uppercase letter as in isGood
.
You might encounter names like canDelete
which will generate a getter with the name isCanDelete
. To avoid this you can use the fluent
parameter:
@Getter(fluent = true) private boolean canDelete;
or (depending on version):
@Getter @Accessors(fluent = true) private boolean canDelete;
In which case it will leave the name as it is so the getter becomes canDelete()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With