-
Bug
-
Resolution: Cannot Reproduce
-
P3
-
5.0, 6
-
generic
-
generic
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-2152443 | OpenJDK6 | Jonathan Gibbons | P3 | Closed | Not an Issue |
import java.util.*;
public class TestGenerics {
/**Subclasses are parameterized by their own type*/
private static abstract class SelfType<T extends SelfType<T>>{
public abstract T getThis();
}
/**Supertype inherits directly from the parameterized SelfType*/
private static class SuperType extends SelfType<SuperType>{
@Override
public SuperType getThis(){
return this;
}
}
/**Subtype inherits indirectly from the parameterized SelfType*/
private static class SubType extends SuperType{}
/**Creates a list containing a single SelfType*/
public static <T extends SelfType<T>> List<T> makeSingletonList(T t){
return Collections.singletonList(t);
}
/**
* Creates a list containing a single SelfType, allowing the list's
* element-type to be a supertype of the type of its single element
*/
public static <T extends SelfType<T>,S extends T> List<T> makeSingletonList2(S s){
return Collections.singletonList((T)s);
}
public static void main(String[] args){
/*making lists of super types works fine ...*/
makeSingletonList(new SuperType());
List<SuperType> lsup = makeSingletonList(new SuperType());
/*but we can't make a list of sub types; seems weird ...*/
List<SubType> lsub = makeSingletonList(new SubType()); //ERROR
/*can't even call it w/o assigning the return value:*/
makeSingletonList(new SubType()); //ERROR
/*so instead, we should be able to make lists of super type containing sub type elements*/
makeSingletonList2(new SubType()); //ERROR
/*even if we assign the return value:*/
lsup = makeSingletonList2(new SubType()); // ERROR (eclipse is okay with this though)
/*this still doesn't work either:*/
lsub = makeSingletonList2(new SubType()); // ERROR
/*we can make lists of super type this way though*/
makeSingletonList2(new SuperType()); // (eclipse doesn't like this though)
/*also ok if we assign the return value*/
lsup = makeSingletonList2(new SuperType());
}
}
See http://forum.java.sun.com/thread.jspa?threadID=632009&tstart=0
###@###.### 2005-06-01 00:42:45 GMT
- backported by
-
JDK-2152443 Compiler fails to infer correct type for under-constrained type variables
-
- Closed
-
- duplicates
-
JDK-6468384 Problem with underconstrained type variables
-
- Closed
-
-
JDK-6284158 Can't infer common supertype for String and Integer/int
-
- Closed
-
- relates to
-
JDK-6570321 javac rejects valid assignment for incompatible types
-
- Closed
-
-
JDK-6491939 JLS 4494762 - add compiler test
-
- Closed
-
-
JDK-6650759 Inference of formal type parameter (unused in formal parameters) is not performed
-
- Closed
-
-
JDK-6536404 Regression in method type argument inference
-
- Closed
-
-
JDK-6559165 inference: Adherence to specification causes inference regression
-
- Closed
-