I'm currently facing a problem with Lombok's @SuperBuilder
annotation and generics. I have an abstract
parent and a final
child class. When creating the child using the builder, passing values of the correct type leads to compile errors.
The simplified classes and interfaces
@SuperBuilder(toBuilder = true)
public abstract class Parent<T> {
private SomeGenericClass<?, Parent<T>> someGenericClass;
private T value;
}
interface SomeInterface<D> {
}
class SomeGenericClass<D, S> implements SomeInterface<D> {
}
@SuperBuilder(toBuilder = true)
final class Child<T> extends Parent<T> {
}
The test code
public class SuperBuilderTest {
public static void main(String[] args) {
SomeGenericClass<String, Parent<Long>> someGenericClass = new SomeGenericClass<>();
Child<Long> child = Child.builder()
.someGenericClass(someGenericClass) // error: The method someGenericClass(SomeGenericClass<?,Parent<Object>>) in
// the type Parent.ParentBuilder<Object,capture#1-of ?,capture#2-of ?> is
// not applicable for the arguments (SomeGenericClass<String,Parent<Long>>)
.value(10L) // error: Type mismatch: cannot convert from capture#1-of ? to Child<Long>
.build();
}
}
If works, as soon as the child itself no longer has a generic parameter. So having the child like this, everything compiles and works perfectly fine:
@SuperBuilder(toBuilder = true)
final class Child extends Parent<Long> {
}
Am I doing something wrong or is it not possible to have generic final
classes when using the @SuperBuilder
?