List of utility methods to do Reflection Generic Type from Class
Class | getGenericParameterType(Class note: for getting actual generic parameter, the object must be instantiated from an anonymouse class (in line class overriding) Type type = clazz.getGenericSuperclass(); if (!(type instanceof ParameterizedType)) return null; ParameterizedType parameterizedType = ((ParameterizedType) clazz.getGenericSuperclass()); Type[] types = parameterizedType.getActualTypeArguments(); type = types[0]; while (type instanceof ParameterizedType) { types = ((ParameterizedType) type).getActualTypeArguments(); ... |
Type | getGenericParameterType0(Class> cls, Class> genericSuper, int paramIndex) get Generic Parameter Type if (!genericSuper.isAssignableFrom(cls)) return null; if (genericSuper.isInterface()) { for (Type type : cls.getGenericInterfaces()) { final Class<?> clazz = getClass(type); if (genericSuper.isAssignableFrom(clazz)) { if (genericSuper.equals(clazz)) return type instanceof ParameterizedType ... |
Class[] | getGenerics(Class classType) get Generics Type type = (Type) classType.getGenericSuperclass(); ParameterizedType pt = (ParameterizedType) type; Type[] types = pt.getActualTypeArguments(); if (types[0] instanceof Class) { Class[] classz = new Class[types.length]; for (int i = 0; i < classz.length; i++) { classz[i] = (Class) types[i]; return classz; } else { return null; |
Class | getGenericsClass(Class cls) get Generics Class ParameterizedType superClass = (ParameterizedType) cls.getGenericSuperclass();
Class target = (Class) superClass.getActualTypeArguments()[0];
return target;
|
Class[] | getGenericSuperclass(Class> clazz) get Generic Superclass try { Type typeGeneric = clazz.getGenericSuperclass(); if (typeGeneric != null) { if (typeGeneric instanceof ParameterizedType) { return getGeneric((ParameterizedType) typeGeneric); } catch (Exception e) { ... |
Type | getGenericSuperclass(Class> cls) get Generic Superclass return cls.getGenericSuperclass();
|
void | getGenericSuperclassActualTypes(Collection get Generic Superclass Actual Types if (aClass != null && types != null) { Type superclass = aClass.getGenericSuperclass(); if (superclass instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) superclass; Type[] interfaces = parameterizedType.getActualTypeArguments(); types.addAll(Arrays.asList(interfaces)); } else if (superclass instanceof Class) { Class sClass = (Class) superclass; ... |
Class | getGenericSuperType(Class clz, int index) get Generic Super Type Class[] clzs = getAllGenericSuperType(clz); if (index < 0 || index > clzs.length - 1) return Object.class; return clzs[index]; |
Type | getGenericSuperType(Class extends T> subclass, Class extends T> classWithParameter) returns either the generic superclass or a generic interface, but only when it is assignable from the typed classWithParameter argument if (subclass.getSuperclass() != null && classWithParameter.isAssignableFrom(subclass.getSuperclass())) { return subclass.getGenericSuperclass(); } else { int i = 0; for (Class in : subclass.getInterfaces()) { if (classWithParameter.isAssignableFrom(in)) { return subclass.getGenericInterfaces()[i]; i++; return null; |
Type | getGenericSuperType(Class> class1, Class> class2) for example when class1 is EpochDateConverter which implements TypeConverter if (class1 == null || class2 == null) throw new RuntimeException("null value"); List<Type> genericSupers = new ArrayList<Type>(); genericSupers.add(class1.getGenericSuperclass()); genericSupers.addAll(newArrayList(class1.getGenericInterfaces())); for (Type type : genericSupers) { if (type instanceof ParameterizedTypeImpl) { ParameterizedTypeImpl a = (ParameterizedTypeImpl) type; ... |