Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lombok annotation @Getter for boolean field

Tags:

java

lombok

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()?

like image 520
Nitesh Kumar Avatar asked Mar 06 '17 07:03

Nitesh Kumar


People also ask

What is @getter annotation in Java?

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.

What is @getter and @setter in spring boot?

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.

Can I override Lombok getter?

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.


2 Answers

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() 
like image 168
Harald Gliebe Avatar answered Sep 18 '22 19:09

Harald Gliebe


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().

like image 33
Kent Munthe Caspersen Avatar answered Sep 19 '22 19:09

Kent Munthe Caspersen