I have am getting a method, and checking method.getParameterTypes()[0]
, which is a java.util.List
. But I want to figure out what the containing type is, for instance if it's a java.util.List<String>
I want to figure out that it should hold strings.
How do I do this?
Thanks!
Note that the element type needn't be an actual
Class
likeString
-- it could be a type variable, a wildcarded type, etc.You can read more about scraping generics info from reflected items here and here.
It's simple : you can't. At compile time, Java erases generic types (see Type erasure).
You can see here and here for more information about
GetGenericType
(which I didn't know about, honestly), but it seems rather limited.I had the same issue, and I did as below
in here : your class is initialize as clazz (eg - Company.Class) and your Collection as