61

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

cuh
  • 3,723
  • 4
  • 30
  • 47
Nitesh Kumar
  • 789
  • 1
  • 5
  • 11

3 Answers3

116

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()
Sergii Bishyr
  • 8,331
  • 6
  • 40
  • 69
Harald Gliebe
  • 7,236
  • 3
  • 33
  • 38
23

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

Kent Munthe Caspersen
  • 5,918
  • 1
  • 35
  • 34
12

I do some tests against the lombok(1.16.8), and the conclusions are as below.

private Boolean good;

getter => getGood()              Boolean
setter => setGood(Boolean good)  void 


private boolean good;

getter => isGood()               boolean
setter => setGood(boolean good)  void 


private Boolean isGood;

getter => getIsGood()            Boolean
setter => setIsGood()            void 


private boolean isGood;

getter => isGood()               boolean
setter => setGood(boolean good)  void
David Siegal
  • 4,518
  • 2
  • 17
  • 15
Sunny
  • 129
  • 1
  • 3
  • 2
    How to force Lombok to generate isGood for getter on non-primitive types like `Boolean isGood` ? – Andrew Feb 02 '23 at 10:27