集册 Java实例教程 获取实际类型

获取实际类型

欢马劈雪     最近更新时间:2020-01-02 10:19:05

540
获取实际类型


//package com.nowjava;


import java.lang.reflect.GenericDeclaration;/** nowjava - 时代Java 提供 **/

import java.lang.reflect.ParameterizedType;

import java.lang.reflect.Type;

import java.lang.reflect.TypeVariable;

import java.util.Collections;

import java.util.LinkedHashMap;

import java.util.Map;


public class Main {

    /**

     *

     * @param type

     * @param targetClass

     * @return

     */

    public static Type getActualType(Type type, Class<?> targetClass) {

        Map<TypeVariable<?>, Type> map = getTypeVariableMap(targetClass);

        return getActualType(type, map);

    }


    /**

     *

     * @param type

     * @param map

     * @return

     */
     /*
      from nowjava.com 
     */

    private static Type getActualType(Type type,

            Map<TypeVariable<?>, Type> map) {

        if (Class.class.isInstance(type)) {

            return type;

        } else if (ParameterizedType.class.isInstance(type)) {

            return ParameterizedType.class.cast(type).getRawType();

        } else if (TypeVariable.class.isInstance(type)) {

            return map.get(TypeVariable.class.cast(type));

        } else {

            return null;

        }

    }


    /**

     *

     * @param clazz

     * @return

     */

    private static Map<TypeVariable<?>, Type> getTypeVariableMap(

            final Class<?> clazz) {

        if (clazz == null) {

            return Collections.emptyMap();

        }

        final Map<TypeVariable<?>, Type> map = new LinkedHashMap<>();

        final Class<?> superClass = clazz.getSuperclass();

        final Type superClassType = clazz.getGenericSuperclass();

        if (superClass != null) {

            gatherTypeVariables(superClass, superClassType, map);

        }

        final Class<?>[] interfaces = clazz.getInterfaces();

        final Type[] interfaceTypes = clazz.getGenericInterfaces();

        for (int i = 0; i < interfaces.length; ++i) {

            gatherTypeVariables(interfaces[i], interfaceTypes[i], map);

        }

        return map;

    }


    /**

     *

     * @param clazz

     * @param type

     * @param map

     */

    private static void gatherTypeVariables(final Class<?> clazz,

            final Type type, final Map<TypeVariable<?>, Type> map) {

        if (clazz == null) {

            return;

        }

        gatherTypeVariables(type, map);


        final Class<?> superClass = clazz.getSuperclass();

        final Type superClassType = clazz.getGenericSuperclass();

        if (superClass != null) {

            gatherTypeVariables(superClass, superClassType, map);

        }

        final Class<?>[] interfaces = clazz.getInterfaces();

        final Type[] interfaceTypes = clazz.getGenericInterfaces();

        for (int i = 0; i < interfaces.length; ++i) {

            gatherTypeVariables(interfaces[i], interfaceTypes[i], map);

        }

    }


    /**

     *

     * @param type

     * @param map

     */

    private static void gatherTypeVariables(final Type type,

            final Map<
展开阅读全文