Lombok annotation @Getter for boolean field

感情迁移 提交于 2019-12-17 18:34:13

问题


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


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/42619986/lombok-annotation-getter-for-boolean-field

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!