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

2024-05-10Java开发问题
30

本文介绍了如何忽略 Sonar 中的重复代码报告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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.

这篇关于如何忽略 Sonar 中的重复代码报告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13