I am creating inmutable classes with inheritance but whenever I try to use @With
in a Child class attribute, I receive an error because the constructor that Lombok uses in the @With
does not include the parent attributes. Example:
Parent class
@EqualsAndHashCode(callSuper = true)
@Getter
public abstract class Order extends SelfValidating<Order> {
@Valid
@NotBlank(message = "orderNumber must not be empty")
private final String orderNumber;
private final String orderId;
private final String externalOrderId;
protected Order(String orderNumber, String orderId, String externalOrderId) {
this.orderNumber = orderNumber;
this.orderId = orderId;
this.externalOrderId = externalOrderId;
}
}
Child class
@EqualsAndHashCode(callSuper = true)
@Value
public class ChildOrder extends Order {
@NotBlank(message = "Invoice number should not be blank")
@Valid
@With
String invoiceNumber;
@Builder
public ChildOrder(
String orderNumber,
String orderId,
String externalOrderId,
String invoiceNumber) {
super(orderNumber, orderId, externalOrderId);
this.invoiceNumber = invoiceNumber;
super.validateSelf();
}
}
This is throwing an exception when I run the code:
error: constructor ChildOrder in class ChildOrder cannot be applied to given types;
@With
^
required: String,String,String,String
found: String
reason: actual and formal argument lists differ in length
This is because the Lombok's implementation of @With
is as follow:
public ChildOrder withInvoiceNumber(@NotBlank(message = "Invoice number should not be blank") @Valid String invoiceNumber) {
return this.invoiceNumber == invoiceNumber ? this : new ChildOrder(invoiceNumber);
}
But the constructor new ChildOrder(invoiceNumber)
does not exist, since I am using a super
to include parent's attributes.
So far, I implemented the With
pattern manually and I am including all the attributes for the parent, but I would like to know if there is a way to keep using Lombok for this?
Aucun commentaire:
Enregistrer un commentaire