All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.base4j.mvc.util.GenricUtil Maven / Gradle / Ivy

There is a newer version: 1.3.0
Show newest version
package com.base4j.mvc.util;

import sun.reflect.generics.reflectiveObjects.TypeVariableImpl;

import java.lang.reflect.GenericDeclaration;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class GenricUtil {

    public static Class getGenericClass(final Class clazz, final int index) {
        //返回表示此 Class 所表示的实体(类、接口、基本类型或 void)的直接超类的 Type。
        Type genType = clazz.getGenericSuperclass();
        if (!(genType instanceof ParameterizedType)) {
            return Object.class;
        }
        //返回表示此类型实际类型参数的 Type 对象的数组。
        Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
        if (index >= params.length || index < 0) {
            return Object.class;
        }
        if (params[index] instanceof TypeVariableImpl) {
            GenericDeclaration genericDeclaration = ((TypeVariableImpl) params[index]).getGenericDeclaration();
            genericDeclaration.getTypeParameters()[index].getName();
            return null;
        }
        if (!(params[index] instanceof Class)) {
            return Object.class;
        }
        return (Class) params[index];

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy