35

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

Michael Lihs
  • 7,460
  • 17
  • 52
  • 85
mwKART
  • 885
  • 2
  • 9
  • 18
  • 5
    Consider 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
  • 2
    I have to stick to "has" prefix based on the API Documentation. So I dont have that privilege. – mwKART Mar 08 '17 at 15:20
  • 1
    Possible 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

5 Answers5

28

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
  • 49,552
  • 17
  • 113
  • 135
  • 2
    The 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
  • 15
    @Getter(fluent = true) doesn't compile – dan carter Sep 26 '19 at 08:36
  • 1
    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
23

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.

Community
  • 1
  • 1
mwKART
  • 885
  • 2
  • 9
  • 18
  • Hi I voted this answer down because it changed within 5 years and @Rami s answer from May 11, 2022 is the best one (in my oppinion). – klenkes74 Jan 20 '23 at 13:51
  • @klenkes74 I disagree with the downvote because Accessor modifies both the getter and setter when Data is used so this is still the best answer with OP's original question of modifying the getter. – CodeMonkey May 25 '23 at 00:27
19

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
  • 3,217
  • 2
  • 27
  • 42
3

Just like this:

 @Data
 class ExampleClass {
     
      private Object data;

      @Accessors(fluent = true)
      private boolean hasObject;
 }

This will provide getData() and hasObject() methods.

Rami
  • 73
  • 6
0

In case you are looking for solution which will apply for your whole application, you can set lombook configuration lombok.accessors.fluent to true

For more informations see https://projectlombok.org/features/configuration

Pavel Merta
  • 69
  • 2
  • 2