United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-6476118 : compiler bug causes runtime ClassCastException for generics overloading

Details
Type:
Bug
Submit Date:
2006-09-28
Status:
Closed
Updated Date:
2011-05-23
Project Name:
JDK
Resolved Date:
2011-03-08
Component:
tools
OS:
windows_xp
Sub-Component:
javac
CPU:
x86
Priority:
P3
Resolution:
Fixed
Affected Versions:
6,7
Fixed Versions:

Related Reports
Duplicate:
Relates:
Relates:

Sub Tasks

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
                                     
2010-12-25
SUGGESTED FIX

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

If class B is public, a public bridge to A.compareTo is added which
causes a compilation failure.
                                     
2006-10-09
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.
                                     
2006-10-04
EVALUATION

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



Hardware and Software, Engineered to Work Together