简体   繁体   English

用于布尔字段的 Lombok 注释 @Getter

[英]Lombok annotation @Getter for boolean field

I am using Java lombok annotation @Getter to generate getters for my POJO.我正在使用 Java @Getter注释@Getter为我的 POJO 生成 getter。 I have a boolean field by the name isAbc .我有一个名为isAbcboolean字段。

The @Getter annotation in this case generates a method by the name isAbc() .在这种情况下, @Getter注释生成一个名为isAbc() Shouldn't it generate a method by the name isIsAbc() ?它不应该生成一个名为isIsAbc()吗?

Read the 'small print' section on the lombok page https://projectlombok.org/features/GetterSetter.html阅读 lombok 页面上的“小字”部分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.对于以 开头的布尔字段,后面紧跟标题大小写字母,没有任何前缀来生成 getter 名称。

So the behavior you experience is as specified.所以你所经历的行为是指定的。

Note that the behavior is different for boolean and Boolean :请注意, booleanBoolean的行为不同:

@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 .如果名称已经以is开头, is Lombok 不以is前缀,后跟一个大写字母,如isGood

You might encounter names like canDelete which too some frustration will have a getter generated with the name isCanDelete .你可能会遇到像canDelete这样的名字,这会让你感到沮丧,它会生成一个名为isCanDelete的 getter。 To avoid this you can use the fluent parameter as in:为避免这种情况,您可以使用fluent参数,如下所示:

@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.在这种情况下,它将保留名称不变。

I do some tests against the lombok(1.16.8), and the conclusions are as below.我对lombok(1.16.8)做了一些测试,结论如下。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM