Uploaded image for project: 'SonarJava'
  1. SonarJava
  2. SONARJAVA-2413

FP on S1872 when instanceof cannot be used

    Details

    • Type: False-Positive
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 4.13
    • Component/s: Rules
    • Labels:
      None

      Description

      In the following code, we can not replace the usage of Integer.class.getName().equals(realType) by usage of instanceof because we don't have the object instance itself, only its fully qualified name.

      The rule should only ask for replacement with instanceof if there is a .class.getName() method invocation on both side of the equals(...), or at least should not ask for replacement if an identifier is used.

      abstract class A {
        Object foo(Number myNumber, String realType) {
          if (Integer.class.getName().equals(realType)) { // FP on S1872
            return convertToInteger(myNumber);
          }
          return myNumber;
        }
      
        abstract Integer convertToInteger(Number n);
      }
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                nicolas.peru Nicolas Peru
                Reporter:
                michael.gumowski Michael Gumowski
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Due:
                  Created:
                  Updated:
                  Resolved: