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);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
资源内容:项目全套源码+完整文档 源码说明: 全部项目源码都是经过测试校正后百分百成功运行。 SpringBoot 毕业设计,SpringBoot 课程设计,基于SpringBoot+Vue开发的,含有代码注释,新手也可看懂。ssm整合开发,小程序毕业设计、期末大作业、课程设计、高分必看,下载下来,简单部署,就可以使用。 包含:项目源码、数据库脚本、软件工具等,该项目可以作为毕设、课程设计使用,前后端代码都在里面。 该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值。 项目都经过严格调试,确保可以运行! 1. 技术组成 前端:Vue/JSP/React/HTML+JS+CSS/ASP 后台框架代码:java/c/c++/php/VB/lun/Andorid/Python 开发环境:idea 数据库:MySql(建议用 5.7,8.0 有时候会有坑) 部署环境:Tomcat(建议用 7.x 或者 8.x b版本),maven
资源推荐
资源详情
资源评论
收起资源包目录
计算机毕业设计-报价管理系统-源码.rar (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
资源评论
程序员小马软件开发定制
- 粉丝: 5009
- 资源: 2246
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功