集册 Java实例教程 查找在提供的类实现的单个接口上声明的泛型类型。

查找在提供的类实现的单个接口上声明的泛型类型。

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

461
查找在提供的类实现的单个接口上声明的泛型类型。


//package com.nowjava;
/** from 
时 代 J a v a**/

import java.lang.reflect.ParameterizedType;

import java.lang.reflect.Type;


public class Main {

    /**

     * Finds the generic type declared on a single interface implemented by the provided class.

     * @param clazz the class on which we want to find the generic type.

     * @return the actual type declared on the provided generic interface.

     */

    public static Class<?> getDeclaredGenericType(Class<?> clazz,

            Class<?> genericInterface) {

        Type[] genericInterfaces = clazz.getGenericInterfaces();

        for (Type genericType : genericInterfaces) {

            if (genericType instanceof ParameterizedType) {

                ParameterizedType paramType = (ParameterizedType) genericType;

                if (paramType.getRawType().equals(genericInterface)) {

                    
展开阅读全文