tillias
tillias

Reputation: 1275

Lombok @RequiredArgsConstructor in multi-module project is not preserved

Is @RequiredArgsConstructor preserved accross modules?

I have a very simple project structure (2 maven modules):

In dependency-A I declare following class:

@RequiredArgsConstructor
@RestController
@Validated
@Slf4j
public abstract class ControllerBase {}

Now in spring-boot-app:

@RequestMapping("/api/v1")
public class ConcreteController extends ControllerBase {

    private final SomeDependency dependency

After this IntelliJ says that

Variable dependency might not been initialized

If I check decompiled version of ControllerBase from sprint-boot-app module:

@RestController
@Validated
public class ControllerBase {
    private static final Logger log = LoggerFactory.getLogger(MyController.class);

    public ControllerBase () {
    }
}

Is it restriction of Lombok or I should configure maven-compiler plugin somehow, so @RequiredArgsConstructor will be preserved? Please advise

Upvotes: 0

Views: 1098

Answers (2)

Nicolaas Winter
Nicolaas Winter

Reputation: 76

Lombok annotations cannot be inherited.
They are compile time directives that add code to the annotated class and only the annotated class.
Even if you were creating a child class in the same module it would not propagate.

You will have to annotate each class with @RequiredArgsConstructor.

Upvotes: 1

LalithK90
LalithK90

Reputation: 1028

copy past from Lombok

@RequiredArgsConstructor generates a constructor with one parameter for each field that requires special handling. All non-initialized final fields get a parameter, as well as any fields that are marked as @NonNull that aren't initialized where they are declared. For those fields marked with @NonNull, an explicit null check is also generated. The constructor will throw a NullPointerException if any of the parameters intended for the fields marked with @NonNull contain null. The order of the parameters match the order in which the fields appear in your class.

Upvotes: 1

Related Questions