Here you can find the source of getGenericClass(Object source, ParameterizedType type)
Parameter | Description |
---|---|
source | a parameter |
type | a parameter |
private static Class getGenericClass(Object source, ParameterizedType type)
//package com.java2s; import java.lang.reflect.*; public class Main { /**/* ww w .j a v a2 s .c o m*/ * Try to extract the generic type of the given ParameterizedType used in the given source object. * * @param source * @param type * @return */ private static Class getGenericClass(Object source, ParameterizedType type) { Type type1 = type.getActualTypeArguments()[0]; if (type1 instanceof ParameterizedType) { return (Class) ((ParameterizedType) type1).getRawType(); } else if (type1 instanceof TypeVariable) { // Type is generic, try to get its actual type from the super class // e.g.: ObjectProperty<T> where T extends U if (source.getClass().getGenericSuperclass() instanceof ParameterizedType) { return (Class) ((ParameterizedType) source.getClass().getGenericSuperclass()) .getActualTypeArguments()[0]; } else { // The actual type is not declared, use the upper bound of the type e.g. U return (Class) ((TypeVariable) type1).getBounds()[0]; } } else { return (Class) type1; } } }