Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Lombok @SuperBuilder on Abstract classes with final fields

Given the following classes with the Lombok annotations @Data and @SuperBuilder

@Data
@SuperBuilder
public abstract class Parent {

    protected final String userId;
    protected final Instant requestingTime;
}

@Data
@SuperBuilder
public class Child extends Parent {

    private final Instant beginningDate;
    private final Instant endingDate;
    private final Collection<String> fields;
}

I am getting the following error appearing over the @Data annotation in the Child class:

Implicit super constructor Parent() is undefined. Must explicitly invoke another constructor.

Is there a way to configure a non-default constructor on the Child class's @Data annotation in order to have all final fields on both the Child and Parent classes initialized when invoking the Builder?

I have tried a few different combinations of the @Data, @Getter, @Setter annotations with the @SuperBuilder annotation on both the child and parent classes, but haven't found a working solution yet. I am using Lombok 1.18.10.

For reference, this question is related

EDIT

This is effectively the constructor that Lombok should be constructing and invoking on the SuperBuilder.build() operation.

public Child(
    final String userId,
    final Instant requestingTime,
    final Instant beginningDate,
    final Instant endingDate,
    final Collection<String> fields) {

    super(userId, requestingTime);
    this.beginningDate = beginningDate;
    this.endingDate = endingDate;
    this.fields= fields;
}

As requested, this is how I would expect to invoke the builder on the Child object.

final Child child = Child.Builder()
                         .userId(<value>)
                         .requestingTime(<value>)
                         .beginningDate(<value>)
                         .endingDate(<value>)
                         .fields(<value>)
                         .build();
like image 521
burtmacklin16 Avatar asked Feb 20 '20 14:02

burtmacklin16


People also ask

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.

Is SuperBuilder still experimental?

Especially knowing that “SuperBuilder” is still considered as an experimental feature.

Can we use @data and @builder together?

Lombok @Data and @Builder together If you use @Data annotation alone, public required-args constructor is generated. If you are using @Data and @Builder annotations together, all-args constructor (Package access level) is generated.

Does @builder require AllArgsConstructor?

Martin Grajcar Your @Builder needs an @AllArgsConstructor (add it; feel free to make it private).


Video Answer


1 Answers

AFAIK, @Data generates a @NoArgsConstructor, which is just wrong. Actually, @Data is wrong per se, as it's meant for mutable classes; @Value would be better, but it can't deal with the super constructor either.

So remove @Data, add @Getter, @EqualsAndHashCode, @ToString and whatever you need. Don't forget to add callSuper=true in the subclass.


This is effectively the constructor that Lombok should be constructing and invoking on the SuperBuilder.build() operation.

public Child(
    final String userId,
    final Instant requestingTime,
    final Instant beginningDate,
    final Instant endingDate,
    final Collection<String> fields) {

    super(userId, requestingTime);
    this.beginningDate = beginningDate;
    this.endingDate = endingDate;
    this.fields= fields;
}

No, that's not how SuperBuilder works. This is actually Lombok can't do as it can't see the super fields. Instead, the builder uses something like

public Child(ChildBuilder b) {    
    super(b);
    this.beginningDate = b.beginningDate;
    this.endingDate = b.endingDate;
    this.fields= b.fields;
}

You can believe what Jan Rieke says, he wrote it.

like image 91
maaartinus Avatar answered Oct 08 '22 01:10

maaartinus