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!
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.
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.
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.
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.
@SuppressWarnings
annotations for this is a bit of an abuse, when there are dedicated features in SonarQubeFor instance, you can add the following property to your scanner configuration:
sonar.cpd.exclusions=path/to/your/package/*.java
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With