how to Call super constructor in Lombok

user2067797 picture user2067797 · Apr 20, 2015 · Viewed 111.5k times · Source

I have a class

@Value
@NonFinal
public class A {
    int x;
    int y;
}

I have another class B

@Value
public class B extends A {
    int z;
}

lombok is throwing error saying it cant find A() constructor, explicitly call it what i want lombok to do is to give annotation to class b such that it generates the following code:

public class B extends A {
    int z;
    public B( int x, int y, int z) {
        super( x , y );
        this.z = z;
    }
}

Do we have an annotation to do that in Lombok?

Answer

Roel Spilker picture Roel Spilker · Apr 21, 2015

This is not possible in Lombok. Although it would be a really nice feature, it requires resolution to find the constructors of the super class. The super class is only known by name the moment Lombok gets invoked. Using the import statements and the classpath to find the actual class is not trivial. And during compilation you cannot just use reflection to get a list of constructors.

It is not entirely impossible but the results using resolution in val and @ExtensionMethod have taught us that is it hard and error-prone.

Disclosure: I am a Lombok developer.