[false-negative]S2133 can not detect getClass called by array of classes

Affects Sonarqube Version:
Sonarqube version:
sonar-scanner version:

S2133: Objects should not be created only to “getClass”

Unable to detect getClass called by array of classes,such as: new Object[0].getClass()
This rule is implemented in java-checks-!\org\sonar\java\checks\ObjectCreatedOnlyToCallGetClassCheck.java.
This rule uses TypeCriteria.subtypeOf(“java.lang.Object”)).name(“getClass”).withoutParameter() to filter class objects that may have errors, but the array of class objects may be ignored.

Code Sample demonstrating the issue:

assertEquals(new Long[0].getClass(), types[0]);
Constructor<?> constructor = nsDataClass.getConstructor(new Class[]{new byte[0].getClass()});

Expected outcome:

Running Sonarqube through:
command line

Thanks for reporting this false negative, I created a ticket (SONARJAVA-3442) to keep track of it.

For the record, it seems that the problem is not coming from the line you stated since it changed since version 6.2.
In any case, it should not be too hard to identify.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.