I have abstract class with generic type. it has abstract toBuilder method as suggested here: Using Lombok @SuperBuilder annotation with toBuilder on an abstract class?
@Data
@SuperBuilder(toBuilder = true)
public abstract class TenantConfItem<T extends DeepCloneable<T>> {
private final URN urn;
private final Long version;
private final T configData;
public abstract TenantConfItemBuilder<T, ?, ?> toBuilder();
public TenantConfItem<T> copy() {
return this.toBuilder().build();
}
}
the subtype:
@SuperBuilder(toBuilder = true)
@EqualsAndHashCode(callSuper = true)
public class PointsExpirationByEarnedDatePolicy extends TenantConfItem<ExpirationByEarnedDate> implements Singleton {
}
here I'm using its builder:
val policyBuilder = PointsExpirationByEarnedDatePolicy.builder()
.version(0L)
.configData(new ExpirationByEarnedDate(retentionDays));
but I get an error for the configData:
error: incompatible types: CAP#1 cannot be converted to ?
.configData(new ExpirationByEarnedDate(retentionDays));
^
where CAP#1,CAP#2 are fresh type-variables:
CAP#1 extends PointsExpirationByEarnedDatePolicyBuilder<CAP#2,CAP#1> from capture of ?
CAP#2 extends PointsExpirationByEarnedDatePolicy from capture of ?
I guess it is because the parent builder has 3 arguments with the generic (TenantConfItemBuilder<T, ?, ?>) and the sub one has 2 (PointsExpirationByEarnedDatePolicyBuilder<?, ?>).
Any idea for workaround?
@SuperBuilder(toBuilder=true) supports generic classes and refining the type parameter from the superclass in the subclass. So your classes and their annotations are fine.
However, lombok sometimes has problems inferring the correct type for val, especially when type parameters are involved.
The solution is to replace val with the actual type PointsExpirationByEarnedDatePolicyBuilder<?, ?>.