I have a very basic code to reproduce the issue:
public class Main {
public static <T extends Number> void bar(Wrapper<List<T>> wrapper) {
// do something
}
public static void foo(Wrapper<List<? extends Number>> wrapper) {
// do something
}
public static <T extends Number> void bar2(List<T> aList) {
// do something
}
public static void foo2(List<? extends Number> aList) {
// do something
}
public static void main(String[] args) {
List<Integer> aList = Collections.singletonList(1);
foo2(aList); // compiles
bar2(aList); // compiles
Wrapper<List<Integer>> wrapper = new Wrapper<>(Collections.singletonList(1));
foo(wrapper); // not compiles
bar(wrapper); // compiles
}
private static class Wrapper<T> {
private final T t;
public Wrapper(T t) {
this.t = t;
}
}
}
So the question is why javac gives an error when I try to compile the code:
Main.java:26: error: method foo in class Main cannot be applied to given types;
foo(wrapper); // not compiles
^
required: Wrapper<List<? extends Number>>
found: Wrapper<List<Integer>>
reason: argument mismatch; Wrapper<List<Integer>> cannot be converted to Wrapper<List<? extends Number>>
1 error