public static final Class resolveTypeVariable(Class invocationClass, Type targetType) {
        if (targetType == null) {
            throw new NullPointerException("TypeVariable is null");
        }
        // Class类型
        if (targetType instanceof Class) {
            return (Class) targetType;
        }
        // 参数容器类型（注意：返回的是容器类型，而非具体的参数类型。不要混淆！）
        if (targetType instanceof ParameterizedType) {
            return resolveTypeVariable(invocationClass,
                (Type) ((ParameterizedType) targetType).getRawType());
        }
        // 数组类型
        if (targetType instanceof GenericArrayType) {
            Type componentType = ((GenericArrayType) targetType).getGenericComponentType();
            componentType = resolveTypeVariable(invocationClass, componentType);
            return Array.newInstance((Class) componentType, 0).getClass();
        }

        Map<TypeVariable, Type> refs = new HashMap<TypeVariable, Type>();

        // 
        List<Type> allSuperTypes = new LinkedList<Type>();
        allSuperTypes.addAll(Arrays.asList(invocationClass.getGenericInterfaces()));
        for (int i = 0; i < allSuperTypes.size(); i++) {
            Type type = allSuperTypes.get(i);
            if (type instanceof ParameterizedType) {
                ParameterizedType parameterizedType = ((ParameterizedType) type);
                Class interfaceClass = (Class) parameterizedType.getRawType();
                int j = 0;
                for (Type actualTypeArgument : parameterizedType.getActualTypeArguments()) {
                    TypeVariable v = interfaceClass.getTypeParameters()[j++];
                    refs.put(v, actualTypeArgument);
                    // System.out.println("put " + v + "  --->  " + actualTypeArgument);
                }

                for (Type t : interfaceClass.getGenericInterfaces()) {
                    if (!allSuperTypes.contains(t)) {
                        allSuperTypes.add(t);
                    }
                }
            } else {
                for (Type t : ((Class) type).getGenericInterfaces()) {
                    if (!allSuperTypes.contains(t)) {
                        allSuperTypes.add(t);
                    }
                }
            }
        }

        Type returnType = targetType;
        while (true) {
            Type old = returnType;
            returnType = refs.get(returnType);
            if (returnType instanceof Class) {
                return (Class) returnType;
            }
            if (returnType == null) {
                returnType = old;
                if (returnType instanceof WildcardType) {
                    return (Class) ((WildcardType) returnType).getUpperBounds()[0];
                }
                return (Class) ((TypeVariable) returnType).getBounds()[0];
            }
        }
    }
public static final Class resolveTypeVariable ( Class invocationClass , Type targetType ) { if ( targetType == null ) { throw new NullPointerException ( "TypeVariable is null" ) ; } if ( targetType instanceof Class ) { return ( Class ) targetType ; } if ( targetType instanceof ParameterizedType ) { return resolveTypeVariable ( invocationClass , ( Type ) ( ( ParameterizedType ) targetType ) . getRawType ( ) ) ; } if ( targetType instanceof GenericArrayType ) { Type componentType = ( ( GenericArrayType ) targetType ) . getGenericComponentType ( ) ; componentType = resolveTypeVariable ( invocationClass , componentType ) ; return Array . newInstance ( ( Class ) componentType , 0 ) . getClass ( ) ; } Map < TypeVariable , Type > refs = new HashMap < TypeVariable , Type > ( ) ; List < Type > allSuperTypes = new LinkedList < Type > ( ) ; allSuperTypes . addAll ( Arrays . asList ( invocationClass . getGenericInterfaces ( ) ) ) ; for ( int i = 0 ; i < allSuperTypes . size ( ) ; i ++ ) { Type type = allSuperTypes . get ( i ) ; if ( type instanceof ParameterizedType ) { ParameterizedType parameterizedType = ( ( ParameterizedType ) type ) ; Class interfaceClass = ( Class ) parameterizedType . getRawType ( ) ; int j = 0 ; for ( Type actualTypeArgument : parameterizedType . getActualTypeArguments ( ) ) { TypeVariable v = interfaceClass . getTypeParameters ( ) [ j ++ ] ; refs . put ( v , actualTypeArgument ) ; } for ( Type t : interfaceClass . getGenericInterfaces ( ) ) { if ( ! allSuperTypes . contains ( t ) ) { allSuperTypes . add ( t ) ; } } } else { for ( Type t : ( ( Class ) type ) . getGenericInterfaces ( ) ) { if ( ! allSuperTypes . contains ( t ) ) { allSuperTypes . add ( t ) ; } } } } Type returnType = targetType ; while ( true ) { Type old = returnType ; returnType = refs . get ( returnType ) ; if ( returnType instanceof Class ) { return ( Class ) returnType ; } if ( returnType == null ) { returnType = old ; if ( returnType instanceof WildcardType ) { return ( Class ) ( ( WildcardType ) returnType ) . getUpperBounds ( ) [ 0 ] ; } return ( Class ) ( ( TypeVariable ) returnType ) . getBounds ( ) [ 0 ] ; } } }
求给定的invocationClass类中，目标类型type的值

如果type已是类型，则直接返回它；
如果type是类型变量( {@link TypeVariable})，则求他的值；

@param invocationClass
@param targetType
@return