Community
Participate
Working Groups
Javac correctly compiles the snippet below, but Eclipse shows as an error: ****************************** import java.util.Collection; public class Fails { public static <E extends Enum<E>> void fails () { Class<? extends Enum> enumType = null; final Collection<E> test = allOf(enumType); } public static <E extends Enum<E>> Collection<E> allOf(final Class<E> enumType) { return null; } }
Javac issues the following warnings: X.java:7: warning: [unchecked] unchecked method invocation: <E>allOf(java.lang.Class<E>) in X is applied to (java.lang.Class<capture of ? ex tends java.lang.Enum>) final Collection<E> test = allOf(enumType); ^ X.java:7: warning: [unchecked] unchecked conversion found : java.util.Collection required: java.util.Collection<E> final Collection<E> test = allOf(enumType); ^ 2 warnings
We issue: ---------- 1. WARNING in X.java (at line 7) final Collection<E> test = allOf(enumType); ^^^^^^^^^^^^^^^ Type safety: Unchecked invocation allOf(Class<capture-of ? extends Enum>) of the generic method allOf(Class<E>) of type X ---------- 2. ERROR in X.java (at line 7) final Collection<E> test = allOf(enumType); ^^^^^^^^^^^^^^^ Type mismatch: cannot convert from Collection<capture-of ? extends Enum> to Collection<E> ----------
This is a bug in javac, also see bug 129261 for more details. Note that the following code exposes the issue more visually. Both assignments to 'test' and 'test2' are equivalent in theory in term of types involved. However, javac only catches 'test2' assignment, because it is losing track of some capture through method invocation. import java.util.Collection; public class X { public static <E extends Enum<E>> void fails () { Class<? extends Enum> enumType = null; final Collection<E> test = allOf(enumType); Collection<? extends Enum> colType = null; final Collection<E> test2 = colType; } public static <E extends Enum<E>> Collection<E> allOf(final Class<E> enumType) { return null; } } *** This bug has been marked as a duplicate of 129261 ***
Added GenericTypeTest#test929
Reopening. I now believe we should have accepted this code. JLS 15.12.2.6 mandates an erasure conversion in presence of unchecked conversion during applicability check. Hence javac's behavior seem legite.
and actually, this bug wasn't a true dup of bug 129261
Given changes for bug 258798, we now pass this test. Closing as dup *** This bug has been marked as a duplicate of bug 258798 ***
Verified for 3.5M5 using build I20090127-0100.