How to Call Super Constructor in Lombok

How to ensure that child class will call the correct super constructor in lombok?

The order of the parameters of an @AllArgsConstructor matches the order of the fields in the source code. So right now you are safe.

However, if you later modify the order of the fields in A (or rename them), you will get wrong assignments (or wrong parameter names in your builder), but no compiler error.

But there is an easy way out: Use @SuperBuilder, and remove @Data. (Note that you need @SuperBuilder on all classes in your hierarchy.) You won't get @AllArgsConstructors, changes to the field order are irrelevant, and name changes are immediately reflected in the builder classes.

If that is not possible, your only choice is to put a big fat warning comment into A advising later coders not to mess around with the names and order.

Generate constructor for required super fields

The @…Constructor annotations will not explicitly call a constructor, so they all rely on a default constructor doing a proper job. So, no, you cannot convince Lombok to create these constructors for you.

The closest you can get it is to either:

  1. Provide a default constructor (no arguments) in EmailData that is protected and assigns some reasonable value to recipients.
  2. Write the required-args constructor for PasswordRecoveryEmail yourself.

Inheritance often is not quite covered by Lombok in my experience.

Lombok subclass constructor @Autowired

If you use @SuperBuilder, no constructor is created.
In Spring in order to inject OneRepository and SuperRepository, you need a constructor having these two objects as parameters.

Using @AllArgConstructor, Lombok creates the constructor just for the members of that class, not considering the super class.
Why? You can read the answer of a Lombok developer here.

In the end, your first solution is a unique solution if you want to have a super class.

NoArgsConstructor and AllArgsConstructor annotation on Child class for parent constructors

In your Child class you have no attributes, so @NoArgsConstructor and @AllArgsConstructor are the same and the error occurs.


If you wanted an all-args constructor that would pass the properties to the parent class's all-args constructor, you'd have to write it yourself; Lombok won't generate it.

@SuperBuilder
@NoArgsConstructor
public class Child extends Parent {
public Child(String propertyA, String propertyB) {
super(StringUtils.upperCase(propertyA), propertyB);
}

@Override
public void setPropertyA(String propertyA) {
this.propertyA(StringUtils.upperCase(propertyA));
}
}

Cant find Lombok constructor for derived class

Lombok @AllArgsConstructor doesn't call super() so you need to create constructor manually for derived classes

Lombok: Annotation that generates only few paramteres in Constructor

Currently, there is no such feature and the only way to generate a constructor for the selected fields is by using @RequiredArgsConstructor and labeling only and only such fields as final.

@RequiredArgsConstructor
class Foo {

private final String a;
private final double b;
private int c;
}
new Foo("String", 1.0);

However, you don't want to design the class and its fields to satisfy the needs that Lombok doesn't provide.



Related Topics



Leave a reply



Submit