正经学徒,佛系记录,不搞事情
在开发工具类时经常会使用到将泛型T转化为Class<T>
//使用方法 参数 T params
Class<T> clazz = GenericsUtils.getSuperClassGenricType(params.getClass());
package com.mn.springboot.utils;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
public class GenericsUtils {
/**
* 通过反射,获得定义Class时声明的父类的范型参数的类型
* @param clazz
* @return 返回第一个类型
*/
public static Class getSuperClassGenricType(Class clazz) {
return getSuperClassGenricType(clazz, 0);
}
/**
* 通过反射,获得定义Class时声明的父类的范型参数的类型
* @param clazz
* @param 返回某下标的类型
*/
public static Class getSuperClassGenricType(Class clazz, int index)
throws IndexOutOfBoundsException {
Type genType = clazz.getGenericSuperclass();
if (!(genType instanceof ParameterizedType)) {
return Object.class;
}
Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
if (index >= params.length || index < 0) {
return Object.class;
}
if (!(params[index] instanceof Class)) {
return Object.class;
}
return (Class) params[index];
}
}