programmatically determine generic type
apologies if this is a开发者_如何学JAVA duplicate.
Is there any way to determine what generic type a class is?
ie I want to know what T
is....
public void doSomething(SomeClass<T> thing)
{
Class<T> t = ???;
}
thanks, p.
Not in Java. You will have to accept a parameter of Class<T>
too, which the caller will have to supply. (You can do this in the constructor if you would like, so that the class reference will be available to all class methods.) This is a commonly used hack to determine what T
refers to at runtime, which is not available otherwise due to the way Java implements generics.
精彩评论