Java reflection for generics

Posted by Vijay Bhore on Stack Overflow See other posts from Stack Overflow or by Vijay Bhore
Published on 2012-04-08T21:27:37Z Indexed on 2012/04/08 23:29 UTC
Read the original article Hit count: 224

Filed under:
|
|

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!

© Stack Overflow or respective owner

Related posts about java

Related posts about generics