1

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?

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
pron
  • 1,693
  • 18
  • 28
  • 3
    possible duplicate of [How to pass a parameterized class as an argument](http://stackoverflow.com/questions/4980011/how-to-pass-a-parameterized-class-as-an-argument) and [Java: how do I get a class literal from a generic type?](http://stackoverflow.com/questions/2390662/java-how-do-i-get-a-class-literal-from-a-generic-type) – Paul Bellora Oct 09 '13 at 14:36
  • Agreed -- pleas see the answer linked by @PaulBellora – bstempi Oct 09 '13 at 14:41

1 Answers1

0
pron
  • 1,693
  • 18
  • 28