13
Let's suppose I have the following:
public <T extends Widget> List<T> first(T n) {
return first(n.getClass());
}
public <T extends Widget> List<T> first(Class<T> n) {
return new ArrayList<>();
}
The compiler complains at line 3 with "incompatible types; required: java.util.List<T>; found: java.util.List<capture#1 of ? extends my.app.Widget>". Which I don't understand why. It seems reasonable to me that the type T could never change in either case other than a sub-type.
This can be fixed via explicit casting, though I do not know why it is required.
public <T extends Widget> List<T> first(T n) {
return (List<T>)first(n.getClass());
}
public <T extends Widget> List<T> first(Class<T> n) {
return new ArrayList<>();
}
Could this be a compiler bug?
Note I am using JDK 1.7.0_15:
java version "1.7.0_15"
Java(TM) SE Runtime Environment (build 1.7.0_15-b03)
Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)