隨筆 - 55  文章 - 187  trackbacks - 0
          <2014年3月>
          2324252627281
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          常用鏈接

          留言簿(12)

          隨筆分類

          隨筆檔案

          groovy

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

           1package test;
           2
           3import java.lang.reflect.Method;
           4import java.lang.reflect.ParameterizedType;
           5import java.lang.reflect.Type;
           6import java.util.List;
           7import java.util.Map;
           8
           9public class TempTest {
          10
          11    public static void main(String[] args) throws Exception {
          12        Method[] methods = TempTest.class.getDeclaredMethods();
          13        for (Method method : methods) {
          14            System.out.println("method:" + method.getName());// 方法名
          15
          16            // //////////////方法的參數
          17            System.out.println(" paramTypeType: ");
          18            Type[] paramTypeList = method.getGenericParameterTypes();// 方法的參數列表
          19            for (Type paramType : paramTypeList) {
          20                System.out.println("  " + paramType);// 參數類型
          21                if (paramType instanceof ParameterizedType)/* 如果是泛型類型 */{
          22                    Type[] types = ((ParameterizedType) paramType)
          23                            .getActualTypeArguments();// 泛型類型列表
          24                    System.out.println("  TypeArgument: ");
          25                    for (Type type : types) {
          26                        System.out.println("   " + type);
          27                    }

          28                }

          29            }

          30
          31            // //////////////方法的返回值
          32            System.out.println(" returnType: ");
          33            Type returnType = method.getGenericReturnType();// 返回類型
          34            System.out.println("  " + returnType);
          35            if (returnType instanceof ParameterizedType)/* 如果是泛型類型 */{
          36                Type[] types = ((ParameterizedType) returnType)
          37                        .getActualTypeArguments();// 泛型類型列表
          38                System.out.println("  TypeArgument: ");
          39                for (Type type : types) {
          40                    System.out.println("   " + type);
          41                }

          42            }

          43
          44        }

          45
          46    }

          47
          48    public static String method1(List list) {
          49        return null;
          50    }

          51
          52    private static Map<String, Double> method2(Map<String, Object> map) {
          53        return null;
          54    }

          55
          56}
          posted on 2008-02-28 10:29 大衛 閱讀(5816) 評論(3)  編輯  收藏 所屬分類: Java

          FeedBack:
          # re: 通過反射獲取方法的參數和返回值的泛型信息 2008-03-02 14:42 xifu
          能人就是強悍,博主厲害  回復  更多評論
            
          # fd  2008-05-28 23:44 fdf
          fdfdf  回復  更多評論
            
          # re: 通過反射獲取方法的參數和返回值的泛型信息 2014-03-06 14:58 博主SB
          你自己試過沒,方法返回值的泛型是無法獲取的,只能獲取通用類型也就是 的泛型的定義, 別在那誤導人,垃圾  回復  更多評論
            
          主站蜘蛛池模板: 南宫市| 洛宁县| 汝南县| 靖宇县| 天长市| 鹤峰县| 枣庄市| 晋中市| 呼图壁县| 普安县| 永州市| 二手房| 嵩明县| 南靖县| 拜泉县| 云龙县| 吉林省| 晋宁县| 手游| 仙游县| 湟中县| 曲周县| 浦县| 马边| 平定县| 南昌市| 昭平县| 千阳县| 井陉县| 永春县| 正蓝旗| 阿拉善右旗| 昭觉县| 濮阳市| 宣恩县| 兴安盟| 广安市| 嘉禾县| 乡宁县| 松桃| 青海省|