Учитывая следующий класс:Java генериков, шаблоны, коллекции: Ошибка компиляции
import java.util.ArrayList;
import java.util.Collection;
public class Main {
private static class A {
}
private static class B<T> {
private void thenReturn(T value) {
}
}
private static <T> B<T> when(T methodCall) {
return new B<T>();
}
private static Collection<? extends A> method() {
return new ArrayList<>();
}
public static void main(String[] args) {
Collection<? extends A> result = new ArrayList<>();
// Does not compile.
when(method()).thenReturn(result);
}
}
я получаю ошибку компиляции The method thenReturn(Collection<capture#1-of ? extends Main.A>) in the type Main.B<Collection<capture#1-of ? extends Main.A>> is not applicable for the arguments (Collection<capture#3-of ? extends Main.A>)
Что мне изменить в основной метод для того, что он будет компилировать? Есть ли лучшее решение, чем
public static void main(String[] args) {
Collection result = new ArrayList<>();
when(method()).thenReturn(result);
}