Search code examples
javagenericsparameter-passingsuppress-warningsraw-types

class parameter of generic type


This code does not compile:

public static void main(String[] args) {
  someMethod(SomeInterfaceImpl.class); // Compile error
}

static <T> void someMethod(Class<? extends SomeInterface<T>> param) {}

interface SomeInterface<T> {}

class SomeInterfaceImpl<T> implements SomeInterface<T> {}

This error is shown:
The method someMethod(Class<? extends SomeInterface<T>>) in the type SomeApp is not applicable for the arguments (Class<SomeInterfaceImpl>)

I could ignore generics and annotate with @SuppressWarnings("rawtypes").

How can I change my code to be able to use this method without omitting the generics definition? Either I need to change how the method is called and provide some kind of genericified class or I need to change the method definition.


Solution

  • Possible solution

    old:

    static <T> void someMethod(Class<? extends SomeInterface<T>> param) {..}
    

    new:

    static <T extends SomeInterface<?>> void someMethod(Class<T> param) {..}