List of utility methods to do Reflection Generic Type from Class
Class> | getGenericClass(final Class> class1) get Generic Class final Type type = class1.getGenericSuperclass(); return getGenericClass(type); |
Class | getGenericClass(final Class> parametrizedClass, int pos) get Generic Class final ParameterizedType pType = (ParameterizedType) parametrizedClass.getGenericSuperclass(); final TypeVariable tv = (TypeVariable) pType.getActualTypeArguments()[pos]; if (tv.getBounds()[0] instanceof ParameterizedType) { return (Class<T>) ((ParameterizedType) tv.getBounds()[0]).getRawType(); } else { return (Class<T>) tv.getBounds()[0]; |
Class | getGenericClass(final Class> parametrizedClass, int pos) get Generic Class return (Class<T>) ((ParameterizedType) parametrizedClass.getGenericSuperclass())
.getActualTypeArguments()[pos];
|
Class | getGenericClass(final Method method) get Generic Class final Type returnType = method.getGenericReturnType(); return getActualType(returnType, 0); |
Class | getGenericClass(Object o) get Generic Class ParameterizedType parameterizedType = (ParameterizedType) o.getClass().getGenericSuperclass();
return (Class) parameterizedType.getActualTypeArguments()[0];
|
Class> | getGenericClass(Object object, int index) get Generic Class ParameterizedType genericSuperclass = getParameterizedType(object.getClass()); if (genericSuperclass == null) { return String.class; Class<?> rawType = getRawType(genericSuperclass.getActualTypeArguments()[index]); if (rawType == null) { return String.class; return rawType; |
Class | getGenericClass(Object source, ParameterizedType type) Try to extract the generic type of the given ParameterizedType used in the given source object. Type type1 = type.getActualTypeArguments()[0]; if (type1 instanceof ParameterizedType) { return (Class) ((ParameterizedType) type1).getRawType(); } else if (type1 instanceof TypeVariable) { if (source.getClass().getGenericSuperclass() instanceof ParameterizedType) { return (Class) ((ParameterizedType) source.getClass().getGenericSuperclass()) .getActualTypeArguments()[0]; } else { ... |
Class> | getGenericClass(Type type) get Generic Class return getGenericClass(type, 0);
|
Type | getGenericClassByIndex(Type genericType, int index) Get parameterized type Type clazz = null; if (genericType instanceof ParameterizedType) { ParameterizedType t = (ParameterizedType) genericType; Type[] types = t.getActualTypeArguments(); clazz = types[index]; return clazz; |
Type | getGenericClassType(Class clazz, Class filterClass) Extract the real Type from the passed class. for (Type type : clazz.getGenericInterfaces()) { if (type == filterClass) { return type; } else if (type instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) type; if (filterClass.isAssignableFrom((Class) pType.getRawType())) { return type; return null; |