Clarity on rules that scan the Java code

235 views
Skip to first unread message

seet...@gmail.com

unread,
Jul 12, 2017, 1:51:25 AM7/12/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 12, 2017, 3:37:08 AM7/12/17
to seet...@gmail.com, SonarQube
Hello Seetesh,

to help you I need to understand how exactly you run your analysis. Which plugins do you use to analyze your Java files? In which version?

You can find all installed plugins on your SonarQube instance in the update center, or via this URL https://[SONARQUBE_HOST]/deploy/plugins/index.txt  , replace SONARQUBE_HOST with you actual SonarQube hostname.

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/6ff5b559-ada8-4fce-9af2-44ad6d1593e1%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 12, 2017, 7:01:25 AM7/12/17
to SonarQube, seet...@gmail.com
Sonarqube plugin is installed in Jenkins (version 2.32). Sonarqube (version 6.2) is configured as post build action in Jenkins and the analysis is done when the build is triggered.

The plugins folder has 12 jars files which are referenced during the scan.




On Wednesday, July 12, 2017 at 1:07:08 PM UTC+5:30, Tibor Blenessy wrote:
Hello Seetesh,

to help you I need to understand how exactly you run your analysis. Which plugins do you use to analyze your Java files? In which version?

You can find all installed plugins on your SonarQube instance in the update center, or via this URL https://[SONARQUBE_HOST]/deploy/plugins/index.txt  , replace SONARQUBE_HOST with you actual SonarQube hostname.

Best regards

Tibor Blenessy

unread,
Jul 12, 2017, 7:20:55 AM7/12/17
to seet...@gmail.com, SonarQube
Thanks for coming back with versions of SonarQube and Jenkins plugin. However for the Java file analysis it is important to know version of SonarJava or other analyzer responsible for scanning of Java files. Can you please provide versions of those? 

Also, what is the exact rule, which is reporting unused methods / arguments? Can you send the rule key? 

Thanks

--
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.
--

seet...@gmail.com

unread,
Jul 12, 2017, 9:15:08 AM7/12/17
to SonarQube, seet...@gmail.com
Not sure of SonarJava since we rely on SonarQube for the builtin scanners but JDK version is 1.7.

Tibor Blenessy

unread,
Jul 12, 2017, 9:28:34 AM7/12/17
to seet...@gmail.com, SonarQube
I am sorry, I won't be able to help you unless you are able to provide 1. analyzers you are using to scan Java files, 2. exact rule key from issue, which is being triggered.

In normal circumstances, the issue you described (unused public method), should not be triggered. There must be something else in play. Without 1. and 2.  it is not possible to guess what it might be.

Best regards


For more options, visit https://groups.google.com/d/optout.

seet...@gmail.com

unread,
Jul 13, 2017, 2:20:04 AM7/13/17
to SonarQube, seet...@gmail.com
Apologies for the JDK version : It is 1.8

SonarQube Scanner for Jenkins is 2.6.1

For the overloaded method having 1 parameter but different type

Unused method parameters should be removed

Remove this unused method parameter "Dog"


Hope this is sufficient for your analysis


On Wednesday, July 12, 2017 at 6:58:34 PM UTC+5:30, Tibor Blenessy wrote:

Tibor Blenessy

unread,
Jul 13, 2017, 4:02:07 AM7/13/17
to seet...@gmail.com, SonarQube
Hello,

I checked implementation of rule Unused method parameters, and indeed, the rule is reporting only first unused parameter of the method. This should be improved, I created a ticket to handle it https://jira.sonarsource.com/browse/SONARJAVA-2392 .


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.

seet...@gmail.com

unread,
Jul 14, 2017, 2:28:02 AM7/14/17
to SonarQube, seet...@gmail.com
Also there was one more issue with 2 overloaded methods with just one parameter but defined as an Object which the code

Both the methods are being called in another program but Sonar throws an Alert that the 2nd overloaded method is unused.

Basically the overloaded method is not having a different datatype but the Object defined as parameter is different and I am wondering if Sonar is able to find the basic difference between the 2 Objects?

Thanks for raising a ticket for the other issue as discussed.

seet...@gmail.com

unread,
Jul 14, 2017, 8:20:32 AM7/14/17
to SonarQube, seet...@gmail.com
Hello Tibor,

Will you be raising another JIRA on the single parameter of different Objects as part of the same two overloaded methods which uses the rule key description

Unused "private" methods should be removed


Also the call to this method is present but not scanned by Sonar. I believe the overloading is not handled by Sonar for Objects as parameters.

Regards,

Seetesh

Tibor Blenessy

unread,
Jul 14, 2017, 8:41:19 AM7/14/17
to seet...@gmail.com, SonarQube
Hello Seetesh,

I am sorry, I am not able to reproduce the issue you are describing with current version of SonarJava analyzer. The example you posted previously doesn't have any private methods, so I don't see how it could raise the issue.

Can you provide self-contained example triggering this issue and version of SonarJava analyzer you are using to analyze your code?

Best regards


For more options, visit https://groups.google.com/d/optout.

seet...@gmail.com

unread,
Jul 17, 2017, 3:54:11 AM7/17/17
to SonarQube, seet...@gmail.com
My mistake of not putting that as a private method but by mistake I had written the public.

BTW all methods are usually private only since Sonar expects private and not public.
Reply all
Reply to author
Forward
0 new messages