segfault
segfault

Reputation: 83

@RequiredArgsConstructor does not recognize inherited fields

In the following example:

@RequiredArgsConstructor
class Child extends Parent {
}

abstract class Parent {
    @NonNull int field;
}

The Lombok annotation @RequiredArgsConstructor does not recognize the field "field" as part of the Child class, thus not adding it to the generated constructor. Here's the Delombok output for the Child class:

class Child extends Parent {
    public Child() {
    }
}

Is there any workaround to fix this without scrapping Lombok altogether?

Upvotes: 1

Views: 3226

Answers (2)

Jan Rieke
Jan Rieke

Reputation: 8102

That's not possible with lombok at the moment. To do so, lombok would have to resolve the parent class, which is not possible at the time when lombok is invoked by the compiler.

As a workaround, you could use @SuperBuilder (on all classes in your hierarchy) and use that to construct your instances. A @SuperBuilder also contains setter methods for all fields from superclasses.

Upvotes: 0

Eric Lingamfelter
Eric Lingamfelter

Reputation: 31

I do not believe there are plans for Lombok to implement this:

https://github.com/projectlombok/lombok/issues/3002

Upvotes: 1

Related Questions