List of utility methods to do Reflection Generic Parameter
Type | getGenericParamType(Type type) get Generic Param Type if (type instanceof ParameterizedType) { return type; if (type instanceof Class) { return getGenericParamType(((Class<?>) type).getGenericSuperclass()); return type; |
Class> | getGenericParamType(Type type) Get the class of the parameter of the provided parameterized type. Class<?> generic = null; if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); generic = (Class<?>) actualTypeArguments[0]; return generic; |
Class | getGenericParamType1(Type parameterizedType) Get the parameterized type of a generic type GenericType if (parameterizedType instanceof ParameterizedType) { ParameterizedType type = (ParameterizedType) parameterizedType; Type[] typeArguments = type.getActualTypeArguments(); if (typeArguments.length > 0) return (Class) typeArguments[0]; return null; |
Class>[] | getGenericParamTypes(Type genericType) get Generic Param Types Objects.requireNonNull(genericType, "Generic type is NULL!"); try { if (genericType instanceof ParameterizedType) { Type[] genericTypes = ((ParameterizedType) genericType).getActualTypeArguments(); Class<?>[] result = new Class<?>[genericTypes.length]; for (int i = 0; i < genericTypes.length; i++) { if (genericTypes[i] instanceof Class<?>) { result[i] = (Class<?>) genericTypes[i]; ... |
Type | getGenericType(Parameter param) Attempts to infer the type in the generic part of the declaration of the type Type parameterizedType = param.getParameterizedType(); if (parameterizedType instanceof ParameterizedType) { parameterizedType = ((ParameterizedType) parameterizedType).getRawType(); return parameterizedType; |
Class | getGenericTypeParameter(Type genericType) Get the type parameter out of a generic type. ParameterizedType fieldParamType = null; if (genericType instanceof ParameterizedType) { fieldParamType = (ParameterizedType) genericType; if (fieldParamType == null) { return Object.class; Class typeParameter = (Class) fieldParamType.getActualTypeArguments()[0]; ... |
Type[] | getGenericTypes(ParameterizedType t) get Generic Types if (t == null) return new Type[] {}; else return t.getActualTypeArguments(); |
List | getGenericTypes(Type[] genericParameterTypes) Returns a list of class's for every generic parameter type given by Method#getGenericParameterTypes() or Class#getGenericInterfaces() List<Class<?>> l = new ArrayList<>(); if (genericParameterTypes == null) return l; try { for (Type t : genericParameterTypes) { ParameterizedType pType = (ParameterizedType) t; for (Type type : pType.getActualTypeArguments()) { l.add((Class<?>) type); ... |