Lombok在多模块项目中的@RequiredArgsConstructor不被保留。

huangapple go评论70阅读模式
英文:

Lombok @RequiredArgsConstructor in multi-module project is not preserved

问题

Is @RequiredArgsConstructor preserved accross modules?

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

  • dependency-A
  • sprint-boot-app

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 spring-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

英文:

Is @RequiredArgsConstructor preserved accross modules?

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

  • dependency-A
  • sprint-boot-app

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

答案1

得分: 1

@RequiredArgsConstructor 会为每个需要特殊处理的字段生成一个参数的构造函数。所有未初始化的 final 字段都会获得一个参数,以及所有标记为 @NonNull 但在声明时未初始化的字段。对于那些标记为 @NonNull 的字段,还会生成显式的空值检查。如果任何标记为 @NonNull 的字段的参数包含 null,构造函数将抛出 NullPointerException。参数的顺序与字段在类中出现的顺序相匹配。

英文:

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.

答案2

得分: 1

Lombok注解无法继承。它们是在编译时添加代码到被注解的类中,并且仅限于被注解的类。即使您在同一模块中创建了一个子类,代码也不会传播。

您需要使用@RequiredArgsConstructor为每个类进行注解。

英文:

Lombok annotations cannot be inherited.<br />
They are compile time directives that add code to the annotated class and only the annotated class.<br />
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.

huangapple
  • 本文由 发表于 2020年9月17日 22:16:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63940034.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定