List of utility methods to do Reflection Generic Type from Class
Class | getGenericArgumentType(Class clazz) Gets the first generic argument of the given class. return getGenericArgumentType(clazz, 0);
|
Class | getGenericArgumentType(Class currentClass, Class genericSuperClass, int argumentIndex) get Generic Argument Type Type superType = currentClass.getGenericSuperclass(); if (superType == null) { throw new IllegalArgumentException( "Class '" + currentClass + "' doesn't have correct generic super class"); if (!(superType instanceof ParameterizedType) || genericSuperClass != null && ((ParameterizedType) superType).getRawType() != genericSuperClass) { return getGenericArgumentType(currentClass.getSuperclass(), genericSuperClass, argumentIndex); ... |
Class | getGenericClass(Class clazz) get Generic Class return getGenericClass(clazz, 0);
|
Class | getGenericClass(Class clazz) get the first generic declaration on a class. Type genType = clazz.getGenericSuperclass(); if (!(genType instanceof ParameterizedType)) { return Object.class; Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); return (Class) params[0]; |
Class | getGenericClass(Class clazz) get Generic Class return getGenericClass(clazz, 0);
|
Class> | getGenericClass(Class clazz, int index) get Generic Class Type type = getGenericType(clazz, index); if (type instanceof Class<?>) { return (Class<?>) type; throw new IllegalArgumentException("Generic parameter is not of type Class"); |
Class | getGenericClass(Class clazz, int index) Locates generic declaration by index on a class. Type genType = clazz.getGenericSuperclass(); if (genType instanceof ParameterizedType) { Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); if ((params != null) && (params.length >= (index - 1))) { return (Class) params[index]; return null; ... |
Class> | getGenericClass(Class> clazz) get Generic Class Type t = clazz.getGenericSuperclass(); if (t instanceof ParameterizedType) { Type[] p = ((ParameterizedType) t).getActualTypeArguments(); return ((Class<?>) p[0]); return null; |
Class | getGenericClass(Class> clazz, int index) get Generic Class Type[] actualTypeArguments = ((ParameterizedType) clazz.getGenericSuperclass()).getActualTypeArguments(); if (actualTypeArguments.length < index) throw new IndexOutOfBoundsException(); return (Class<T>) actualTypeArguments[index]; |
Class> | getGenericClass(Class> cls) get Generic Class return getGenericClass(cls, 0);
|