Consider the following returnsNull
function and a call to it with a generic type:
public static <T> List<T> returnNull(Class<? extends T> clazz) {
return null;
}
public static void main( String[] args )
{
List<AtomicReference<?>> l = returnNull(AtomicReference.class);
}
The Eclipse compiler, when set to Java 8, accepts it, but javac
in Java 8 rejects it with:
incompatible types: cannot infer type-variable(s) T
(argument mismatch; java.lang.Class<java.util.concurrent.atomic.AtomicReference> cannot be converted to java.lang.Class<? extends java.util.concurrent.atomic.AtomicReference<?>>)
The underlying difference seems to be that given a two parameterized types P1<T>
and P2<T>
, Eclipse allows conversion from the outer type parameterized with the raw inner type: P1<P2>
to the outer type parameterized with a lower bound of the of the inner-type with an unbounded wildcard like P1<? extends P2<?>>
. javac
doesn't.
This isn't just a theoretical musing: if this code was accepted it would solve my generics filtering problem.
Who is right?