Quantcast
Channel: User Jonathan - Stack Overflow
Viewing all articles
Browse latest Browse all 40

Answer by Jonathan for Create instance of generic type in Java when parameterized type is contained?

$
0
0

What you're trying to do can work so long as E is parameterized in a type definition somewhere. For example:

Parameterized<E> pe = new Parameterized<E>();

This will not allow you to resolve E since it's not part of a type definition. On the otherhand, this:

class StringE extends Parameterized<String> {}

or this:

Parameterized<String> ps = new Parameterized<String>(){};

will work since we're specifying the value of E as part of a type definition. To resolve the value of E, you might use TypeTools (which I authored):

Class<?> stringType = TypeResolver.resolveRawArgument(Parameterized.class, ps.getClass());assert stringType == String.class;

Viewing all articles
Browse latest Browse all 40

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>