27

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.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Neriudon
  • 377
  • 1
  • 3
  • 7
  • you can change the severity level to normal for this one – Yogen Rai Oct 18 '18 at 01:40
  • If the Xxxx fields genuinely **mean** the same thing, it *may* be appropriate to create an abstract superclass for `A` and `B`. The fact that they are used differently doesn't imply that you can't or shouldn't extract common code. (Just saying ...) – Stephen C Oct 18 '18 at 01:47
  • @Yogen Rai Wow, I don't know it. Now my role is just a developer, I will ask to the administrator. Thank you! – Neriudon Oct 18 '18 at 02:23
  • @Neriudon ya.. we always do that on each sprint if there is unnecessary severity level assignment :) – Yogen Rai Oct 18 '18 at 02:25
  • @Stephen C Certainly, I should consider to create create superclass to avoid this problem. Thanks! – Neriudon Oct 18 '18 at 02:27
  • At the time of asking this question, duplicate code was implemented as a Rule which you could resolve yourself. That's now been deprecated and replaced with a separate duplicate code metric which can't be resolved or have its severity reduced, so any older answers here are not going to be very useful. – Nick Aug 16 '23 at 09:42

4 Answers4

28

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
Mithfindel
  • 4,553
  • 1
  • 23
  • 32
  • Thank you very much for your answer. At first, I will try refactoring my code. Next, I will consider to change SonarQube configuration (not using `@SuppressWarnings`). – Neriudon Oct 18 '18 at 08:39
  • 2
    I am using autogenerated validation code which expands out the schemas of the objects it is validating. Any reused sub-objects will be definition be duplicates. It would be nice to just have an override in the UI for instances like this. – Andrew Gill Jan 20 '21 at 12:46
  • 26
    **or mark it as "won't fix"** - but how? I can't find this button for the report of duplicated lines. at all. – Pedro A Sep 17 '22 at 04:23
2

The best way would be to tweak the quality gate to suit your need. Need to keep in mind that default built-in quality gate is standard and should be always preferred.

Copy the default and create a new quality gate. Edit it to accept duplication percentage according to your comfort level (depending on your project needs).

Copy the default and edit it to accept duplication percentage according to your comfort level

async_soul
  • 143
  • 1
  • 8
-8

Sonar helps us to improve code quality by applying certain rules. But exceptions are always there and if you are confident that your code needs to suppress certain violation, you can use @SuppressWarnings. eg @SuppressWarnings("common-java:DuplicatedBlocks")

Now how we will get reule info or id.

  1. Go to sonar dashboard.
  2. Click on Issues.
  3. Select your class and issue and further check see rule.
  4. Rule id is mentioned in the top right corner of rule description.

enter image description here

pinaci
  • 353
  • 4
  • 4
  • 1
    according to https://stackoverflow.com/questions/36983618/how-is-the-warning-source-files-should-not-have-any-duplicated-blocks-suppress?rq=1 this does not work! – jherek Sep 02 '21 at 13:29
-12

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.

Yogen Rai
  • 2,961
  • 3
  • 25
  • 37