List of utility methods to do Reflection Generic Type
Class | getGenericType(Object target) get Generic Type return getGenericType(target, 0);
|
Class | getGenericType(Type genType, int index) get Generic Type if (!(genType instanceof ParameterizedType)) { while (!Object.class.equals(genType) && !(genType instanceof ParameterizedType)) { genType = ((Class) genType).getGenericSuperclass(); if (Object.class.equals(genType)) { return Object.class; Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); if (index >= params.length || index < 0) { return Object.class; if (!(params[index] instanceof Class)) { return Object.class; return (Class) params[index]; |
Class | getGenericType(Type t, int index) get Generic Type if (t == null) { return null; if (!(t instanceof ParameterizedType)) { return null; ParameterizedType ptype = (ParameterizedType) t; if ((ptype.getActualTypeArguments() == null) || (ptype.getActualTypeArguments().length < (index + 1))) { ... |
Class> | getGenericType(Type type) get Generic Type return getGenericType(type, 0);
|
Type[] | getGenericTypeArguments(Method m, int i) get Generic Type Arguments try { Type t = m.getGenericParameterTypes()[i]; if (t instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) t; return pt.getActualTypeArguments(); } catch (Exception e) { System.out.println("Error probing generic type arguments!"); ... |
String | getGenericTypeName(Method method) Get generic type of specific method's return type in string. String genericTypeName = null; String returnTypeName = method.getGenericReturnType().toString(); if (returnTypeName.matches(".*<.*>")) { genericTypeName = returnTypeName.substring(returnTypeName.indexOf("<") + 1, returnTypeName.indexOf(">")); return genericTypeName; |
Type[] | getGenericTypes(Constructor> targetConstructor, Integer constructorArgOrderingNumber) Gets the generic types for a given constructor argument (specified by its ordering number). Type[] genericParameterTypes = targetConstructor.getGenericParameterTypes();
ParameterizedType type = (ParameterizedType) genericParameterTypes[constructorArgOrderingNumber - 1];
return type.getActualTypeArguments();
|
Type[] | getGenericTypes(final Type type) get Generic Types if (type instanceof ParameterizedType) { if (type != null) { return ((ParameterizedType) type).getActualTypeArguments(); return new Class[] { Object.class }; |