Skip to content

Instantly share code, notes, and snippets.

@lonniev
Created March 27, 2017 02:31
Show Gist options
  • Save lonniev/7b0d19ace7d6c0f31cb57bde51e2c43a to your computer and use it in GitHub Desktop.
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.
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