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

Casting from generic class with wildcard to subclass fails to compile

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 6
    • tools
    • x86
    • windows_xp

      FULL PRODUCT VERSION :
      6.0_06, 1.6.0_10-beta-b14 and 1.7.0-ea-b25


      A DESCRIPTION OF THE PROBLEM :
      Code fails to compile when casting from a generic class with unbounded wildcard to a subclass.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      class A<X extends Comparable<X>> {}

      class B extends A<String> {}

      public class Teste {
          public static void main(String[] args) {
              B x = new B();
              A<?> y = x;
              B z = (B) y;
          }
      }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      It should compile (and run) nicely.
      ACTUAL -
      Code fails to compile.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      CUSTOMER SUBMITTED WORKAROUND :
      class A<X extends Comparable<X>> {}

      class B extends A<String> {}

      public class Teste {
          public static void main(String[] args) {
              B x = new B();
              A<?> y = x;
              B z = B.class.cast(y); // <-- Workaround here. Using the cast method.
          }
      }

            mcimadamore Maurizio Cimadamore
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: