I am having a boolean variable hasObject in lombok which generates isHasObject(). I am using @Data lombok annotation. How can i change the method to hasObject()
Asked
Active
Viewed 2.3k times
29
Michael Lihs
- 6,690
- 14
- 46
- 75
mwKART
- 768
- 2
- 6
- 18
-
5Consider renaming your field name. Something like `boolean objectPresent`. Then you can follow the getter/setter naming patterns and your getter would be `isObjectPresent()`. – Fabian Barney Mar 08 '17 at 13:27
-
1I have to stick to "has" prefix based on the API Documentation. So I dont have that privilege. – mwKART Mar 08 '17 at 15:20
-
1Possible duplicate of [Lombok how to customise getter for Boolean object field?](https://stackoverflow.com/questions/18139678/lombok-how-to-customise-getter-for-boolean-object-field) – Tyler Jul 06 '17 at 20:59
-
Does this answer your question? [Lombok annotation @Getter for boolean field](https://stackoverflow.com/questions/42619986/lombok-annotation-getter-for-boolean-field) – E-Riz Dec 20 '21 at 22:14
4 Answers
22
in your case it could be:
class XY : Object {
@Getter(fluent = true)
public boolean hasObject;
}
OR
@Accessors(fluent = true)
class XY : Object {
public boolean hasObject;
}
according to the docs:
fluent - A boolean. If true, the getter for pepper is just pepper(), and the setter is pepper(T newValue). Furthermore, unless specified, chain defaults to true. Default: false.
Daij-Djan
- 48,496
- 17
- 105
- 132
-
2The major problem is that it doesn't apply to one variable only :-/ `@Getter(fluent=true)` doesn't work with 1.18.6.0 and the `@Accessor` is way to much cz it influences the whole class – LeO Aug 14 '19 at 08:26
-
9
-
https://stackoverflow.com/a/62953224/5625736 as described at this answer, Accessors can be used at a field. – tabata Oct 15 '20 at 06:20
20
I found out help from lombok-how-to-customise-getter-for-boolean-object-field. By this I will be have the altering accessor level and the code getter old fashion,
@Getter(AccessLevel.NONE) private boolean hasObject;
public boolean hasObject() {
return hasObject;
}
I will be keeping this question open. Is this the only way to change getter method name or I will wait for better suggestions.
11
Combining the Accessors and Getter, you might get the folllowing:
class ExampleClass {
@Accessors(fluent = true)
@Getter
private boolean hasObject;
}
is an equivalent to the Vanilla Java:
class ExampleClass {
private boolean hasObject;
public hasObject() {
return hasObject;
}
Which is what you wanted, I guess.
Benjamin
- 2,960
- 2
- 26
- 39
1
Just like this:
@Data
class ExampleClass {
private Object data;
@Accessors(fluent = true)
private boolean hasObject;
}
This will provide getData() and hasObject() methods.
Rami
- 43
- 6