简体   繁体   English

如何告诉 lombok 生成的 getter 是 @Override

[英]How to tell to lombok the generated getter is an @Override

I'm using lombok in my project and I have an interface :我在我的项目中使用 lombok 并且我有一个界面:

public interface MyInterface{

    Object getA()
}

And a class还有一个班

@Getter
public class MyClass implements MyInterface{

      private Object a;

      private Object b

}

And i've checked the generated class and the method generated in the class is not @Override而且我检查了生成的类,类中生成的方法不是@Override

I'm wondering how to add this annotation ?我想知道如何添加此注释? And what are the consequences of a missing @Override ?缺少@Override的后果是什么?

It's maybe an another question but this code is analyzed by sonarqube and sonar say that private field a is never used.这可能是另一个问题,但此代码由声纳分析,声纳说从未使用过私有字段 a。

I've already seen the subject about sonarqube + lombok = false positives我已经看过关于sonarqube + lombok = 误报的主题

But In my case b doesn't create a false positive.但在我的情况下, b 不会产生误报。 So I don't think this is directly related所以我不认为这有直接关系

Do you see a solution to avoid this problems without reimplement getA() ?您是否看到无需重新实现 getA() 即可避免此问题的解决方案?

You can use the onMethod attribute of the annotation to add any annotation you want to the generated method.您可以使用注释的onMethod属性将您想要的任何注释添加到生成的方法中。

@Getter
public class MyClass implements MyInterface{
    @Getter(onMethod = @__(@Override))
    private Object a;
    private Object b
}

As a matter of style in this case, I would probably move the class-level @Getter to the other field.在这种情况下,作为风格问题,我可能会将类级别的@Getter移动到另一个字段。 If there were c , d , e which should all use normal (no annotation) @Getter logic, I would leave it as above.如果有cde都应该使用正常(无注释)@Getter 逻辑,我会保留如上。

public class MyClass implements MyInterface{
    @Getter(onMethod = @__(@Override))
    private Object a;
    @Getter
    private Object b
}

You may also want to enable Lombok's generated annotations .您可能还想启用 Lombok 的生成注释 It will prevent some tools (coverage, static analysis, etc) from bothering to check Lombok's methods.它将防止某些工具(覆盖、静态分析等)打扰检查 Lombok 的方法。 Not sure if it will help in this case, but I pretty much have it enabled in all of my projects.不确定在这种情况下它是否会有所帮助,但我几乎在所有项目中都启用了它。

lombok.addLombokGeneratedAnnotation = true

You can add @Override to a particular field by using @Getter 's onMethod .您可以添加@Override通过使用一个特定的领域@GetteronMethod

eg例如

@Getter(onMethod = @__(@Override))
private Object a;

@Override has no affect on the code itself . @Override 对代码本身没有影响 It simply informs the compiler that the method is overriding a method from its superclass.它只是通知编译器该方法正在覆盖其超类中的方法。 The compiler will fail the build if the method does not override a method from a superclass.如果该方法没有覆盖超类中的方法,编译器将导致构建失败。

A more general way to prevent false positives detections of missing @Override by Error Prone (or similar tools) is to configure Lombok to annotate its generated code with @lombok.Generated .防止通过Error Prone (或类似工具)对缺少 @Override 进行误报检测的更通用方法是配置 Lombok 以使用@lombok.Generated注释其生成的代码。 This can be done - globally - by adding the following entry to Lombok's lombok.config :这可以通过将以下条目添加到 Lombok 的lombok.config来全局完成:

lombok.addLombokGeneratedAnnotation = true

Note that this is not a universal solution - the relevant tool needs to respect @lombol.Generated (or better, any @*.Generated ) annotation.请注意,这不是一个通用的解决方案 - 相关工具需要尊重@lombol.Generated (或者更好,任何@*.Generated )注释。

Tools like Jacoco and Error Prone (very recently) already support it. JacocoError Prone (最近)等工具已经支持它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM