1

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.

TechGnome
  • 195
  • 1
  • 11
  • 1
    It is duplicate code because you essentially do the same thing. A (logical) way to fix this would be to create a method for that and call this method multiple times. Things that differ can be parameters and things that are the same every time can be inserted directly in the method. – dan1st Jun 14 '21 at 14:54
  • You have actually posted one code snippet as example. Please post the other code block that the sonar warns as a `duplicate` of the above code, but which you feel is not a duplicate. – Gautham M Jun 14 '21 at 15:06
  • @GauthamM - Yeah, that's the rub... that IS the "duplicated code" ... So in that example Sonar is reporting lines 1-7 are duplicates of lines 3-9 ... same block... – TechGnome Jun 14 '21 at 17:13
  • @dan1st - I get that, but that's effectively what we are doing, as far as I can tell. It's all inside a constructor that sets up a read only mapping structure. If we move it all to a method, then everywhere we create this map, we'd have to follow it up with a call to the method that fills it. – TechGnome Jun 14 '21 at 17:15
  • @TechGnome This is not the sonar warning for `duplicate literal` right? – Gautham M Jun 14 '21 at 17:32
  • @GauthamM - No, that one I recognize and know how to deal with - Had to deal with a few of those as well. This is completely different. It's quite befuddling... and everything I come across seems to end up pointing right back at https://docs.sonarqube.org/latest/project-administration/narrowing-the-focus/ which doesn't really help either. I'm about to just add it to the sonar exempt list and call it a day. – TechGnome Jun 14 '21 at 19:49
  • 1
    No, you are doing multiple things in each line. You are not only creating an object (calling the constructor) but you also execute the `put` method. An improvement would be to e.g. use another method like this: `someOtherMethod(2350L "Another Value"));` and infer the other parameters in that method. – dan1st Jun 14 '21 at 22:22

1 Answers1

-1

May be if you cannot absolutely avoid the duplicated code snippet you can annotate using @SupressWarnings("Duplicates")