Lombok Builder无法运行

时间:2018-06-03 14:04:59

标签: java lombok

我有一个基类

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class FurnitureBillingRequest implements JSONAble<FurnitureBillingRequest> {

    private static final Gson gsonSerializer;

    static {
        gsonSerializer = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
    }

    @Expose
    protected String name;

    @Expose
    protected long date;

    @Expose
    protected String phone;

    @Expose
    protected List<BillItemRO> items;

    @Expose
    protected int totalAmount;

    @Expose
    protected int amountPaid;

    @Expose
    protected String remark;

    @Expose
    protected String address;

    public FurnitureBillingRequest(String name, long date, String phone, int totalAmount,
                                   int amountPaid, String remark, String address) {
        this(name, date, phone, new ArrayList<>(), totalAmount, amountPaid, remark, address);
    }

    public String toJson() {
        return gsonSerializer.toJson(this);
    }

    public FurnitureBillingRequest toObject(String json) {
        return gsonSerializer.fromJson(json, FurnitureBillingRequest.class);
    }

    @Override
    public boolean equals(Object that) {

        if (!(that instanceof FurnitureBillingRequest)) {
            return false;
        }

        FurnitureBillingRequest thatFurnitureBillingRequest = (FurnitureBillingRequest) that;

        if (!StringUtils.equals(this.name, thatFurnitureBillingRequest.name)) return false;

        if (this.date != thatFurnitureBillingRequest.date) return false;

        if (!StringUtils.equals(this.phone, thatFurnitureBillingRequest.phone)) return false;

        if (!this.items.equals(thatFurnitureBillingRequest.items)) return false;

        if (this.totalAmount != thatFurnitureBillingRequest.totalAmount) return false;

        if (this.amountPaid != thatFurnitureBillingRequest.amountPaid) return false;

        return true;
    }
}

一个子类扩展如下

@Getter
@Setter
@NoArgsConstructor
public class UpdateBillRequest extends FurnitureBillingRequest {

    private static final Gson gsonSerializer;

    static {
        gsonSerializer = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
    }

    @Expose
    private String uid;

    @Builder
    public UpdateBillRequest(String uid, String name, long date, String phone, List<BillItemRO> items, int totalAmount, int amountPaid, String remark, String address) {
        super(name, date, phone, items, totalAmount, amountPaid, remark, address);
        this.uid = uid;
    }

    public UpdateBillRequest(String uid, String name, long date, String phone, int totalAmount, int amountPaid, String remark, String address) {
        super(name, date, phone, totalAmount, amountPaid, remark, address);
        this.uid = uid;
    }

    public String toJson() {
        return gsonSerializer.toJson(this);
    }

    public FurnitureBillingRequest toObject(String json) {
        return gsonSerializer.fromJson(json, FurnitureBillingRequest.class);
    }
}

我写了一个单元测试来检查json是否创建得很好。

public class UpdateBillRequestTest {

    @Test
    public void toJson() {

        UpdateBillRequest request = UpdateBillRequest.builder()
                .date(1234567868L)
                .uid("9i09909-90i90-8u90")
                .name("kuldeep")
                .phone("9535580366")
                .build();

        System.out.println(request.toJson());
    }

    @Test
    public void toObject() {
    }
}

但是当我尝试编译代码时,它最终会导致错误

[ERROR] /Users/kuldeep/Work/Repositories/billing/BillingModel/src/main/java/com/tarkshala/services/lambda/model/UpdateBillRequest.java:[24,5] builder()in com。 tarkshala.services.lambda.model.UpdateBillRequest无法覆盖com.tarkshala.services.lambda.model.FurnitureBillingRequest中的builder() [ERROR]返回类型com.tarkshala.services.lambda.model.UpdateBillRequest.UpdateBillRequestBuilder与com.tarkshala.services.lambda.model.FurnitureBillingRequest.FurnitureBillingRequestBuilder不兼容

我不知道出了什么问题?

1 个答案:

答案 0 :(得分:1)

这是Java的一个限制。尝试编译此代码:

class Foo {
    public static FooBuilder builder() {
        return new FooBuilder();
    }
}
class Bar extends Foo {
    public static BarBuilder builder() {
        return new BarBuilder();
    }
}
class FooBuilder {
    public Foo build() {
        return new Foo();
    }
}
class BarBuilder {
    public Bar build() {
        return new Bar();
    }
}

经典构建器模式,但我省略了所有属性。

现在eclipse告诉我&#34;返回类型与Foo.builder()&#34;不兼容。 Bar.builder()

您不能使用不兼容的返回类型的重载方法。这甚至适用于静态方法。