Sonar 重​​命名此方法;父 class 中有一个同名的 "private" 方法

Sonar Rename this method; there is a "private" method in the parent class with the same name

当我们使用与父私有方法相同的名称时,Sonar 抱怨 class 中的私有方法名称。在代码质量上定义与父私有方法同名的私有方法有什么缺点?

或者我们是否需要将其归类为误报

在我看来,这是因为这可能会让人感到困惑。考虑下面,阅读评论:

class Child extends Super{
   public void myMethod() {
     System.out.println("in child");
   }
 }

 class Super{
   public static void main(String[] args) {
    Super s = new Child(); 
    s.myMethod(); // At this point you might expect myMethod of child to be called if it'll call the Parent's since it is private.
  }
   private void myMethod() {
     System.out.println("in super");
   }
 }

当你的子类中有一些方法与你的超类同名时,乍一看,假设将是一个重写,当它不是时会造成混淆。

文档提到了三种可能发生这种情况的情况:

The parent class method is static and the child class method is not.

The arguments or return types of the child method are in different packages than those of the parent method.

The parent class method is private.

还有推荐:

But if the intent is truly for the child class method to be different, then the method should be renamed to prevent confusion.

因此,如果您真的想覆盖超类中的方法,建议更改它以避免混淆。

您可以在 RSPEC-2177 - Sonar Rule Documentation

中查看示例

重命名方法或将事件标记为误报的决定完全取决于团队如何组织他们的代码库,以及开发人员之间使用的代码约定。

IHMO,这条规则毫无意义。
如果命名对父级和子级都有意义class,您就不会为其中之一发明不同的名称来取悦 Sonar。
它可以使代码不那么清晰,也可以使它在你的基础代码中不那么同质。 私有方法只在当前class内部可见,所以足以让这个选择安全。

我明白这条规则的目的。但是,它不应该应用于具有具体私有方法的抽象 类。

我们正在使用 Apache MINA 库,它在 CumulativeProtocolDecoder 中有几个具体的私有方法,这些方法在其 public 具体方法中被引用。如果 public 方法被覆盖,我们将被迫提供我们自己的私有方法实现。为了避免被 Sonar 贬低而给它们取别的名字是没有意义的。