Lombok @SuperBuilder on abstract class with generic cause error: incompatible types

Lombok @SuperBuilder on abstract class with generic cause error: incompatible types

我有通用类型的抽象 class。它具有此处建议的抽象 toBuilder 方法:

这里是摘要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();
    }
}

子类型:

@SuperBuilder(toBuilder = true)
@EqualsAndHashCode(callSuper = true)
public class PointsExpirationByEarnedDatePolicy extends TenantConfItem<ExpirationByEarnedDate> implements Singleton {

}

我在这里使用它的构建器:

val policyBuilder = PointsExpirationByEarnedDatePolicy.builder()
                .version(0L)
                .configData(new ExpirationByEarnedDate(retentionDays));

但我收到配置数据错误:

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 ?

我猜这是因为父构建器有 3 个参数和泛型 (TenantConfItemBuilder<T, ?, ?>) 而子构建器有 2 个 (PointsExpirationByEarnedDatePolicyBuilder<?, ?>)。 有解决方法吗?

@SuperBuilder(toBuilder=true) 支持泛型 类 并在子类中细化来自超类的类型参数。所以你的 类 和他们的注释没问题。

但是,lombok 有时会在推断 val 的正确类型时出现问题,尤其是在涉及类型参数时。

解决方法是将val替换为实际类型PointsExpirationByEarnedDatePolicyBuilder<?, ?>