Community
Participate
Working Groups
The following example fails to compile producing the following diagnostic: Severity Description Resource In Folder Location Creation Time 2 Bound mismatch: The generic method sort(List<T>) of type Collections is not applicable for the arguments (List<X>) since the type X is not a valid substitute for the bounded parameter <T extends Comparable<? super T>> X.java Test/src line 15 April 8, 2005 3:57:50 PM javac compiles without a problem. import java.util.*; class X implements Comparable { public int compareTo(Object o) { // TODO Auto-generated method stub return 0; } } class Y { public static void main(String[] args) { List<X> lx = null; Collections.sort(lx); } }
As I have investigated this further it's not clear to me that Eclipse has the wrong behaviour. Since 15.12.2.2 requires that X <: Comparable<? super X>, which it is not. It's possible my JLS3 is out of date. The one I have is dated 8/16/04. I'll investigate further.
This is related to fix for bug 86838. I introduced a limitation in bound check preventing raw type to be a good match to a non-raw bound.
I suspect javac treats differently variable bound check for type or method: import java.util.*; public class X implements Comparable { public static void main(String[] args) { List<X> lx = null; sort1(lx); sort2(lx); sort3(lx); sort4(lx); sort5(lx); S1<X> s1; S2<X> s2; S3<X> s3; S4<X> s4; S5<X> s5; } public int compareTo(Object o) { return 0; } static <T extends Comparable<? super T>> void sort1(List<T> list) {} static <T extends Comparable<? extends T>> void sort2(List<T> list) {} static <T extends Comparable<?>> void sort3(List<T> list) {} static <T extends Comparable<T>> void sort4(List<T> list) {} static <T extends Comparable> void sort5(List<T> list) {} } class S1<T extends Comparable<? super T>> {} class S2<T extends Comparable<? extends T>> {} class S3<T extends Comparable<?>> {} class S4<T extends Comparable<T>> {} class S5<T extends Comparable> {} ========== X.java:8: warning: [unchecked] unchecked method invocation: <T>sort1(java.util.List<T>) in X is applied to (java.util.List<X>) sort1(lx); ^ X.java:9: warning: [unchecked] unchecked method invocation: <T>sort2(java.util.List<T>) in X is applied to (java.util.List<X>) sort2(lx); ^ X.java:11: warning: [unchecked] unchecked method invocation: <T>sort4(java.util.List<T>) in X is applied to (java.util.List<X>) sort4(lx); ^ X.java:14: type parameter X is not within its bound S1<X> s1; ^ X.java:15: type parameter X is not within its bound S2<X> s2; ^ X.java:16: type parameter X is not within its bound S3<X> s3; ^ X.java:17: type parameter X is not within its bound S4<X> s4; ^ 4 errors 3 warnings
Added GenericTypeTest#test598-600. I changed the TypeVariableBinding#boundCheck semantics to answer: OK, MISMATCH or UNCHECKED in this scenario. Depending on who initiates the call, UNCHECKED is either bad or tolerable; which is the case for method type argument inference; but still will issue some unchecked method invocation warning. Changes are to be released along with fix for bug 84496
*** Bug 84284 has been marked as a duplicate of this bug. ***
*** Bug 90430 has been marked as a duplicate of this bug. ***
Released
*** Bug 94701 has been marked as a duplicate of this bug. ***
Verified in I20050510-0010