Community
Participate
Working Groups
i have this class: import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Test { public static <T> T[] asArray(Iterator< ? extends T> it, Class<T> clazz) { List<T> lst = new ArrayList<T>(); while (it.hasNext()) { lst.add(it.next()); } return lst.toArray((T[])java.lang.reflect.Array.newInstance(clazz, lst.size())); } public void test() { String[] asString = null; // eclipse 3.5M4 this worked in build I20090129-1200 it doesnt anymore asString = Test.<String> asArray(getIterator(), String.class); // now i have to do this: Iterator<String> iterator = getIterator(); asString = Test.<String> asArray(iterator, String.class); // this also works except if i have remove unnecessary cast enabled then the cast is removed and i get a compile error asString = Test.<String> asArray((Iterator<String>)getIterator(), String.class); } public Iterator getIterator() { return new Iterator() { public void remove(){} public Object next(){ return null; } public boolean hasNext(){ return false; } }; } } the error is: Type mismatch: cannot convert from Object[] to String[] I think the error now isnt right. It should see that it will return a string because it is type with Test.<String> it shouldnt look what goes into the method as arguments (that is just a warning that the iterator isnt generified) also if this error was right. Then the cast shouldnt be removed by the unnecessary cast save action. Because it is needed.
Created attachment 124519 [details] Proposed patch
This issue got introduced while fixing bug 258798. For explicitly parameterized invocations, we should not apply 15.12.2.6 after unchecked conversions occurred in argument types. Note that the true 15.12.2.6. mandates to do it, even if non generic invocation. This is being debated with the spec master; and until resolved, I agree that matching javac behavior is the best for now. Added GenericTypeTest#test1444-1445. Also note that javac is not producing the proper diagnostics for thrown exceptions (failing to substitute T with IOException) e.g.: import java.io.IOException; import java.util.List; public class X { <T extends Throwable> X(List<T> lt) throws T { } <T extends Throwable> List<T> foo(List<T> t) throws T { return t; } static void bar(List l) { new X(l).foo(l); } static void baz(List l) throws IOException { new <IOException> X(l). <IOException> foo(l); } X(List l, long l2) throws IOException { <IOException> this(l); } static void baz2(List l) throws IOException { new <IOException> X(l){}. <IOException> foo(l); } }
Released for 3.5M6
Verified.