Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8216620

Spurious unchecked cast warning given for objects with same type params.

XMLWordPrintable

    • x86_64
    • linux

      A DESCRIPTION OF THE PROBLEM :
      Upon compilation of the provided source code an incorrect warning is given for an unchecked conversion at the line containing the cast in the method named compiles_with_unchecked_cast_warning. It does not show this warning for the same operation in the method named compiles_with_no_warnings with the difference being that this method has a static context.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile the provided source code using javac version 1.8.0_191 .

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      No javac warnings should be given upon compilation.
      ACTUAL -
      Warning:java: unchecked cast
        required: InterfaceImpl
        found: Interface<java.lang.Object>

      ---------- BEGIN SOURCE ----------
      class Scratch
      {
          interface Interface<T>{}
          class InterfaceImpl implements Interface<Object>{}
          static class StaticInterfaceImpl implements Interface<Object>{}

          public void compiles_with_unchecked_cast_warning( Interface<Object> list )
          {
              InterfaceImpl classA = (InterfaceImpl)list;
          }

          public static void compiles_with_no_warnings( Interface<Object> list )
          {
              StaticInterfaceImpl classA = (StaticInterfaceImpl)list;
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      The warnings can be suppressed by adding an annotation, @SuppressWarnings("unchecked"), above the statement.

      FREQUENCY : always


            pmangal Priyanka Mangal (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: