akop
akop

Reputation: 7855

Lombok Customize SuperBuilder

I have two classes like this:

@Builder
public class Parent {
    final int a;
    final int b;

    public class static ParentBuilder {
        public ParentBuilder setAllTo(final int value) {
           return a(value).b(value);
        }
    }
}

public class Child extends Parent {
   final in c;

   @Builder(builderMethodName = "childBuilder")
   public Child(final int a, final int b, final int c) {
      super(a, b);
      this.c = c;
   }
}

My classes are growing up and got more and more fields. And this is a reson to use the @SuperBuilder. But how can I add customized builder methods?

The same way dosent work. I tried this way:

@SuperBuilder
public abstract class Parent { //yes, I want a abstract parent
    final int a;
    final int b;

    public class static ParentBuilder {
        public ParentBuilder setAllTo(final int value) {
           return a(value).b(value);
        }
    }
}


@SuperBuilder
public class Child extends Parent {
   final in c;

}

Edit

Its not possible yet. When I try to do it the same way, then I got a exception: @SuperBuilder does not support customized builders. Use @Builder instead.
The override is a inner class like this:

public abstract static class ParentBuilder<C extends ParentBuilder, B extends Parent.ParentBuilder<C, B>> {
    // custom imlementations here
}

Upvotes: 21

Views: 16931

Answers (3)

skuzniarz
skuzniarz

Reputation: 53

It is worth mentioning, that if you want to customize child class builder, the custom builder has to inherit from the parent builder (whether or not the parent builder was customized):

@SuperBuilder
public abstract class Parent {
    ...
}

@SuperBuilder
public class Child extends Parent {
    ...
    public static class ChildBuilder<C extends Child, B extends ChildBuilder<C, B>> extends ParentBuilder<C, B> {
        ...
    }
}

Upvotes: 3

Anubhav
Anubhav

Reputation: 265

I recently tried customizing @SuperBuilder using Lombok 1.18.8 and IntelliJ, and it worked fine. The only issue I faced was, I lost ability to use toBuilder flag in SuperBuilder - @SuperBuilder(toBuilder=true).

Below is the code to override @SuperBuilder methods.

public static abstract class ChildBuilder<C extends Child, B extends ChildBuilder<C, B>>
        extends ParentBuilder<C, B> {

    private LocalDate date;

    public B date(String dateStr) {
        this.date = LocalDate.parse(dateStr);
        return self();
    }
}

I added my working code here: Customize SuperBuilder in Lombok

Upvotes: 22

Jan Rieke
Jan Rieke

Reputation: 8052

When @SuperBuilder was introduced in 1.18.2, customising it was not possible. If you try, Lombok 1.18.2 gives you the error message SuperBuilder does not support customized builders.

However, Lombok 1.18.4 added limited customisation possibilities of @SuperBuilder. (It's limited because you cannot modify setter methods, but you can add your own methods and modify build() and builder().)

The generated @SuperBuilder code is quite complex and differs from @Builder. To avoid accidently messing up the generics-loaded builder code, you should start by copying the builder class header from the delombok output. In your case (adding a new setter method), customize the abstract builder class ParentBuilder (and not the ParentBuilderImpl). Have a look at the delomboked code to find out how your setter should be defined, especially the return type.

This is the customized builder code for your example:

public abstract static class ParentBuilder<C extends Parent, B extends ParentBuilder<C, B>> {
    public B setAllTo(final int value) {
       return a(value).b(value);
    }
}

With Lombok 1.18.4, this compiles and works as expected.

Upvotes: 9

Related Questions