Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customizing Builder generated by Lombok

Tags:

java

lombok

I have defined a class with a builder and now I would like to limit boilerplate code using Lombok's @Builder annotation.

public class ClientApp {

    private UUID clientId;

    ClientApp(UUID clientId) {
      this.clientId = clientId;
    }

    public static Builder builder() {
      return new Builder();
    }

    public static class Builder {

      private UUID clientId;

      public Builder clientId(String clientId) {
        return clientId(UUID.fromString(clientId));
      }

      public Builder clientId(UUID clientId) {
        this.clientId = clientId;
        return this;
      }

      public ClientApp build() {
        return new ClientApp(this.clientId);
      }
    }

    public Builder clientId(String clientId) {
        return clientId(UUID.fromString(clientId));
    }
}

However, the annotation will not generate clientId(String) method, only clientId(UUID). How can I generate it with Lombok?

like image 592
dzieciou Avatar asked Jul 01 '26 15:07

dzieciou


1 Answers

Well, lombok will not generate this for you but you can use @Builder with and have a ClientAppBuilder class containing the one method that accepts a String and routes it to the other, to be generated, method. Possibly you need to mark your method with @Tolerate, otherwise Lombok will not generate the UUID accepting method.

Disclosure: I am a Lombok developer.

like image 167
Roel Spilker Avatar answered Jul 03 '26 04:07

Roel Spilker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!