Created
March 27, 2017 02:31
-
-
Save lonniev/7b0d19ace7d6c0f31cb57bde51e2c43a to your computer and use it in GitHub Desktop.
Determine the actual type of a method's first parameter at runtime when that parameter is generic.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Class<?> getTypeOfFirstParameter( final Method method ) | |
{ | |
final Type type = method.getGenericParameterTypes()[0]; | |
// Now get the parametrized type of the generic. | |
if (!(type instanceof ParameterizedType)) | |
{ | |
return method.getParameterTypes()[0]; | |
} | |
final ParameterizedType pType = (ParameterizedType) type; | |
final Type t = pType.getActualTypeArguments()[0]; | |
if (t instanceof Class) { | |
return (Class<?>) t; | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment