I've got a Java class that extends a HashMap to hold some objects in a key/value pair fashion. In the constructor, we populate this map with all the values it needs to hold... a few hundred. For some reason somewhere around line 90ish, Sonar has decided that lines 90-120 are duplicates of lines 108-124, even though you can clearly see they are different. And yes, that block over laps. This also doesn't happen just once or twice, no it's reporting 10 such duplicate blocks, some overlap, some don't. I've tried adding
@SuppressWarnings('Duplicates')
@SuppressWarnings('all')
@SuppressWarnings('common-java:DuplicateBlocks')
To the constructor and the class, no joy. I tried adding //NOSONAR
directly to some of the lines affected, still no joy. When I comment out the lines above it, it also doesn't make a difference.
I found this post How do I ignore duplicated code report in Sonar? Tried to follow what's in there, but it wasn't much help either. We're not running SonarCube in the cloud, but locally, so marking it in the UI isn't going to help. It needs to be marked as excluded in the code. Short of excluding it in the POM file, what else is there? I know the @SuppressWarnings('Duplicates')
should work, as we've used it to exclude other code in other classes where there is legit duplicated code. But in this case, the code really isn't duplicated.
Example of the code in question:
this.put(2350L, new MyObject(2350L, "Another Value", "Value 2"));
this.put(2351L, new MyObject(2350L, "Another Value", "Value 2"));
this.put(32633L, new MyObject(32633L, "Another Value", "Value 2"));
this.put(38907L, new MyObject(38907L, "Some Value", "Value 2"));
this.put(38908L, new MyObject(38908L, "Another Value", "Value 2"));
this.put(38909L, new MyObject(38909L, "Another Vaue", "Value 2"));
this.put(38910L, new MyObject(38910L, "Some Value", "Value 2"));
this.put(38911L, new MyObject(38911L, "Some Value", "Value 2"));
this.put(38912L, new MyObject(38912L, "Some Value", "Value 2"));
Sonar has been less than helpful as to why it thinks this is duplicate code.