您的位置:首页 > 编程语言 > Java开发

Java获取泛型的Class的工具类

2014-02-08 16:29 357 查看
package com.grape.jxc2.utils;

import java.lang.reflect.ParameterizedType;

import java.lang.reflect.Type;

public class GenericsUtils {

/**

* 通过反射,获得定义Class时声明的父类的范型参数的类型.

* 如public BookManager extends GenricManager<Book>

*

* @param clazz The class to introspect

* @return the first generic declaration, or <code>Object.class</code> if cannot be determined

*/

@SuppressWarnings("rawtypes")

public static Class getSuperClassGenricType(Class clazz) {

return getSuperClassGenricType(clazz, 0);

}

/**

* 通过反射,获得定义Class时声明的父类的范型参数的类型.

* 如public BookManager extends GenricManager<Book>

*

* @param clazz clazz The class to introspect

* @param index the Index of the generic ddeclaration,start from 0.

*/

@SuppressWarnings("rawtypes")

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];

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: