隨筆 - 55  文章 - 187  trackbacks - 0
          <2008年2月>
          272829303112
          3456789
          10111213141516
          17181920212223
          2425262728291
          2345678

          常用鏈接

          留言簿(12)

          隨筆分類

          隨筆檔案

          groovy

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

           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            // //////////////方法的參數(shù)
          17            System.out.println(" paramTypeType: ");
          18            Type[] paramTypeList = method.getGenericParameterTypes();// 方法的參數(shù)列表
          19            for (Type paramType : paramTypeList) {
          20                System.out.println("  " + paramType);// 參數(shù)類型
          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 大衛(wèi) 閱讀(5817) 評(píng)論(3)  編輯  收藏 所屬分類: Java

          FeedBack:
          # re: 通過(guò)反射獲取方法的參數(shù)和返回值的泛型信息 2008-03-02 14:42 xifu
          能人就是強(qiáng)悍,博主厲害  回復(fù)  更多評(píng)論
            
          # fd  2008-05-28 23:44 fdf
          # re: 通過(guò)反射獲取方法的參數(shù)和返回值的泛型信息 2014-03-06 14:58 博主SB
          你自己試過(guò)沒(méi),方法返回值的泛型是無(wú)法獲取的,只能獲取通用類型也就是 的泛型的定義, 別在那誤導(dǎo)人,垃圾  回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 顺昌县| 吴桥县| 尖扎县| 兖州市| 阿鲁科尔沁旗| 澎湖县| 巢湖市| 台东县| 阿合奇县| 耿马| 土默特左旗| 农安县| 山西省| 四平市| 木兰县| 施秉县| 丽水市| 财经| 吉安市| 青海省| 信丰县| 甘德县| 阿巴嘎旗| 巴青县| 漾濞| 昌平区| 茂名市| 岐山县| 介休市| 铁力市| 夏邑县| 垫江县| 尼木县| 封开县| 许昌市| 高邑县| 科尔| 临澧县| 商南县| 平顶山市| 泰来县|