FULL PRODUCT VERSION :
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]
A DESCRIPTION OF THE PROBLEM :
When iterating over generic iterable elements with the enhanced for-each loop, and you do not use typeinformation in the loop, you get a ClassCastException runtime. One would expect this not to happen, since we do not use the generic type information in the for-each loop, but the compiled code still does a cast.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile the attached java file, and run it.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Expected result was to see a regular run of the program with no errors.
ACTUAL -
The actual result is a classcastexception
ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.lang.ClassCastException: ForEachBug$B
at ForEachBug.reproduceBug(ForEachBug.java:19)
at ForEachBug.main(ForEachBug.java:10)
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.util.ArrayList;
import java.util.List;
public class ForEachBug {
public class A {}
public class B {}
public static void main(String[] args) {
new ForEachBug().reproduceBug();
}
private void reproduceBug() {
List list = new ArrayList();
list.add(new B());
List<A> aList = new ArrayList<A>();
aList.addAll(list);
for (Object a : aList) {
System.out.println(a);
}
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
make sure you do not use @supressWarnings("unchecked"), and remove all type safety warnings in your code.
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]
A DESCRIPTION OF THE PROBLEM :
When iterating over generic iterable elements with the enhanced for-each loop, and you do not use typeinformation in the loop, you get a ClassCastException runtime. One would expect this not to happen, since we do not use the generic type information in the for-each loop, but the compiled code still does a cast.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile the attached java file, and run it.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Expected result was to see a regular run of the program with no errors.
ACTUAL -
The actual result is a classcastexception
ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.lang.ClassCastException: ForEachBug$B
at ForEachBug.reproduceBug(ForEachBug.java:19)
at ForEachBug.main(ForEachBug.java:10)
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.util.ArrayList;
import java.util.List;
public class ForEachBug {
public class A {}
public class B {}
public static void main(String[] args) {
new ForEachBug().reproduceBug();
}
private void reproduceBug() {
List list = new ArrayList();
list.add(new B());
List<A> aList = new ArrayList<A>();
aList.addAll(list);
for (Object a : aList) {
System.out.println(a);
}
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
make sure you do not use @supressWarnings("unchecked"), and remove all type safety warnings in your code.
- relates to
-
JDK-6923689 Enhanced for loop with unsafe generics is incorrectly desugared.
- Closed
-
JDK-6690688 Enhanced-for-loop translation as described in JLS 14.14.2 is not type-safe
- Closed