反射獲取方法引數與返回值型別
阿新 • • 發佈:2019-01-26
{
14 System.out.println("method:"+ method.getName());// 方法名
15
16 ////////////////方法的引數17 System.out.println(" paramTypeType: ");
18 Type[] paramTypeList = method.getGenericParameterTypes();// 方法的引數列表19for (Type paramType : paramTypeList) {
20 System.out.println(""+ paramType); // 引數型別21if (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 }
14 System.out.println("method:"+ method.getName());// 方法名
15
16 ////////////////方法的引數17 System.out.println(" paramTypeType: ");
18 Type[] paramTypeList = method.getGenericParameterTypes();// 方法的引數列表19for (Type paramType : paramTypeList) {
20 System.out.println(""+ paramType);
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);
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 }