JDK-6476118 : compiler bug causes runtime ClassCastException for generics overloading
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 6,7
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-09-28
  • Updated: 2017-05-16
  • Resolved: 2011-03-08
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7 b123Fixed
Related Reports
Duplicate :  
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.6.0-rc"
Java(TM) SE Runtime Environment (build 1.6.0-rc-b100)
Java HotSpot(TM) Client VM (build 1.6.0-rc-b100, mixed mode, sharing)


ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
The attached code compiles with no errors or warnings, but raises unexpected ClassCastException at runtime.  This behavior shows an "unsoundness" of the compiler (in terms of the implementation of generics in Java).

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
C:\WINDOWS\Temp>type B.java
class A{
        public int compareTo(Object o){
                return 0;
        }
}

class B extends A implements Comparable<B>{
        public int compareTo(B b){
                return 0;
        }

        public static void main(String[] argv){
                System.out.println(new B().compareTo(new Object(){}));
        }
}

C:\WINDOWS\Temp>c:\progra~1\java\jdk1.6.0\bin\javac.exe -Xlint B.java

C:\WINDOWS\Temp>c:\progra~1\java\jdk1.6.0\bin\java.exe B
Exception in thread "main" java.lang.ClassCastException: B$1 cannot be cast to B

        at B.compareTo(B.java:7)
        at B.main(B.java:13)

C:\WINDOWS\Temp>


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
should be a compiler-time error, according to The Java Language Specification, Third Edition, page 227 (fourth item of the list)
ACTUAL -
runtime ClassCastException

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.lang.ClassCastException: B$1 cannot be cast to B

        at B.compareTo(B.java:7)
        at B.main(B.java:13)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
class A{
        public int compareTo(Object o){
                return 0;
        }
}

class B extends A implements Comparable<B>{
        public int compareTo(B b){
                return 0;
        }

        public static void main(String[] argv){
                System.out.println(new B().compareTo(new Object(){}));
        }
}

---------- END SOURCE ----------

Comments
EVALUATION http://hg.openjdk.java.net/jdk7/build/langtools/rev/1d625fbe6c22
25-12-2010

SUGGESTED FIX A webrev of this fix is available at the following URL: http://hg.openjdk.java.net/jdk7/tl/langtools/rev/1d625fbe6c22
09-12-2010

EVALUATION If class B is public, a public bridge to A.compareTo is added which causes a compilation failure.
09-10-2006

EVALUATION The compiler should probably reject the program or give a stern warning. It may be tricky to make changes in this area as we should be careful to avoid breaking bridge methods in general.
04-10-2006

EVALUATION Could be a problem with bridge methods?
28-09-2006