FULL PRODUCT VERSION :
A DESCRIPTION OF THE PROBLEM :
$ javac -J-showversion -fullversion Test3.java
java version "1.7.0-ea"
Java(TM) SE Runtime Environment (build 1.7.0-ea-b71)
Java HotSpot(TM) Client VM (build 16.0-b08, mixed mode, sharing)
javac full version "1.7.0-ea-b71"
$ java -showversion Test3
java version "1.7.0-ea"
Java(TM) SE Runtime Environment (build 1.7.0-ea-b71)
Java HotSpot(TM) Server VM (build 16.0-b08, mixed mode)
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at Test3.testInnerClassWithMismatchedOuterType(Test3.java:26)
at Test3.main(Test3.java:30)
$ cat -n Test3.java
1 import java.util.*;
2
3 class Test3 {
4 static class ListHolder<T> {
5 final List<T> list = new ArrayList<T>();
6
7 class Inserter {
8 Inserter(T itemToAdd) {
9 list.add(itemToAdd);
10 }
11 }
12 }
13
14 static class StringListHolder extends ListHolder<String> {
15 class IntInserter extends ListHolder<Integer>.Inserter {
16 IntInserter() {
17 super(42);
18 }
19 }
20 }
21
22 public void testInnerClassWithMismatchedOuterType() {
23 StringListHolder list = new StringListHolder();
24 list.new IntInserter();
25
26 System.out.println(list.list.get(0).length());
27 }
28
29 public static void main(String... args) {
30 new Test3().testInnerClassWithMismatchedOuterType();
31 }
32 }
33
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The program should fail to compile.
ACTUAL -
The program compiles and causes a class cast exception.
REPRODUCIBILITY :
This bug can be reproduced always.