4

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.

Rainer Jung
  • 636
  • 7
  • 23

1 Answers1

2

Possible solution

old:

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

new:

static <T extends SomeInterface<?>> void someMethod(Class<T> param) {..}
Rainer Jung
  • 636
  • 7
  • 23
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • 1
    I made this answer community wiki to encourage others to add proper explanation of why solution from this answer works but OP version doesn't (currently I can't). – Pshemo Nov 25 '19 at 10:07