Say I have the following Java method
static <V> KK<V> foo(Class<? extends V> c) {
return null;
}
where KK is the following generic class:
class KK<T> {
}
The following then compiles:
KK<String> x = foo(String.class);
But this doesn't:
KK<Collection<String>> x = foo(Collection.class);
due to "incompatible types: required: KK<Collection<String>>
found: KK<Collection>
If I define foo as follows:
static <V> KK<V> foo(Class<? super V> c) {
return null;
}
the previous call compiles, but so does the following,
KK<Collection<String>> x = foo(Object.class);
which I'd wish would not.
The reason is that Class<V>
can only take a raw type. My question is, is there a way to work around this, namely I want foo to take Class<C>
where C
is the raw type of V
. Is this possible?