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;
}
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
}
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With