Duplicated block of Code in Sonar for import statements in Java
Asked Answered
L

2

23

In Sonar 4.5.6 (with default settings) I am seeing Duplicated Blocks message as

enter image description here

My java code for which I am getting the message is like below:-

package com.jabong.orchestratorservice.adapter.order.endpoints;

import com.jabong.orchestratorservice.adapter.order.request.UpdateOrderStatusReadyShipRequest;

public class UpdateOrderReadyShipEndPoint extends BaseOrderEndPoint {
    private final static String API_NAME = "setStatusToReadyToShip";

    @Override
    public String getSourceEndPoint() {
    return new StringBuilder("direct:").append(API_NAME).toString();
    }

    @Override
    public String getDestinationEndPoint() {
    return new StringBuilder("bean:orderHelper?method=").append(API_NAME).toString();
    }

    @Override
    protected String getName() {
    return API_NAME;
    }

    @Override
    protected String getApiInputClassName() {
    return UpdateOrderStatusReadyShipRequest.class.getName();
    }
}

UpdateOrderStatusReadyShipRequest also does not import UpdateOrderReadyShipEndPoint

package com.jabong.orchestratorservice.adapter.order.request;

public class UpdateOrderStatusReadyShipRequest extends BaseOrderRequest {

Can some let me know what does this mean?

Liston answered 20/3, 2016 at 8:1 Comment(2)
Does the other class import this one?Wineglass
No it does not import.Liston
B
37

The Duplicate Blocks rule raises issues at the file level. So it's not trying to tell you that your import statement is duplicated, but that somewhere in the file is a duplicate block. If you'll scroll down, you should see a vertical yellow/orange bar in the left margin. It marks the duplicate block. Click on the bar to get details of where the block is duplicated.

EDIT In more recent versions the duplication marker is brown or gray.

Bash answered 21/3, 2016 at 12:22 Comment(6)
I had same issue on my project. Abstract class implemented with two other classes with same overrides. Sonar issued this as same block. But actually it was not. Anyway I get rid of it by replacing method order in one of that classes. Funny, isn't it? :)Nought
@G. Ann - SonarSource Team Where Can I find the logic of this rule common-java:DuplicatedBlocks ?Lucre
It's in SonarQube itselfBash
@G. Ann - SonarSource Team .Would be quite interesting to know how this works.This rule is in sonarqube and rest of the rules starting with squid-xxxx are in sonar-java.Is Sonar using some external library to check code duplication?Lucre
@G. Ann - SonarSource Team Thank you for the hint ,I have figured out how copy code is working.Lucre
what to do in case of similar type of private variables and there getter/setter which are marked as duplicate blocks?Spheroidal
W
25

You have to look (scroll down) your code. There will be a duplication marker in brown/gray like this:

enter image description here

Willywilly answered 17/3, 2020 at 16:35 Comment(5)
Thanks, We could locate the duplicate block of code and the lines as mentioned clearly by you in the screenshot.Vardon
Thanks you saved my day. The yellow background on code lines is really misleading.Celestial
Woah ! thanks man Sonarqube needs to do better at documentation. You saved the day !Gerous
Thanks Carlos, your screenshot is a savior. I was scrolling mindlessly, and this gray marker wasn't catching my attention.Dissentious
@Carlos saved the day. thank youPitchdark

© 2022 - 2024 — McMap. All rights reserved.