3

I'm trying to figure out if there is a way to set the getter method name manually using lombok. Consider the following example:

@Getter
@Builder(setterPrefix = "with")
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class Context {

    @Builder.Default
    private final boolean logReceivedMessages = false;
  
    ... many other fields ...

}

With the above example you are able to build the context like so:

context = Context.builder().withLogReceivedMessages(true/false).build;

and then use it as

if(context.isLogReceivedMessages()) {
   XYZ
} else {
   zyx
}

The name of the generated method is not really how I would word it and was wondering if there was a way to customize it? Is there an annotation which would allow me to name it to something like shouldLogReceivedMessages() instead of isLogReceivedMessages ? I can't seem to find that in the docs.

tftd
  • 16,203
  • 11
  • 62
  • 106
  • 1
    Refer https://stackoverflow.com/questions/42669129/edit-lombok-getter-method-name-for-boolean-member-having-prefix-has – Teddy Jun 15 '22 at 13:43
  • is -> should? No. There is config to remove the `get` part and that's all. So `getFoo()` would just be `foo()` – Michael Jun 15 '22 at 13:44
  • 1
    The point of Lombok is pretty much to generate JavaBeans-style getters/setters (boolean getters are defined to use the naming `is`). If you want an entirely different naming convention, then it no longer is JavaBeans getters/setters (nor record-style properties), and thus falls outside the scope of what Lombok does. – Mark Rotteveel Jun 15 '22 at 14:01
  • If the `is` prefix of a boolean typed getter does not sound great, then maybe the field name itself needs to be thought over! – Seelenvirtuose Jun 15 '22 at 14:07
  • @Seelenvirtuose : Well, I would argue that `is` is actually not always the best prefix for a getter method. You could have `can` (`canFly()`, as opposed to `isFly()` -- I mean -- is it really a fly, or can it fly?), the same goes for `has`, `should` and other conditional words, depending on the context. And -- sure, as programmers, we should be aware of the linguistic limitations of a language, but should we always be constrained just for the sake of a naming convention that doesn't always suit us? – carlspring Jun 15 '22 at 14:49
  • @carlspring The JB naming conventions were created for tools, not humans. – Dave Newton Jun 15 '22 at 21:01
  • @carlspring What about a property `ableToFly`? ;-) – Seelenvirtuose Jun 16 '22 at 08:09

1 Answers1

2

If this is the only method which needs name change, I would suggest to add another getter by your own inside Context; it will override lombok's getter.

But if you need lot more customisation in different methods then refer SO post mentioned by Teddy.

Ashish Patil
  • 4,428
  • 1
  • 15
  • 36
  • Thanks for the answer! I will be going for Teddy's solution for now, but yours was what I was thinking initially - marking the `Getter` as `private` and then adding my own. – tftd Jun 15 '22 at 14:48