92


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

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
Paddy
  • 3,472
  • 5
  • 29
  • 48
  • By definition, overriding means, writing your own getXXX for that provided by lombok. If you want to add your own isXXX, it's a different method. Why don't you create an isXXX in your POJO and in the implementation, call getXXX if required or implement it the way you like? – Jayz Aug 09 '13 at 04:07
  • I have declared @Data at class level and have too many attributes in the class, so excluding getter or making the default getter private for one field will be tough. Otherwise, even if I add a isXXX method, the getXXX method will be still visible to users of the class anyway - I would rather then use the getXXX method itself. I hope you understand my context. – Paddy Aug 09 '13 at 04:31

5 Answers5

151

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

ruakh
  • 175,680
  • 26
  • 273
  • 307
18

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

kate
  • 1,769
  • 1
  • 11
  • 10
  • 17
    True, but you need to have the field as a `Boolean` if the column is nullable on the database level. The problem is that with a primitive field (*i.e.* `boolean`) you can't really tell if a certain value is supposed to be a `NULL` or a zero (`0`). Hence OP's question. – Priidu Neemre Apr 28 '16 at 07:44
11

I know the question is old but I will leave this for future references.

You have two options to override a Getter/Setter in your class.

One is the answer from First Option response

The other option is to simply define the getter/setter method and lombok will not automatically produce the specified method.

I tested it myself and it seems to work fine:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ProductResponse {

    private UUID id;
    private String supplierId;
    private String sku;
    private String name;
    private String brand;
    private String imgUrl;
    private String description;
    private BigDecimal price;
    private Float quantity;
    private String unit;
    //@Getter(AccessLevel.NONE) //This means @Data will not produce a getter for this field so have to explicitly define it
    private Set<ProductTag> tags;

    //Here we override @Data getter with a different getter (return is different type)
    public List<UUID> getTags() {
    return     tags.stream().map(ProductTag::getId).collect(Collectors.toList());
    }
}

Here is also a reference from the development team comments: Lombok's developer comment

In my example I'm using the "override" feature for a Collection type but this can be used for any other type like Boolean in your case.

  • 4
    In your case it "works" because the name of your custom getter matches the name that lombok is supposed to generate. OP asks what to do if **the name doesn't match**. In their case they are `isXXX()` and `getXXX()`. – Sasha Shpota Dec 27 '18 at 14:01
  • Good catch! Thanks for highlighting this which I overlooked in the question. – Rafael R. S. Robles Jan 04 '19 at 09:49
2

what is the name of boolean field? according to the lombok doc:

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)

lombok will generate getter with name isXXX for your boolean field

Septem
  • 3,614
  • 1
  • 20
  • 20
  • 14
    You seem to be confused. The OP is obviously aware that a `boolean` field's getter will be named `isXXX()` by default; the problem is that (s)he wants that naming convention to be used for a `Boolean` field (`Boolean` being the object wrapper type for `boolean`, like what `Integer` is to `int`). (The page that you link to actually states this explicitly: "Any variation on `boolean` will *not* result in using the `is` prefix instead of the `get` prefix; for example, returning `java.lang.Boolean` results in a `get` prefix, not an `is` prefix.") – ruakh Aug 09 '13 at 03:59
1

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.

George Kargakis
  • 4,940
  • 3
  • 16
  • 12