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

FP on S1872 when the class name is not obtained from "xxx.getClass()"

    Details

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

      Description

      The rule should not raise issue on cases where we are not starting from the real type of the object being compared.

      In the following cases, we do not have access to the object that we want to compare to, making the rule raising FPs.

      public abstract class A {
        boolean foo(Iterable<String> argTypes) {
          for (String argType : argTypes) {
            // FP S1872
            if (Object.class.getName().equals(argType)) {
              return true;
            }
          }
          return false;
        }
      
        boolean bar() {
          String argType = getParameterTypeFromName("MY_PARAM");
          // FP S1872
          return Object.class.getName().equals(argType);
      
        }
      
        abstract String getParameterTypeFromName(String s);
      }
      

      Note that a possible workaround could be to use a dedicated constant:

      public abstract class A {
        private static final String OBJECT_CLASS_NAME = Object.class.getName();
      
        boolean foo(Iterable<String> argTypes) {
          for (String argType : argTypes) {
            if (OBJECT_CLASS_NAME.equals(argType)) {
              return true;
            }
          }
          return false;
        }
      
        boolean bar() {
          String argType = getParameterTypeFromName("MY_PARAM");
          return OBJECT_CLASS_NAME.equals(argType);
      
        }
      
        abstract String getParameterTypeFromName(String s);
      }
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                christophe.zurn Christophe Zurn
                Reporter:
                michael.gumowski Michael Gumowski
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Due:
                  Created:
                  Updated:
                  Resolved: