Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I ignore duplicated code report in Sonar?

Tags:

java

sonarqube

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!

like image 629
Neriudon Avatar asked Oct 18 '18 01:10

Neriudon


People also ask

How do I ignore duplicate codes in Sonar?

The only way to ignore duplications is by using the Duplication Exclusion section of the settings. This settings requires filename patterns. There is no way to select parts of files, only entire files.

How do I stop code duplication?

Don't Repeat Yourself (DRY): Using DRY or Do not Repeat Yourself principle, you make sure that you stay away from duplicate code as often as you can. Rather you replace the duplicate code with abstractions or use data normalization. To reduce duplicity in a function, one can use loops and trees.

How can I check duplicate code in SonarQube?

SonarQube shows you all the duplications metrics on a project, package, or class basis. Further, you can use the Duplications tab in the file detail view to see the location of a duplication in a class.


2 Answers

There are several ways for you to achieve this, depending on the importance you give to this duplication issue. SonarQube reports what it finds, it's all up to you to decide what to do with it.

  • If you believe that this is indeed an issue, you have to refactor your code: SonarQube cannot report duplication when there is none
  • If you believe that this particular instance is not an issue, you can either lower the severity of the issue, or mark it as "won't fix", with a nice comment for the person who will come after you - I believe that using @SuppressWarnings annotations for this is a bit of an abuse, when there are dedicated features in SonarQube
  • If you believe that SonarQube should not even raise issues about duplicated code, you can either disable the rule (the nuclear option), or setup your analysis to ignore duplication for your POJO package(s)

For instance, you can add the following property to your scanner configuration:

sonar.cpd.exclusions=path/to/your/package/*.java
like image 123
Mithfindel Avatar answered Sep 18 '22 16:09

Mithfindel


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: enter image description here

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.

like image 21
Yogen Rai Avatar answered Sep 16 '22 16:09

Yogen Rai