JDK-6717191 : Casting from generic class with wildcard to subclass fails to compile
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2008-06-20
  • Updated: 2011-02-16
  • Resolved: 2008-06-20
Related Reports
Duplicate :  
Description
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.
    }
}

Comments
EVALUATION This is the same issue described in CR 6548436. Moreover, the experimental fix I have in place for that bug also address effectively the problem described in this CR.
20-06-2008