Community
Participate
Working Groups
I was trying to understand when the Eclipse compiler generates warnings about casting to an erased type. In this example, only the lines with // lines produce warning messages: package org.example; import java.util.List; import java.util.Map; public class Warning { public static void main(String[] args) { Object object = null; List list = (List)object; // foo((List)object); foo((List<?>)object); foo((List<Object>)object); // foo((List<? extends Object>)object); foo((Map)object); foo((Map<?, ?>)object); foo((Map<Object, ?>)object); foo((Map<?, Object>)object); foo((Map<Object, Object>)object); // foo((Map<? extends Object, Object>)object); foo((Map<? extends Object, ? extends Object>)object); } public static void foo(List<?> list) { System.out.println("###" + list); } public static void foo(Map<?, ?> map) { System.out.println("###" + map); } } But Javac produces more: $ javac -Xlint:all org/example/Warning.java org/example/Warning.java:16: warning: [unchecked] unchecked cast found : java.lang.Object required: java.util.List<java.lang.Object> foo((List<Object>)object); ^ org/example/Warning.java:21: warning: [unchecked] unchecked cast found : java.lang.Object required: java.util.Map<java.lang.Object,?> foo((Map<Object, ?>)object); ^ org/example/Warning.java:22: warning: [unchecked] unchecked cast found : java.lang.Object required: java.util.Map<?,java.lang.Object> foo((Map<?, Object>)object); ^ org/example/Warning.java:23: warning: [unchecked] unchecked cast found : java.lang.Object required: java.util.Map<java.lang.Object,java.lang.Object> foo((Map<Object, Object>)object); ^ org/example/Warning.java:24: warning: [unchecked] unchecked cast found : java.lang.Object required: java.util.Map<? extends java.lang.Object,java.lang.Object> foo((Map<? extends Object, Object>)object); ^ 5 warnings It looks to me like Eclipse doesn't warn about the cast to the erasure if any one of the template arguments is a wildcard but Javac doesn't warn about them only if all of the template arguments are wildcards. I think that the Javac behavior seems more consistent. I've marked this as "major" because we are trying to eliminate all warnings in the EMF code and more importantly in the generated code EMF produces so I want to be sure that the current set of warnings are complete that we don't suddenly get new ones when oversights in where to produce warnings are fixed.
Tuned unchecked cast semantics. We were indeed missing some situations. Also addressing bug 106451 at the same time, since both are related (though not the same bug). Added GenericTypeTest#test1078-1082.
Created attachment 54333 [details] Combined fix for 165143 and 106451
Released for 3.3M4.
Created attachment 54346 [details] Complementary patch Ensure #checkUnsafeCast is called even if compliance < 1.5
*** Bug 165909 has been marked as a duplicate of this bug. ***
Verified for 3.3M4 with I20061211-1119