I have a class that uses generics. Because it needs a reference to the generic type, the constructor has a Class
parameter (as discussed in this question), so it looks like this:
MyGenericClass<T>
public MyGenericClass( final Class<T> valueClass ) {
this.clazz = valueClass;
}
Now I want to pass MyGenericClass as the T, but I can't get it working.
MyGenericClass<MyGenericClass<String>> foo =
new MyGenericClass<MyGenericClass<String>>(MyGenericClass<String>.class);
is giving me a compiler error.
It looks like it's a problem of Type Erasure (see this SO question). With this dual layer of generics, I'm not sure if it's possible to do what I want to do.
Any suggestions?
UTA:
I can't post the entire code because it's corporate code and I'm not sure of the rules of posting. The class is essentially a MultipleValueMap that maps a single key to a list of values. The class object is used to create a typed array of the values in the list (using list.toArray(T[])
)
In this case, I want to have a key that maps to more than one list of values. And that's causing the issue.