Lombok annotation @Getter for boolean field

后端 未结 3 1265
逝去的感伤
逝去的感伤 2020-11-30 07:37

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

相关标签:
3条回答
  • 2020-11-30 08:01

    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()
    
    0 讨论(0)
  • 2020-11-30 08:01

    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 too some frustration will have a getter generated with the name isCanDelete. To avoid this you can use the fluent parameter as in:

    @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 is.

    0 讨论(0)
  • 2020-11-30 08:10

    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
    
    0 讨论(0)
提交回复
热议问题