如何忽略 Sonar 中的重复代码报告?

2022-01-17 00:00:00 sonarqube java

SonarQube 向不同的简单 POJO 类报告为重复代码块",如下所示.在这种情况下,A 和 B 是不同的角色.所以,我认为我不应该创建抽象类.

公共类 A{私人字符串 xxx;//省略其他字段.公共 A() {}公共字符串 getXxx() {返回xxx;}公共无效 setXxx(字符串 xxx){this.xxx=xxx;}//省略其他字段的 setter 和 getter}公共类 B{私人字符串 xxx;//省略其他字段.公共 B() {}公共字符串 getXxx() {返回xxx;}公共无效 setXxx(字符串 xxx){this.xxx=xxx;}//省略其他字段的 setter 和 getter}

严重性为主要.所以,我想忽略它.然后,我将@SuppressWarning("common-java:DuplicatedBlocks") 和@SuppressWarning("all") 添加到这两个类中.但也不容忽视.

虽然在

有时 Sonar 报告的情况并不严重,但又取决于项目性质 :)

但是,就像上面评论中提到的@Stephen,如果 xxx 是相同的字段和继承是有意义的,那么你可以有父抽象类来避免报告.

SonarQube reports as "block of duplicated code" to different simple POJO class like below. In this case, A and B are different role. So, I think that I should not create abstract class.

public class A{
  private String xxx;

  // omitted other fields.

  public A() {}

  public String getXxx() {
     return xxx;
  }
  public void setXxx(String xxx) {
     this.xxx= xxx;
  }

  // omitted other fields' setter and getter

}

public class B{
  private String xxx;

  // omitted other fields.

  public B() {}

  public String getXxx() {
     return xxx;
  }
  public void setXxx(String xxx) {
     this.xxx= xxx;
  }

  // omitted other fields' setter and getter

}

The severity is Major. So, I would like to ignore it. Then, I added @SuppressWarning("common-java:DuplicatedBlocks") and @SuppressWarning("all") to both classes. But it could not be ignored.

Though similar question was raised in JIRA, but it have been not solved. My SonarQube's version is 6.5. Thanks!

解决方案

Putting this into the answer section to attach a screenshot:

If you are confident enough that those two blocks of code have different roles, then you can change the reported severity level to Minor or Info from web-console. For example, see the screenshot below:

Sometimes Sonar reports as severe on things which are not really severe, but again depends on the project nature :)

However, like @Stephen mentioned on a comment above, if xxx are same field and inheritance makes sense, then you can have parent abstract class to avoid the report.

相关文章