Java reflection for generics
- by Vijay Bhore
I am using Java Reflection to expose methods in custom eclipse tool.
I am writing method getReturnType which accepts java.lang.reflect.Method as input and returns object of Class
private static Class<?> getReturnType(Method method) {
Type type = ((ParameterizedType)method.getGenericReturnType()).getRawType();
return getClass(type);
}
This code compiles well but at runtime i get the below exception while casting Type to ParameterizedType.
java.lang.ClassCastException: java.lang.Class cannot be cast to
java.lang.reflect.ParameterizedType
Please suggest. Thanks!