I have a generic static method and I want it to return Class object of the inserted actual generic type.
I have tried it like this:
@SuppressWarnings("unchecked")
public static <T> Class<T> getClassType() {
Method thisMethod;
try {
thisMethod = CommonTools.class.getMethod("getClass", new Class<?>[0]);
} catch (Exception ex) {
//ErrorLog.error(ex.getMessage(), ex);
return null;
}
Type returnType = thisMethod.getGenericReturnType();
Type tType = returnType.getClass().getGenericSuperclass();
ParameterizedType parameterizedType = (ParameterizedType) tType;
Class<T> clazz = (Class<T>) parameterizedType.getActualTypeArguments()[0];
return clazz;
}
My inspiration for this was found in google code morphia library, there they have used something like this:
(((Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]))
the difference between my code and their code is that they call it for finding generic type in a class - I want it to be used in a method.
My code does not work and it falls within the (Class) conversion line.
Do you have any ideas how to get the current generic type from a static method?
thanks