泛型工具类GenericsUtils

原文地址:http://songjianyong.iteye.com/blog/1638417

Java代码   收藏代码
  1. package cn.com.tcgroup.yunlu.commons;  
  2.   
  3. import java.lang.reflect.Field;  
  4. import java.lang.reflect.Method;  
  5. import java.lang.reflect.ParameterizedType;  
  6. import java.lang.reflect.Type;  
  7. import java.util.ArrayList;  
  8. import java.util.List;  
  9.   
  10. /** 
  11.  * 泛型工具类 
  12.  */  
  13. public class GenericsUtils {  
  14.     /** 
  15.      * 通过反射,获得指定类的父类的泛型参数的实际类型. 如DaoSupport<Buyer> 
  16.      *  
  17.      * @param clazz 
  18.      *            clazz 需要反射的类,该类必须继承范型父类 
  19.      * @param index 
  20.      *            泛型参数所在索引,从0开始. 
  21.      * @return 范型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回 
  22.      *         <code>Object.class</code> 
  23.      */  
  24.     @SuppressWarnings("unchecked")  
  25.     public static Class getSuperClassGenricType(Class clazz, int index) {  
  26.   
  27.         Type genType = clazz.getGenericSuperclass();// 得到泛型父类  
  28.   
  29.         // 如果没有实现ParameterizedType接口,即不支持泛型,直接返回Object.class  
  30.         if (!(genType instanceof ParameterizedType)) {  
  31.   
  32.             return Object.class;  
  33.         }  
  34.   
  35.         // 返回表示此类型实际类型参数的Type对象的数组,数组里放的都是对应类型的Class, 如BuyerServiceBean extends  
  36.         // DaoSupport<Buyer,Contact>就返回Buyer和Contact类型  
  37.         Type[] params = ((ParameterizedType) genType).getActualTypeArguments();  
  38.         if (index >= params.length || index < 0) {  
  39.   
  40.             throw new RuntimeException("你输入的索引"  
  41.                     + (index < 0 ? "不能小于0" : "超出了参数的总数"));  
  42.         }  
  43.         if (!(params[index] instanceof Class)) {  
  44.   
  45.             return Object.class;  
  46.         }  
  47.         return (Class) params[index];  
  48.     }  
  49.   
  50.     /** 
  51.      * 通过反射,获得指定类的父类的第一个泛型参数的实际类型. 如DaoSupport<Buyer> 
  52.      *  
  53.      * @param clazz 
  54.      *            clazz 需要反射的类,该类必须继承泛型父类 
  55.      * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回 
  56.      *         <code>Object.class</code> 
  57.      */  
  58.     @SuppressWarnings("unchecked")  
  59.     public static Class getSuperClassGenricType(Class clazz) {  
  60.   
  61.         return getSuperClassGenricType(clazz, 0);  
  62.     }  
  63.   
  64.     /** 
  65.      * 通过反射,获得方法返回值泛型参数的实际类型. 如: public Map<String, Buyer> getNames(){} 
  66.      *  
  67.      * @param Method 
  68.      *            method 方法 
  69.      * @param int index 泛型参数所在索引,从0开始. 
  70.      * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回 
  71.      *         <code>Object.class</code> 
  72.      */  
  73.     @SuppressWarnings("unchecked")  
  74.     public static Class getMethodGenericReturnType(Method method, int index) {  
  75.   
  76.         Type returnType = method.getGenericReturnType();  
  77.   
  78.         if (returnType instanceof ParameterizedType) {  
  79.   
  80.             ParameterizedType type = (ParameterizedType) returnType;  
  81.             Type[] typeArguments = type.getActualTypeArguments();  
  82.   
  83.             if (index >= typeArguments.length || index < 0) {  
  84.   
  85.                 throw new RuntimeException("你输入的索引"  
  86.                         + (index < 0 ? "不能小于0" : "超出了参数的总数"));  
  87.             }  
  88.             return (Class) typeArguments[index];  
  89.         }  
  90.         return Object.class;  
  91.     }  
  92.   
  93.     /** 
  94.      * 通过反射,获得方法返回值第一个泛型参数的实际类型. 如: public Map<String, Buyer> getNames(){} 
  95.      *  
  96.      * @param Method 
  97.      *            method 方法 
  98.      * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回 
  99.      *         <code>Object.class</code> 
  100.      */  
  101.     @SuppressWarnings("unchecked")  
  102.     public static Class getMethodGenericReturnType(Method method) {  
  103.   
  104.         return getMethodGenericReturnType(method, 0);  
  105.     }  
  106.   
  107.     /** 
  108.      * 通过反射,获得方法输入参数第index个输入参数的所有泛型参数的实际类型. 如: public void add(Map<String, 
  109.      * Buyer> maps, List<String> names){} 
  110.      *  
  111.      * @param Method 
  112.      *            method 方法 
  113.      * @param int index 第几个输入参数 
  114.      * @return 输入参数的泛型参数的实际类型集合, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回空集合 
  115.      */  
  116.     @SuppressWarnings("unchecked")  
  117.     public static List<Class> getMethodGenericParameterTypes(Method method,  
  118.             int index) {  
  119.   
  120.         List<Class> results = new ArrayList<Class>();  
  121.         Type[] genericParameterTypes = method.getGenericParameterTypes();  
  122.   
  123.         if (index >= genericParameterTypes.length || index < 0) {  
  124.   
  125.             throw new RuntimeException("你输入的索引"  
  126.                     + (index < 0 ? "不能小于0" : "超出了参数的总数"));  
  127.         }  
  128.         Type genericParameterType = genericParameterTypes[index];  
  129.   
  130.         if (genericParameterType instanceof ParameterizedType) {  
  131.   
  132.             ParameterizedType aType = (ParameterizedType) genericParameterType;  
  133.             Type[] parameterArgTypes = aType.getActualTypeArguments();  
  134.             for (Type parameterArgType : parameterArgTypes) {  
  135.                 Class parameterArgClass = (Class) parameterArgType;  
  136.                 results.add(parameterArgClass);  
  137.             }  
  138.             return results;  
  139.         }  
  140.         return results;  
  141.     }  
  142.   
  143.     /** 
  144.      * 通过反射,获得方法输入参数第一个输入参数的所有泛型参数的实际类型. 如: public void add(Map<String, Buyer> 
  145.      * maps, List<String> names){} 
  146.      *  
  147.      * @param Method 
  148.      *            method 方法 
  149.      * @return 输入参数的泛型参数的实际类型集合, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回空集合 
  150.      */  
  151.     @SuppressWarnings("unchecked")  
  152.     public static List<Class> getMethodGenericParameterTypes(Method method) {  
  153.   
  154.         return getMethodGenericParameterTypes(method, 0);  
  155.     }  
  156.   
  157.     /** 
  158.      * 通过反射,获得Field泛型参数的实际类型. 如: public Map<String, Buyer> names; 
  159.      *  
  160.      * @param Field 
  161.      *            field 字段 
  162.      * @param int index 泛型参数所在索引,从0开始. 
  163.      * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回 
  164.      *         <code>Object.class</code> 
  165.      */  
  166.     @SuppressWarnings("unchecked")  
  167.     public static Class getFieldGenericType(Field field, int index) {  
  168.   
  169.         Type genericFieldType = field.getGenericType();  
  170.   
  171.         if (genericFieldType instanceof ParameterizedType) {  
  172.   
  173.             ParameterizedType aType = (ParameterizedType) genericFieldType;  
  174.             Type[] fieldArgTypes = aType.getActualTypeArguments();  
  175.             if (index >= fieldArgTypes.length || index < 0) {  
  176.   
  177.                 throw new RuntimeException("你输入的索引"  
  178.                         + (index < 0 ? "不能小于0" : "超出了参数的总数"));  
  179.             }  
  180.             return (Class) fieldArgTypes[index];  
  181.         }  
  182.         return Object.class;  
  183.     }  
  184.   
  185.     /** 
  186.      * 通过反射,获得Field泛型参数的实际类型. 如: public Map<String, Buyer> names; 
  187.      *  
  188.      * @param Field 
  189.      *            field 字段 
  190.      * @param int index 泛型参数所在索引,从0开始. 
  191.      * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回 
  192.      *         <code>Object.class</code> 
  193.      */  
  194.     @SuppressWarnings("unchecked")  
  195.     public static Class getFieldGenericType(Field field) {  
  196.   
  197.         return getFieldGenericType(field, 0);  
  198.     }  
  199. }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值