package com.sanqing.util;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
/**
* 泛型工具类
*
*/
public class GenericsUtils {
/**
* 通过反射,获得指定类的父类的泛型参数的实际类型. 如BuyerServiceBean extends DaoSupport<Buyer>
*
* @param clazz clazz 需要反射的类,该类必须继承范型父类
* @param index 泛型参数所在索引,从0开始.
* @return 范型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回<code>Object.class</code>
*/
@SuppressWarnings("unchecked")
public static Class getSuperClassGenricType(Class clazz, int index) {
Type genType = clazz.getGenericSuperclass();//得到泛型父类
//如果没有实现ParameterizedType接口,即不支持泛型,直接返回Object.class
if (!(genType instanceof ParameterizedType)) {
return Object.class;
}
//返回表示此类型实际类型参数的Type对象的数组,数组里放的都是对应类型的Class, 如BuyerServiceBean extends DaoSupport<Buyer,Contact>就返回Buyer和Contact类型
Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
if (index >= params.length || index < 0) {
throw new RuntimeException("你输入的索引"+ (index<0 ? "不能小于0" : "超出了参数的总数"));
}
if (!(params[index] instanceof Class)) {
return Object.class;
}
return (Class) params[index];
}
/**
* 通过反射,获得指定类的父类的第一个泛型参数的实际类型. 如BuyerServiceBean extends DaoSupport<Buyer>
*
* @param clazz clazz 需要反射的类,该类必须继承泛型父类
* @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回<code>Object.class</code>
*/
@SuppressWarnings("unchecked")
public static Class getSuperClassGenricType(Class clazz) {
return getSuperClassGenricType(clazz,0);
}
/**
* 通过反射,获得方法返回值泛型参数的实际类型. 如: public Map<String, Buyer> getNames(){}
*
* @param Method method 方法
* @param int index 泛型参数所在索引,从0开始.
* @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回<code>Object.class</code>
*/
@SuppressWarnings("unchecked")
public static Class getMethodGenericReturnType(Method method, int index) {
Type returnType = method.getGenericReturnType();
if(returnType instanceof ParameterizedType){
ParameterizedType type = (ParameterizedType) returnType;
Type[] typeArguments = type.getActualTypeArguments();
if (index >= typeArguments.length || index < 0) {
throw new RuntimeException("你输入的索引"+ (index<0 ? "不能小于0" : "超出了参数的总数"));
}
return (Class)typeArguments[index];
}
return Object.class;
}
/**
* 通过反射,获得方法返回值第一个泛型参数的实际类型. 如: public Map<String, Buyer> getNames(){}
*
* @param Method method 方法
* @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回<code>Object.class</code>
*/
@SuppressWarnings("unchecked")
public static Class getMethodGenericReturnType(Method method) {
return getMethodGenericReturnType(method, 0);
}
/**
* 通过反射,获得方法输入参数第index个输入参数的所有泛型参数的实际类型. 如: public void add(Map<String, Buyer> maps, List<String> names){}
*
* @param Method method 方法
* @param int index 第几个输入参数
* @return 输入参数的泛型参数的实际类型集合, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回空集合
*/
@SuppressWarnings("unchecked")
public static List<Class> getMethodGenericParameterTypes(Method method, int index) {
List<Class> results = new ArrayList<Class>();
Type[] genericParameterTypes = method.getGenericParameterTypes();
if (index >= genericParameterTypes.length ||index < 0) {
throw new RuntimeException("你输入的索引"+ (index<0 ? "不能小于0" : "超出了参数的总数"));
}
Type genericParameterType = genericParameterTypes[index];
if(genericParameterType instanceof ParameterizedType){
ParameterizedType aType = (ParameterizedType) genericParameterType;
Type[] parameterArgTypes = aType.getActualTypeArguments();
for(Type parameterArgType : parameterArgTypes){
Class parameterArgClass = (Class) parameterArgType;
results.add(parameterArgClass);
}
return results;
}
return results;
}
/**
* 通过反射,获得方法输入参数第一个输入参数的所有泛型参数的实际类型. 如: public void add(Map<String, Buyer> maps, List<String> names){}
*
* @param Method method 方法
* @return 输入参数的泛型参数的实际类型集合, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回空集合
*/
@SuppressWarnings("unchecked")
public static List<Class> getMethodGenericParameterTypes(Method method) {
return getMethodGenericParameterTypes(method, 0);
}
/**
* 通过反射,获得Field泛型参数的实际类型. 如: public Map<String, Buyer> names;
*
* @param Field field 字段
* @param int index 泛型参数所在索引,从0开始.
* @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回<code>Object.class</code>
*/
@SuppressWarnings("unchecked")
public static Class getFieldGenericType(Field field, int index) {
Type genericFieldType = field.getGenericType();
if(genericFieldType instanceof ParameterizedType){
ParameterizedType aType = (ParameterizedType) genericFieldType;
Type[] fieldArgTypes = aType.getActualTypeArguments();
if (index >= fieldArgTypes.length || index < 0) {
throw new RuntimeException("你输入的索引"+ (index<0 ? "不能小于0" : "超出了参数的总数"));
}
return (Class)fieldArgTypes[index];
}
return Object.class;
}
/**
* 通过反射,获得Field泛型参数的实际类型. 如: public Map<String, Buyer> names;
*
* @param Field field 字段
* @param int index 泛型参数所在索引,从0开始.
* @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回<code>Object.class</code>
*/
@SuppressWarnings("unchecked")
public static Class getFieldGenericType(Field field) {
return getFieldGenericType(field, 0);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
计算机毕业设计-报价管理系统-Java Web项目源码.zip (234个子文件)
DaoSupport.class 8KB
ProductAction.class 5KB
OrderAction.class 5KB
QuotationAction.class 5KB
ProductManageAction.class 5KB
CustomerAction.class 4KB
OrderManageAction.class 4KB
QuotationManageAction.class 4KB
GenericsUtils.class 3KB
CustomerManageAction.class 3KB
ProductTypeAction.class 3KB
Product.class 3KB
PageView.class 3KB
UserManageAction.class 3KB
UserAction.class 2KB
ProductTypeManageAction.class 2KB
Quotation.class 2KB
Order.class 2KB
Customer.class 2KB
LoginAction.class 2KB
DateConverter.class 1KB
UserServiceImpl.class 1KB
LoginIntercepter.class 1KB
DAO.class 1KB
User.class 1KB
PageIndex.class 1KB
ProductType.class 1KB
QueryResult.class 1KB
QuotationServiceImpl.class 1KB
OrderServiceImpl.class 1004B
BaseAction.class 948B
ProductTypeServiceImpl.class 599B
CustomerServiceImpl.class 581B
ProductServiceImpl.class 575B
UserService.class 293B
ProductTypeService.class 256B
QuotationService.class 250B
CustomerService.class 247B
ProductService.class 244B
OrderService.class 238B
.classpath 5KB
oa.css 6KB
logo_bg.gif 32KB
login_2.gif 9KB
logo.gif 7KB
logo_copyright_bg.gif 5KB
userLogin_button.gif 2KB
userLogin.gif 2KB
addpic.gif 1KB
title_middle.gif 1KB
login_5.gif 1KB
tip.gif 1005B
cz.gif 940B
title_left.gif 544B
arrowdown.gif 424B
arrowup.gif 407B
title_right.gif 303B
userLogin_down.gif 177B
dela.gif 121B
file1.gif 89B
logo_under_line.gif 62B
main.html 3KB
outlook.html 3KB
back_index.html 377B
spring.jar 2.71MB
hibernate-core.jar 2.16MB
hibernate3.jar 2.08MB
aspectjweaver.jar 1.81MB
freemarker-2.3.12.jar 843KB
struts2-core-2.1.2.jar 697KB
testng-5.1.jar 676KB
commons-collections-3.2.jar 558KB
commons-collections-3.1.jar 546KB
mysql-connector-java-5.0.8-bin.jar 528KB
xwork-2.1.1.jar 510KB
dwr.jar 478KB
proxool-0.8.3.jar 465KB
javassist.jar 460KB
antlr-2.7.6.jar 433KB
standard.jar 403KB
hibernate-cglib-repack-2.1_3.jar 377KB
xstream-1.2.2.jar 364KB
log4j-1.2.11.jar 342KB
dom4j-1.6.1.jar 307KB
dom4j.jar 307KB
cglib-2.1.3.jar 276KB
hibernate-annotations.jar 273KB
commons-lang-2.3.jar 240KB
jaxen-1.1-beta-7.jar 222KB
plexus-container-default-1.0-alpha-10.jar 214KB
commons-beanutils-1.7.0.jar 184KB
plexus-utils-1.2.jar 169KB
commons-digester-1.6.jar 164KB
ognl-2.6.11.jar 164KB
tiles-core-2.0.5.jar 122KB
commons-dbcp.jar 119KB
junit-3.8.1.jar 118KB
commons-validator-1.3.0.jar 117KB
hibernate-entitymanager.jar 116KB
ehcache-1.2.jar 115KB
共 234 条
- 1
- 2
- 3
资源评论
mylife512
- 粉丝: 1461
- 资源: 1万+
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 微信小程序 - 图书管理系统源码.zip
- 微信小程序 - 图片自适应 ,富文本解析源码.zip
- 微信小程序 - 同乐居商城:购物车合算源码
- 1、根据输入的三条边值判断能组成何种三角形,并设计测试数据进行判定覆盖测试 三条边为变量a、b、c,范围为1≤边值≤10,不在范
- SQL server 练习题目8道(小白教学).zip
- Python 手写实现 iD3 决策树算法-根据信息增益公式.zip
- 411675952289057车联助手-小窗版(三星)3.5.1.apk
- 三种快速排序方法合并在一个文件中以便直接运行的Python代码示例
- 937712277954201实习5.word
- 2程序语言基础知识pdf1_1716337722703.jpeg
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功