Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lombok Customize SuperBuilder

Tags:

java

lombok

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
}
like image 864
akop Avatar asked Nov 01 '18 10:11

akop


People also ask

Is SuperBuilder still experimental?

Solving this problem is probably possible but seems at first glance quite costly compared to the noise it aims to kill. Especially knowing that “SuperBuilder” is still considered as an experimental feature.

What is difference between @builder and SuperBuilder?

The @SuperBuilder annotation produces complex builder APIs for your classes. In contrast to @Builder , @SuperBuilder also works with fields from superclasses. However, it only works for types. Most importantly, it requires that all superclasses also have the @SuperBuilder annotation.

What is Lombok SuperBuilder?

The SuperBuilder annotation creates a so-called 'builder' aspect to the class that is annotated with @SuperBuilder , but which works well when extending. It is similar to @Builder , except it is only legal on types, is less configurable, but allows you to extends other builder-able classes.

Does @builder require AllArgsConstructor?

Martin Grajcar. Your @Builder needs an @AllArgsConstructor (add it; feel free to make it private). You received this message because you are subscribed to the Google Groups "Project Lombok" group.


1 Answers

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.

like image 114
Jan Rieke Avatar answered Sep 18 '22 17:09

Jan Rieke