My question is a follow up of an other guy's question: Unbounded wildcard passed to method
He was interested why the following code compiled:
public class ColTest {
static<T> T wildSub(ArrayList<? extends T> holder, T arg){
T t=holder.get(0);
return t;
}
public static void main(String[] args) {
ArrayList<?> list=new ArrayList<Long>(Arrays.asList(2L,3L,7L));
Long lng=1L;
ColTest.wildSub(list, lng);
}
}
We came to a conclusion that the trick was that compiler inferred the ? as an Object and made the following Long argument pass thanks to the trivial inheritance of Object->Long.
The code does compile using Sun/Oracle javac (I use 1.6.0_26-b03), but does not compile in Eclipse (I use Helios) where it displays the following compilation error:
The method wildSub(ArrayList<? extends T>, T) in the type ColTest is not applicable for the arguments (ArrayList<capture#2-of ?>, Long)
My question is:
Is this a bug in the Java compiler implementation Eclipse uses or some kind of ambiguity in Java 'generics inference algorithm' specification that is valid and just implemented differently by Eclipse?
1 Answer 1
It appears to be an Eclipse bug.
T should be inferred to as Object, per 15.12.2.7.
15.12.2.8 also has a catch-all clause: "Any remaining type variables that have not yet been inferred are then inferred to have type Object"
with T=Object, per 15.12.2.2, the method is applicable.
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.12