Unable to call generic method with pattern

I have a class defined like this:

public class Test {
    static <T> List<Class<T>> filter(List<Class<T>> input) {
        // code here
    }

    public static void main(String[] args) {
        List<Class<? extends Throwable>> list =
            new ArrayList<Class<? extends Throwable>>();
        filter(list);
    }
}

Calling method filterin maingives the following compilation error:

The method filter(List<Class<T>>) in the type Test is not applicable for the
arguments (List<Class<? extends Throwable>>)

I do not understand why it is <T>not attached to <? extends Throwable>. Are there any guru droids that can help me?

+3
source share
3 answers

I believe the problem is that for ?there is no type.

Consider if for simplicity we replace Classwith AtomicReference.

static <T> void filter(List<AtomicReference<T>> input) {
    T value = input.get(0).get();
    input.get(1).set(value);
}

If it inputconsisted of AtomicReference<Integer>and AtomicReference<String>, we would have problems.

, , . . - .

+4

, .

, :

static <T> List<Class<T>> filter(List<Class<? extends T>> input) {
    // code here
}

, . , , . .

+4

You can also write basic information as shown below:

public static void main(String[] args) {
    List<Class<Throwable>> list =
        new ArrayList<Class<Throwable>>();
    filter(list);
}
0
source

Source: https://habr.com/ru/post/1710955/


All Articles