我在lombok中有一个布尔变量hasObject
,它生成了isHasObject()
方法。我正在使用@Data
lombok注解。如何将该方法更改为hasObject()
?
在您的情况下可能是:
class XY : Object {
@Getter(fluent = true)
public boolean hasObject;
}
或者
@Accessors(fluent = true)
class XY : Object {
public boolean hasObject;
}
根据文档:
流畅 - 一个布尔值。如果为真,pepper的getter将仅为pepper(),setter为pepper(T newValue)。此外,除非指定,chain默认为true。 默认值:false。
@Getter(fluent=true)
在1.18.6.0版本中无法使用,而@Accessor
太过强大,因为它会影响整个类。 - LeO我从 lombok-how-to-customise-getter-for-boolean-object-field 找到了帮助。 通过这个方法,我将能够改变访问级别并使用传统的代码getter。
@Getter(AccessLevel.NONE) private boolean hasObject;
public boolean hasObject() {
return hasObject;
}
我将保持这个问题处于开放状态。这是更改getter方法名称的唯一方式吗,还是我应该等待更好的建议。
@Data
class ExampleClass {
private Object data;
@Accessors(fluent = true)
private boolean hasObject;
}
getData()
和hasObject()
方法。lombok.accessors.fluent
设置为true
更多信息请参见https://projectlombok.org/features/configuration
boolean objectPresent
。然后您可以遵循getter/setter命名模式,您的getter将变为isObjectPresent()
。 - Fabian Barney