Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lombok @Value @NonFinal inheritance

Tags:

java

lombok

I'm trying to chain several @Value @NonFinal classes without creating any constructors.

Expecting that lombok chooses to generate a constructor that call super with all the arguments that match.

Is there any way of achieving this? It seems to break with an error that doesn't make sense because the father class have such constructor given by the annotation. I tried this with classes in different files, same behaviour.

Example:

public class TestLombok {

@Value
@NonFinal
class A {
    @NonNull Integer a;
}

@Value
@NonFinal
class B extends A {
    String b;
}

}

Error:(12, 5) java: constructor A in class TestLombok.A cannot be applied to given types; required: java.lang.Integer found: no arguments reason: actual and formal argument lists differ in length

like image 898
Juan Avatar asked Apr 21 '18 18:04

Juan


People also ask

What is @value annotation in Lombok?

Lombok Value annotation (@Value) is used when creating Immutable classes. All Lombok generated fields are made private and final by default, and setters are not generated. The class itself is also made final by default.

What is the difference between @data and @value Lombok?

The big difference between @Value and @Data annotation is that @Value is mainly used to create immutable objects. @Value is a also an all-in-one annotation that combines: @Getter, @AllArgsConstructor, @ToString and @EqualsAndHashCode and @FieldDefaults(makeFinal = true, level = AccessLevel.

What is difference between @builder and @SuperBuilder?

The @SuperBuilder annotation produces complex builder APIs for your classes. In contrast to @Builder , @SuperBuilder also works with fields from superclasses. However, it only works for types. Most importantly, it requires that all superclasses also have the @SuperBuilder annotation.


1 Answers

Unfortunately, this is not possible.

Finding out what fields or methods a parent class has requires resolution. The moment lombok needs to generate methods, resolution is not possible, since it would change the outcome of the available fields and methods.

Disclosure: I am a lombok developer.

like image 53
Roel Spilker Avatar answered Sep 22 '22 08:09

Roel Spilker