简体   繁体   English

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

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

I have abstract class with generic type.我有通用类型的抽象 class。 it has abstract toBuilder method as suggested here: Using Lombok @SuperBuilder annotation with toBuilder on an abstract class?它具有此处建议的抽象 toBuilder 方法: Using Lombok @SuperBuilder annotation with toBuilder on an abstract class?

here is the abstract class:这是摘要 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:但我收到 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<?, ?> ).我想这是因为父构建器有 3 个 arguments 和通用的 ( TenantConfItemBuilder<T, ?, ?> ) 而子构建器有 2 个 ( PointsExpirationByEarnedDatePolicyBuilder<?, ?> )。 Any idea for workaround?任何解决方法的想法?

@SuperBuilder(toBuilder=true) supports generic classes and refining the type parameter from the superclass in the subclass. @SuperBuilder(toBuilder=true)支持泛型类并在子类中细化来自超类的类型参数。 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.但是,lombok 有时会在推断val的正确类型时出现问题,尤其是在涉及类型参数时。

The solution is to replace val with the actual type PointsExpirationByEarnedDatePolicyBuilder<?, ?> .解决方案是将val替换为实际类型PointsExpirationByEarnedDatePolicyBuilder<?, ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM