I have a type-parameterized method but I can't call getClass
on the type-parameterized parameter because it can be null
, so the only solution I see is to get the Class
from the type parameter itself, somehow:
public <T> Class myMethod(T obj)
{
//can't do this: return obj.getClass() because null is permitted.
}
This method simply illustrates the problem. My actual method doesn't even return the parameter's class, but uses it in a different way. The core of the problem is this: Is there a way to get the Class
object that represents the parameterized type of my method's parameter, without using obj.getClass()
(because in case obj is null
, I still need to know with what type parameter was the method invoked, for example: <String>myMethod(null)
must return the java.lang.String
class object).
EDIT This is not a duplicate of the said question because my class is not parameterized, only my method.