Rainer Jung
Rainer Jung

Reputation: 740

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.

Upvotes: 4

Views: 93

Answers (1)

Pshemo
Pshemo

Reputation: 124225

Possible solution

old:

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

new:

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

Upvotes: 2

Related Questions