List of utility methods to do Reflection Generic Type from Class
Type | getGenericDeclaringType(Class> baseClass, Class> declaringClass) get Generic Declaring Type if (baseClass.equals(declaringClass)) { return baseClass; if (declaringClass.isInterface()) { Type[] interfaces = baseClass.getGenericInterfaces(); for (Type type : interfaces) { if (type instanceof ParameterizedType) { if (((ParameterizedType) type).getRawType().equals(declaringClass)) { ... |
Class> | getGenericFirstClass(Type type) get Generic First Class return findGenericClass(type, 0);
|
Type | getGenericInterface(final Class sourceClass, final Class genericInterface) get Generic Interface Type[] types = sourceClass.getGenericInterfaces(); for (Type type : types) { if (type instanceof Class) { if (genericInterface.isAssignableFrom((Class) type)) { return type; } else if (type instanceof ParameterizedType) { if (genericInterface.isAssignableFrom((Class) ((ParameterizedType) type).getRawType())) { ... |
Type | getGenericInterfaceParamType(Class> cls, Class> rawType) Returns the Type of parameter of the generic interface of the class. while (cls != null) { Type[] interfaces = cls.getGenericInterfaces(); for (Type type : interfaces) { if (type instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) type; if (pType.getRawType() == rawType) { return pType.getActualTypeArguments()[0]; } else { ... |
void | getGenericInterfacesActualTypes(Collection get Generic Interfaces Actual Types if (aClass != null && types != null) { Type[] interfaces = aClass.getGenericInterfaces(); for (Type anInterface : interfaces) { if (anInterface instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) anInterface; Type[] actualTypes = parameterizedType.getActualTypeArguments(); types.addAll(Arrays.asList(actualTypes)); } else if (anInterface instanceof Class) { ... |
Class | getGenericParameter(Class clazz, int index) get Generic Parameter Type genType = clazz.getGenericSuperclass(); if (genType instanceof ParameterizedType) { return getGenericParameter((ParameterizedType) genType, index); return null; |
Class | getGenericParameter(Type class1, int number) Extract generic parameter from super class ParameterizedType type = (ParameterizedType) class1; Type arg = type.getActualTypeArguments()[number]; if (arg instanceof Class) { return (Class) arg; } else if (arg instanceof ParameterizedType) { return (Class) ((ParameterizedType) arg).getRawType(); } else { throw new UnsupportedOperationException(); ... |
Class | getGenericParameterClass(Class actualClass, int parameterIndex) get Generic Parameter Class return (Class) ((ParameterizedType) actualClass.getGenericSuperclass())
.getActualTypeArguments()[parameterIndex];
|
String | getGenericParameterClass(Class clazz) Gets the first generic between class parents and return's its first parameter type Type type = clazz.getGenericSuperclass(); while (!(type instanceof ParameterizedType)) { clazz = clazz.getSuperclass(); if (clazz == null) { return null; type = clazz.getGenericSuperclass(); String genericTypeName = ((ParameterizedType) type).getActualTypeArguments()[0].getTypeName(); return genericTypeName; |
Class>[] | getGenericParameters(Class> clazz) get Generic Parameters return getGenericParametersInternal(clazz.getGenericSuperclass());
|