How can I achieve this?
public class GenericClass
{
public Type getMyType()
{
//How do I return the type of T?
}
}
Just in case you use store a variable using the generic type you can easily solve this problem adding a getClassType method as follows:
public class Constant {
private T value;
@SuppressWarnings("unchecked")
public Class getClassType () {
return ((Class) value.getClass());
}
}
I use the provided class object later to check if it is an instance of a given class, as follows:
Constant> constant = ...;
if (constant.getClassType().equals(Integer.class)) {
Constant integerConstant = (Constant)constant;
Integer value = integerConstant.getValue();
// ...
}