This question is related to a previous question. The original can be solved by adding casts to do unchecked conversions. So now I have the following code:
import java.util.EnumSet;
class A {
static enum E1 {
X
}
private static <T extends Enum<T>> EnumSet<T> barEnum(Class<T> x) {
return null;
}
private static void foo1(EnumSet<E1> s, E1 e) {
EnumSet<E1> x2 = barEnum((Class<E1>)e.getClass());
}
private static void foo2(EnumSet<E1> s) {
EnumSet<E1> x = barEnum((Class<E1>)s.iterator().next().getClass());
}
}
My original intent was to write a generic method. So I generalized the method foo2() to:
private static <E extends Enum<E>> void foo3(EnumSet<E> s) {
EnumSet<E> x = barEnum(s.iterator().next().getClass());
}
This obviously contains unchecked conversions and compiles with the appropriate warning. But I don't cast the result of getClass() explicitly into Class<E>. Since foo1() is one instance of the generic method foo3(), I expected that I need to add the cast here too. Comparing foo1() to foo4()...
private static void foo4(EnumSet<E1> s) {
EnumSet<E1> x = barEnum(s.iterator().next().getClass());
}
...the two are effectively similar (the main difference being the E1 e parameter in foo1()). However foo1() compiles, but foo4() does not compile. I feel that this is inconsistent. Are there any rules allowing implicit conversions for generic methods?