2 overloaded methods defined and used in code but Sonar points one is not used at all

760 views
Skip to first unread message

seet...@gmail.com

unread,
Jul 27, 2017, 3:49:03 AM7/27/17
to SonarQube
Hello Sonar team

I have declared overloading 2 methods in Java with just one parameter which is a object

public class classnew {

    public void abc(Cat ct) {
       System.out.println("Cat");
   }

    public void abc(Dog dg) {
       System.out.println("Dog");
   }

    public void decide(final String abc, int salary) {
           int finalover;
    }


}

public class test {

     classnew nw = new classnew();

     public void test() {

              Cat cat1 = new Cat();
              cat1.setName("Tomcat");

              Dog dog1 = new Dog();
              dog1.setName("Bulldog");

              nw.abc(cat1);

              nw.abc(dog1);
    }

}


while running Sonar scan, we get a alert/issue for the method    public void abc(Dog dg) that it is unused and must be removed.
Another alert is for the unused variable "abc" which is the first parameter to the classnew java file but Sonar doesn't give me an issue alert for the 2nd parameter salary of integer type which too is unused.


Is this behavior of Sonar justified and can anyone point to the documentation where its drafted.

Regards,

Seetesh Hindlekar

Tibor Blenessy

unread,
Jul 27, 2017, 6:06:03 AM7/27/17
to seet...@gmail.com, SonarQube
Hello Seetesh,

you are asking the same question without any additional information. I already answered you in this thread https://groups.google.com/d/msgid/sonarqube/971997df-49ca-421b-b830-3964b6f72667%40googlegroups.com?utm_medium=email&utm_source=footer 

Regards

--
You received this message because you are subscribed to the Google Groups "SonarQube" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/4141f65f-58f4-4331-89c9-d642b300646a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

Tibor Blenessy | SonarSource

SonarJava Developer

http://sonarsource.com 

seet...@gmail.com

unread,
Jul 27, 2017, 8:22:42 AM7/27/17
to SonarQube, seet...@gmail.com
public class classnew {

    private void abc(Cat ct) {
       System.out.println("Cat");
   }

    private void abc(Dog dg) {
       System.out.println("Dog");
   }

    private void decide(final String abc, int salary) {
           int finalover;
    }


}

though they are private, the 2 java files are in same package.

seet...@gmail.com

unread,
Jul 27, 2017, 9:09:34 AM7/27/17
to SonarQube, seet...@gmail.com

package com.cg;

public class tttt {

private static final String AANGESLOTEN_ORGANISATIES = "Aangesloten organisaties";

String message = "foo";

private String foo() {

return message;

}

private void uncoveredMethod() {

System.out.println("uncoveredMethod Method message called in foo() = "+foo());

Cat newcat = new Cat();

System.out.println("uncoveredMethod before callcat - cat");

newcat.setName("catty");

newcat.setLocation("mumbai");

callcat(newcat);

System.out.println("uncoveredMethod before callcat - dog");

Dog dog = new Dog();

dog.setEmpno(11);

dog.setSurname("doggyismaddy");

callcat(dog);

}

private void callcat(Cat ct) {

String a = ct.getName();

String b = ct.getLocation();

String AAAAAHHJHJHJAHHH = "Twenty@777dd$$$$";

changePassword(AAAAAHHJHJHJAHHH);

}

private void callcat(Dog dg) {

String aaa = "dddd";

}

private void changePassword(String pass) {

int aa = pass.length();

System.out.println("pass length "+aa);

}

private void test(final String setingaa, String setnew) {

}

private void test(int ttttfinal, final String aaaasetingaa, String aaasetnew) {

}

private static void seet() {

String newpass = AANGESLOTEN_ORGANISATIES;

newpass = newpass + "ddd";

}

public static void main(String args[]) {

tttt t = new tttt();

t.uncoveredMethod();

}

}


Remove this unused method parameter "dg".
  • L40
  • Code Smell
  • Major
  • Open
  • S
    Seetesh
  • 5min effort
  • cert, misra, unused
Remove this unused private "callcat" method.
  • L40
  • Code Smell
  • Major
  • Open
  • Not assigned
  • 5min effort
  • cert, unused

Tibor Blenessy

unread,
Jul 27, 2017, 9:31:28 AM7/27/17
to seet...@gmail.com, SonarQube
Hi again,

I think the problem might be, that you are not providing declaration for your Dog and Cat classes. If they are both unknown, indeed the call will not be recognized and issue might be raised. You have to provide dependencies by setting sonar.java.binaries and sonar.java.libraries properties for the analysis. Or if you are just testing locally, declare classes in the same file to see if the issue disappears.

Best regards




--
You received this message because you are subscribed to the Google Groups "SonarQube" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages