Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lombok how to customise getter for Boolean object field?

Tags:

java

lombok


One of my POJOs has a Boolean object field to permit NULLS in the database (a requirement). Is it possible to use the @Data Lombok annotation at class level yet override the getter for the Boolean field? The default it generates is getXXX method for the Boolean field. I wish to override it as isXXX()?

Thanks,
Paddy

like image 201
Paddy Avatar asked Aug 09 '13 03:08

Paddy


People also ask

How do you override the getter in Lombok?

From Lombok documentation: You can always manually disable getter/setter generation for any field by using the special AccessLevel. NONE access level. This lets you override the behaviour of a @Getter, @Setter or @Data annotation on a class.

How do you set the setter and getter with Lombok?

Overview. You can annotate any field with @Getter and/or @Setter , to let lombok generate the default getter/setter automatically. A default getter simply returns the field, and is named getFoo if the field is called foo (or isFoo if the field's type is boolean ).

What is the use of @data in Lombok?

@Data is a convenient shortcut annotation that bundles the features of @ToString , @EqualsAndHashCode , @Getter / @Setter and @RequiredArgsConstructor together: In other words, @Data generates all the boilerplate that is normally associated with simple POJOs (Plain Old Java Objects) and beans: getters for all fields, ...


2 Answers

It's a bit verbose, but you can provide your own isXXX, and then use AccessLevel.NONE to tell Lombok not to generate the getXXX:

@Data public class OneOfPaddysPojos {      // ... other fields ...      @Getter(AccessLevel.NONE)     private Boolean XXX;      public Boolean isXXX() {         return XXX;     } } 

(And hey, at least it's not quite as verbose as if you weren't using Lombok to begin with!)

like image 78
ruakh Avatar answered Oct 08 '22 05:10

ruakh


I think if you switch your field from Boolean X to boolean X than lombok generate a getter isX() method.

like image 40
kate Avatar answered Oct 08 '22 05:10

kate