如何告诉 Lombok 生成的 getter 是一个 @Override。

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

How to tell to lombok the generated getter is an @Override

问题

我在我的项目中使用了Lombok,并且有一个接口:

public interface MyInterface {

    Object getA();
}

还有一个类:

@Getter
public class MyClass implements MyInterface {

    private Object a;

    private Object b;
}

我已经检查了生成的类,类中生成的方法没有使用@Override注解。我想知道如何添加这个注解?以及没有@Override注解的后果是什么?

也许这是另一个问题,但是这段代码经过SonarQube分析后,Sonar指出私有字段a从未被使用。

我已经看过关于sonarqube + lombok = false positives的主题。

但在我的情况下,字段b不会引发误报。因此,我认为这不是直接相关的。

您是否看到了在不重新实现getA()的情况下避免这些问题的解决方法?

英文:

I'm using lombok in my project and I have an interface :

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

I'm wondering how to add this annotation ? And what are the consequences of a missing @Override ?

It's maybe an another question but this code is analyzed by sonarqube and sonar say that private field a is never used.

I've already seen the subject about sonarqube + lombok = false positives

But In my case b doesn't create a false positive. So I don't think this is directly related

Do you see a solution to avoid this problems without reimplement getA() ?

答案1

得分: 3

你可以使用注解的 [`onMethod` 属性](https://projectlombok.org/features/experimental/onX) 来向生成的方法添加任何你想要的注解。

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

在这种情况下,作为一种风格,我可能会将类级别的 `@Getter` 移动到另一个字段上。如果还有 `c`、`d`、`e` 这些字段也应该使用普通(无注解)的 @Getter 逻辑,我会保持如上设置。

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

你可能还想要[启用 Lombok 生成的注解](https://projectlombok.org/features/configuration)。这将阻止一些工具(覆盖率、静态分析等)去检查 Lombok 的方法。虽然不确定是否在这种情况下有帮助,但我几乎在我所有的项目中都启用了它。

    lombok.addLombokGeneratedAnnotation = true
英文:

You can use the onMethod attribute of the annotation to add any annotation you want to the generated method.

@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. If there were c, d, e which should all use normal (no annotation) @Getter logic, I would leave it as above.

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. It will prevent some tools (coverage, static analysis, etc) from bothering to check Lombok's methods. Not sure if it will help in this case, but I pretty much have it enabled in all of my projects.

lombok.addLombokGeneratedAnnotation = true

答案2

得分: 2

你可以通过在使用@GetteronMethod中添加@Override来为特定字段添加@Override

例如:

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

@Override对代码本身没有影响。它只是通知编译器该方法是从其父类中的方法进行覆盖。如果该方法没有覆盖父类的方法,编译器将在构建时失败。

英文:

You can add @Override to a particular field by using @Getter's onMethod.

e.g.

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

@Override has no affect on the code itself. 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.

答案3

得分: 2

防止_Error Prone_(或类似工具)误报缺失的 @Override 的一种更通用的方法是配置 Lombok 以 @lombok.Generated 注释其生成的代码。这可以通过向 Lombok 的 lombok.config 添加以下条目来在全局范围内完成:

lombok.addLombokGeneratedAnnotation = true

需要注意的是,这并不是一个通用的解决方案 - 相关工具需要尊重 @lombol.Generated(或者更好的是,任何 @*.Generated)注解。
类似 JacocoError Prone(最近)已经支持这一特性。

英文:

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. This can be done - globally - by adding the following entry to Lombok's 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.

Tools like Jacoco and Error Prone (very recently) already support it.

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

发表评论

匿名网友

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

确定