List of utility methods to do Reflection Generic Type from Class
Class> | getGenericTypeArgument(Class> clazz, int index) Reads the generic type of the given class at the given index. Type genericSuperclass = clazz.getGenericSuperclass();
return findGenericTypeArgument(genericSuperclass, index);
|
Class | getGenericTypeArgumentFromGenericSuperType(Type genericSuperclass, Class Looks if generic supertype is paramized with given baseRequested. Type loopVar = genericSuperclass; while (loopVar != null && !(ParameterizedType.class.isAssignableFrom(loopVar.getClass()))) { loopVar = ((Class<?>) loopVar).getGenericSuperclass(); if (loopVar != null && ParameterizedType.class.isAssignableFrom(loopVar.getClass())) { Type[] typeArgs = ((ParameterizedType) loopVar).getActualTypeArguments(); for (Type typeArg : typeArgs) { if (typeArg instanceof ParameterizedType) { ... |
List | getGenericTypeArgumentsOfInheritedType(final Object object, final Class> inheritedType) Attempts to return the actual generic type arguments of the chosen inherited type Note: Requires the type argument be explicitly declared in the class definition: class Foo extends Bar < String > ... final Class<?> objectType = object.getClass(); final List<Class<?>> genericTypeArguments = new ArrayList<Class<?>>(); boolean found = false; if (objectType.getGenericSuperclass() instanceof ParameterizedType) { final ParameterizedType parameterizedType = (ParameterizedType) objectType.getGenericSuperclass(); if (parameterizedType.getRawType() == inheritedType) { found = true; for (final Type type : parameterizedType.getActualTypeArguments()) { ... |
Class> | getGenericTypeClass(Class> clazz, int index) get Generic Type Class Class<?> superClass = clazz.getSuperclass(); Type genericSuperClass = clazz.getGenericSuperclass(); while (!(genericSuperClass instanceof ParameterizedType)) { genericSuperClass = superClass.getGenericSuperclass(); superClass = superClass.getSuperclass(); Type[] types = ((ParameterizedType) genericSuperClass).getActualTypeArguments(); if (index < types.length) { ... |
List | getGenericTypeClasses(List get Generic Type Classes for (Type genericType : genericTypes) { if (genericType instanceof ParameterizedType) { getGenericTypeClasses(genericTypeClasses, ((ParameterizedType) genericType).getActualTypeArguments()); } else if (genericType instanceof GenericArrayType) { getGenericTypeClasses(genericTypeClasses, ((GenericArrayType) genericType).getGenericComponentType()); } else if (genericType instanceof TypeVariable) { ... |
Class | getGenericTypeForMapProperty(Class javaClass, String propertyName, boolean isKeyType) get Generic Type For Map Property Class genericClass = null; Field declaredField = getDeclaredField(javaClass, propertyName); Type genericType = declaredField != null ? declaredField.getGenericType() : null; if (genericType instanceof ParameterizedType) { Type[] typeArguments = ((ParameterizedType) genericType).getActualTypeArguments(); if (typeArguments.length > 0) { Type typeArg = typeArguments[isKeyType ? 0 : 1]; if (typeArg instanceof Class) { ... |
Class> | getGenericTypeOfInterface(Class> clazz, Class> specificInterface) Fetches the declared type on a specific interface implemented by the provided class. Type[] genericInterfaces = clazz.getGenericInterfaces(); if (genericInterfaces != null) { for (Type genericType : genericInterfaces) { if (genericType instanceof ParameterizedType) { Type rawType = ((ParameterizedType) genericType).getRawType(); if (rawType.equals(specificInterface)) { ParameterizedType paramType = (ParameterizedType) genericType; return (Class<?>) paramType.getActualTypeArguments()[0]; ... |
Class> | getGenericTypeOfParameter(Class> clazz, String method, int parameterIndex) get Generic Type Of Parameter try { Method m = clazz.getMethod(method, new Class<?>[] { Set.class, int.class }); ParameterizedType type = (ParameterizedType) m.getGenericParameterTypes()[parameterIndex]; return (Class<?>) type.getActualTypeArguments()[0]; } catch (Exception e) { try { Method m = clazz.getMethod(method, new Class<?>[] { HashSet.class, int.class }); ParameterizedType type = (ParameterizedType) m.getGenericParameterTypes()[parameterIndex]; ... |
Class> | getGenericTypeParameter(Class> aClass, Class> genericClass, int index) get Generic Type Parameter List<Class<?>> classes = new ArrayList<>(); Class<?> currentCls = aClass; boolean isGenericClassInterface = genericClass.isInterface(); String parameterName = genericClass.getTypeParameters()[index].getName(); while (currentCls != null && currentCls != genericClass && genericClass.isAssignableFrom(currentCls)) { classes.add(currentCls); currentCls = currentCls.getSuperclass(); Class<?> managedClass = null; boolean shouldCheckInInterfaces = isGenericClassInterface; for (int i = classes.size() - 1; i >= 0 && managedClass == null; i--) { Class<?> cls = classes.get(i); ParameterizedType parameterizedType = null; if (shouldCheckInInterfaces) { for (Type superInterface : cls.getGenericInterfaces()) { if (superInterface instanceof ParameterizedType) { ParameterizedType type = (ParameterizedType) superInterface; if (type.getRawType() instanceof Class) { Class rawType = (Class) type.getRawType(); if (rawType.isAssignableFrom(genericClass)) { shouldCheckInInterfaces = false; parameterizedType = type; } else { if (cls.getGenericSuperclass() instanceof ParameterizedType) { parameterizedType = (ParameterizedType) cls.getGenericSuperclass(); index = 0; for (TypeVariable typeVariable : cls.getSuperclass().getTypeParameters()) { if (typeVariable.getName().equals(parameterName)) { break; index++; if (parameterizedType != null && parameterizedType.getActualTypeArguments().length > index) { Type typeArgument = parameterizedType.getActualTypeArguments()[index]; if (typeArgument instanceof Class) { managedClass = (Class<?>) typeArgument; } else if (typeArgument instanceof TypeVariable) { TypeVariable typeVar = (TypeVariable) typeArgument; parameterName = typeVar.getName(); return managedClass; |
Type[] | getGenericTypes(Class> c) get Generic Types Type superClass = c.getGenericSuperclass();
ParameterizedType type = (ParameterizedType) superClass;
return type.getActualTypeArguments();
|