Chinaunix首页 | 论坛 | 博客
  • 博客访问: 533469
  • 博文数量: 135
  • 博客积分: 3568
  • 博客等级: 中校
  • 技术积分: 1942
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-19 17:52
文章分类

全部博文(135)

文章存档

2012年(29)

2011年(41)

2010年(26)

2009年(12)

2008年(9)

2007年(12)

2006年(6)

分类: Java

2011-04-29 21:48:01


  1. package me.test;

  2. import java.lang.reflect.Field;
  3. import java.lang.reflect.Method;
  4. import java.lang.reflect.ParameterizedType;
  5. import java.lang.reflect.Type;
  6. import java.util.List;
  7. import java.util.Map;
  8. /**
  9.  * 如何在运行时获取某一个类的泛型信息?
  10.  *
  11.  *
  12.  * 参考:http://blog.xebia.com/2009/02/acessing-generic-types-at-runtime-in-java/
  13.  *
  14.  * @author btpka3@163.com
  15.  *
  16.  * 运行结果:
  17. ============================类的泛型信息 :
  18. 接口:interface me.test.Main$I:true
  19.     泛型[0] = class java.lang.Class
  20.     泛型[1] = class java.lang.Class
  21.   声明于 = class me.test.Main
  22. 接口:interface java.lang.Runnable:

  23. ============================方法的泛型信息 :
  24. 方法:public void me.test.Main$C.run():
  25.   返回值 = void
  26.   声明于 = class me.test.Main$C
  27. 方法:public java.lang.Integer me.test.Main$C.get(java.lang.String):
  28.   参数[0] = class java.lang.String
  29.   返回值 = class java.lang.Integer
  30.   声明于 = class me.test.Main$C
  31. 方法:public java.util.Map me.test.Main$C.get(java.util.List):
  32.   参数[0] = interface java.util.List
  33.     泛型[0] = class java.lang.Class
  34.   返回值 = interface java.util.Map
  35.     泛型[0] = class java.lang.Class
  36.     泛型[1] = class java.lang.Class
  37.   声明于 = class me.test.Main$C
  38. 方法:public java.lang.Object me.test.Main$C.get(java.lang.Object):
  39.   参数[0] = class java.lang.Object
  40.   返回值 = class java.lang.Object
  41.   声明于 = class me.test.Main$C

  42. ============================获取类C的字段的泛型信息 :
  43. 字段:list:
  44.   类型= interface java.util.List
  45.     泛型[0] = class sun.reflect.generics.reflectiveObjects.WildcardTypeImpl
  46.  */
  47. public class Main {
  48.     // 我们定义了一个泛型的接口 I
  49.     public static interface I<K, V> {
  50.         V get(K k);
  51.     }

  52.     // 我们定义了实现该接口类 C
  53.     public static class C implements I<String, Integer>, Runnable {
  54.         public List<?> list;
  55.         @Override
  56.         public void run() {};
  57.         @Override
  58.         public Integer get(String k) { return null; }
  59.         public Map<String, String> get(List<Object> l) { return null; }
  60.     }

  61.     //我们如何在运行时获取类C中各个字段,方法上的泛型信息呢?
  62.     public static void main(String[] args) {
  63.         System.out.println("============================类的泛型信息 :");
  64.         for (Type t : C.class.getGenericInterfaces()) {
  65.             if (t instanceof ParameterizedType) {
  66.                 ParameterizedType pT = (ParameterizedType) t;
  67.                 System.out.println("接口:" + pT.getRawType() + ":" + (pT.getRawType() == I.class));
  68.                 Type[] types = pT.getActualTypeArguments();
  69.                 for (int i = 0; i < types.length; i++) {
  70.                     System.out.println("    泛型[" + i + "] = " + types[i].getClass());
  71.                 }
  72.                 System.out.println("  声明于 = " + pT.getOwnerType());
  73.             } else {
  74.                 System.out.println("接口:" + t + ":");
  75.             }
  76.         }
  77.         System.out.println();
  78.         System.out.println("============================方法的泛型信息 :");
  79.         Method[] methodArray = C.class.getDeclaredMethods();
  80.         for (Method m : methodArray) {
  81.             System.out.println("方法:" + m + ":");
  82.             Type[] argTypes = m.getGenericParameterTypes();
  83.             for (int i = 0; i < argTypes.length; i++) {
  84.                 Type t = argTypes[0];
  85.                 if (t instanceof ParameterizedType) {
  86.                     ParameterizedType pT = (ParameterizedType) t;
  87.                     System.out.println("  参数[" + i + "] = " + pT.getRawType());
  88.                     Type[] types = pT.getActualTypeArguments();
  89.                     for (int j = 0; j < types.length; j++) {
  90.                         System.out.println("    泛型[" + j + "] = " + types[j].getClass());
  91.                     }
  92.                 } else {
  93.                     System.out.println("  参数[" + i + "] = " + t);
  94.                 }
  95.             }
  96.             Type rtnType = m.getGenericReturnType();
  97.             if (rtnType instanceof ParameterizedType) {
  98.                 ParameterizedType pT = (ParameterizedType) rtnType;
  99.                 System.out.println("  返回值 = " + pT.getRawType());
  100.                 Type[] types = pT.getActualTypeArguments();
  101.                 for (int j = 0; j < types.length; j++) {
  102.                     System.out.println("    泛型[" + j + "] = " + types[j].getClass());
  103.                 }
  104.             } else {
  105.                 System.out.println("  返回值 = " + rtnType);
  106.             }
  107.             System.out.println("  声明于 = " + m.getDeclaringClass());

  108.         }

  109.         System.out.println();
  110.         System.out.println("============================获取类C的字段的泛型信息 :");
  111.         for (Field f : C.class.getDeclaredFields()) {
  112.             Type t = f.getGenericType();
  113.             System.out.println("字段:" + f.getName() + ":");
  114.             if (t instanceof ParameterizedType) {
  115.                 ParameterizedType pT = (ParameterizedType) t;
  116.                 System.out.println("  类型= " + pT.getRawType());
  117.                 Type[] types = pT.getActualTypeArguments();
  118.                 for (int j = 0; j < types.length; j++) {
  119.                     System.out.println("    泛型[" + j + "] = " + types[j].getClass());
  120.                 }
  121.             } else {
  122.                 System.out.println("字段:" + f.getName() + ":");
  123.                 System.out.println("  类型= " + t);
  124.             }
  125.         }
  126.     }
  127. }



阅读(2027) | 评论(0) | 转发(0) |
0

上一篇:ANT 个人笔记

下一篇:免费 SVN 空间

给主人留下些什么吧!~~