[JAVA] S1872 Classes should not be compared by name - False Positive - comparing String variable with class name (any instance does not exist)

93 views
Skip to first unread message

Adam Gabryś

unread,
Sep 26, 2017, 4:16:35 AM9/26/17
to SonarQube

Hi,

Rule S1872 Classes should not be compared by name is raising a false positive for comparison operation of class name with String variable. We know that we shouldn't compare class names of two instances or one instance with hardcoded text. In our code we compare String variable which stores type (class name). That variable is read from context (e.g. metadata read from XML). Below is an image with 5 cases, where I think two of them are False Positives.




Example class:

Project:

Build:
mvn clean package sonar


Best Regards,

Adam Gabryś

Adam Gabryś

unread,
Sep 26, 2017, 4:40:31 AM9/26/17
to SonarQube
Sorry, I forgot.

Environment:
  • SonarQube: 5.6.6
  • SonarJava plugin: 4.12.0.11033
Best Regards
Adam Gabryś

Michael Gumowski

unread,
Sep 26, 2017, 4:43:02 AM9/26/17
to Adam Gabryś, SonarQube
Hello Adam,

Thanks for your feedback, can you try with version 4.13 of SonarJava?
We released it with ticket SONARJAVA-2413 which should cover exactly this case.

Cheers,
Michael

--
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/375b2bd8-bd3d-4e7c-b9e0-9be7c14f7f18%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Michael Gumowski | SonarSource
Software Developer, Language Team
http://sonarsource.com

Adam Gabryś

unread,
Oct 6, 2017, 4:24:15 AM10/6/17
to Michael Gumowski, SonarQube

Hi Michael,

Yesterday our infrastructure team upgraded Java Plugin to 4.14. With that version one of two false positives disappeared:




Best Regards
Adam Gabryś


From: Michael Gumowski <michael....@sonarsource.com>
Sent: Tuesday, September 26, 2017 10:42 AM
To: Adam Gabryś; SonarQube
Subject: Re: [JAVA] S1872 Classes should not be compared by name - False Positive - comparing String variable with class name (any instance does not exist)
 

Adam Gabryś

unread,
Dec 17, 2017, 7:29:56 AM12/17/17
to Michael Gumowski, SonarQube

Hi Michael,

Could you give me a status of the issue? Do you plan to create a Jira ticket?


Best Regards

Adam Gabryś

Michael Gumowski

unread,
Jan 2, 2018, 8:14:11 AM1/2/18
to Adam Gabryś, SonarQube
Hello Adam,

Sorry for the delay answering you. Here is the ticket I created to handle it, as it has indeed not been covered by the previous ticket: SONARJAVA-2603

Cheers,
Michael
Michael Gumowski | SonarSource
Software Developer, Language Team
https://www.sonarsource.com
Reply all
Reply to author
Forward
0 new messages