Method详解

http://blog.csdn.net/zhangquanit/article/details/52927216

http://blog.csdn.net/zhangquanit/article/details/52927216

http://blog.csdn.net/zhangquanit/article/details/52927216


Method详解

标签: Java反射Method
  184人阅读  评论(0)  收藏  举报
  分类:
Method 提供关于类或接口上单独某个方法(以及如何访问该方法)的信息。
一个完整方法包含的属性有:
方法上使用的注解、方法的修饰符、方法上定义的泛型参数、方法的返回值、方法名称、方法参数(泛型、注解)、方法抛出的异常。

比如下面这个方法:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @MyAnnotation  
  2. public <T> boolean add(List<T> list,T...params)throws RuntimeException,Exception{  
  3.     if(null==list){  
  4.         throw new RuntimeException("list=null");  
  5.     }  
  6.       
  7.     if(null==params){  
  8.         return false;  
  9.     }  
  10.     //将参数添加到List集合中  
  11.     if(null!=params){  
  12.         for(T t:params){  
  13.             list.add(t);  
  14.         }  
  15.     }  
  16.     return true;  
  17. }  

注解:@MyAnnotation
修饰符:public
泛型参数:T
返回值:boolean
方法名:add
方法参数(泛型、注解):List<T> list,T...params
抛出的异常:RuntimeException,Exception


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package reflect;  
  2.   
  3. import java.lang.annotation.Annotation;  
  4. import java.lang.reflect.GenericArrayType;  
  5. import java.lang.reflect.Method;  
  6. import java.lang.reflect.Modifier;  
  7. import java.lang.reflect.ParameterizedType;  
  8. import java.lang.reflect.Type;  
  9. import java.lang.reflect.TypeVariable;  
  10. import java.lang.reflect.WildcardType;  
  11. import java.util.ArrayList;  
  12. import java.util.Arrays;  
  13. import java.util.List;  
  14.   
  15. /** 
  16.  * @author zhangquanit 
  17.  */  
  18. public class MethodTest {  
  19.   
  20.     /** 
  21.      * 一个完整方法包含的属性有: 方法上使用的注解、方法的修饰符、方法上定义的泛型参数、方法的返回值、方法名称、方法参数(泛型、注解)、方法抛出的异常 
  22.      */  
  23.     @MyAnnotation  
  24.     private <T> boolean add(@MyAnnotation List<T> list, T... params) throws RuntimeException,  
  25.             Exception {  
  26.         if (null == list) {  
  27.             throw new RuntimeException("list=null");  
  28.         }  
  29.   
  30.         if (null == params) {  
  31.             return false;  
  32.         }  
  33.         // 将参数添加到List集合中  
  34.         if (null != params) {  
  35.             for (T t : params) {  
  36.                 list.add(t);  
  37.             }  
  38.         }  
  39.         return true;  
  40.     }  
  41.   
  42.     public static void main(String[] args) throws Exception {  
  43.         // 获取Method  
  44.         MethodTest obj = new MethodTest();  
  45.         Class<? extends MethodTest> clazz = obj.getClass();  
  46.   
  47.         Method method = clazz.getDeclaredMethod("add", List.class,  
  48.                 Object[].class);  
  49.         if (!method.isAccessible()) {  
  50.             method.setAccessible(true);  
  51.         }  
  52.         //获取方法基本信息  
  53.         getMethodInfo(method);  
  54.           
  55.         // 调用方法  
  56.         List<String> arrayList = new ArrayList<String>();  
  57.         method.invoke(obj, arrayList, new String[] { "1""2" });  
  58.         System.out.println(arrayList);//[1,2]  
  59.           
  60.         //方法定义所在的类  
  61.         Class<?> declaringClass = method.getDeclaringClass();  
  62.           
  63.         // 如果此方法是 bridge 方法,则返回 true;  
  64.         boolean bridge = method.isBridge();  
  65.         //如果该方法是public非抽象非静态,且定义在接口中,则返回true  
  66.         boolean default1 = method.isDefault(); //false  
  67.         //如果此方法为复合方法,则返回 true;  
  68.         boolean synthetic = method.isSynthetic();//false  
  69.         // 如果将此方法的参数带有可变参数,则返回 true  
  70.         boolean varArgs = method.isVarArgs(); //true  
  71.           
  72.           
  73.           
  74.     }  
  75.   
  76.     private static void getMethodInfo(Method method) {  
  77.         // 1、获取方法上的注解  
  78.         boolean annotationPresent = method  
  79.                 .isAnnotationPresent(MyAnnotation.class);  
  80.         if (annotationPresent) {  
  81.             MyAnnotation myAnnotation = method  
  82.                     .getDeclaredAnnotation(MyAnnotation.class);  
  83.         }  
  84.         // 2、方法的修饰符  
  85.         int modifiers = method.getModifiers();  
  86.         String modify = Modifier.toString(modifiers);// private  
  87.         // 3、方法上定义的泛型参数  
  88.         TypeVariable<Method>[] typeParameters = method.getTypeParameters();// [T]  
  89.         // 4、方法的返回值  
  90.         Class<?> returnType = method.getReturnType();// boolean  
  91.         Type genericReturnType = method.getGenericReturnType();// boolean  
  92.         // 5、方法名称  
  93.         String name = method.getName();  
  94.         // 6、方法参数  
  95.         int parameterCount = method.getParameterCount();// 参数个数 2  
  96.         // 方法参数——泛型  
  97.         Class<?>[] parameterTypes = method.getParameterTypes();  
  98.         // 打印 [interface java.util.List, class [Ljava.lang.Object;]  
  99.         Type[] genericParameterTypes = method.getGenericParameterTypes();  
  100.         // 打印 [java.util.List<T>, T[]]  
  101.         for (Type type : genericParameterTypes) {  
  102.             if (type instanceof ParameterizedType) { // 参数类型  
  103.                 System.out.println("ParameterizedType类型:" + type);  
  104.                 ParameterizedType parameterizedType = (ParameterizedType) type;  
  105.                 Type[] actualTypeArguments = parameterizedType  
  106.                         .getActualTypeArguments();  
  107.                 System.out.println("实际参数为:"  
  108.                         + Arrays.toString(actualTypeArguments));  
  109.                 for (Type actualType : actualTypeArguments) {  
  110.                     if (actualType instanceof WildcardType) {  
  111.                         WildcardType wildcardType = (WildcardType) actualTypeArguments[0];  
  112.                         System.out.println("实际参数为WildcardType类型:"  
  113.                                 + wildcardType.getUpperBounds());  
  114.                     } else if (actualType instanceof Class) {  
  115.                         System.out.println("实际参数为Class类型:" + actualType);  
  116.                     }  
  117.                 }  
  118.   
  119.             } else if (type instanceof GenericArrayType) { // 泛型数组类型 T[]  
  120.                 GenericArrayType genericArrayType = (GenericArrayType) type;  
  121.                 System.out.println("GenericArrayType类型:"  
  122.                         + genericArrayType.getGenericComponentType());//T  
  123.             } else if (type instanceof TypeVariable) { // 泛型变量  
  124.                 System.out.println("TypeVariable类型:" + type);  
  125.             } else if (type instanceof Class) { //  
  126.                 System.out.println("Class类型:" + type);  
  127.             }  
  128.         }  
  129.         /* 
  130.          * 方法有2个参数,第一个参数list为ParameterizedType,实际参数为T, 
  131.          * 第二个参数为GenericArrayType泛型数组类型T[],数组元素类型为T 
  132.          */  
  133.           
  134.         //方法参数——注解   第一个参数使用了注解  
  135.         Annotation[][] parameterAnnotations = method.getParameterAnnotations();  
  136.         Annotation myAnnotation=parameterAnnotations[0][0];  
  137.         //打印 @reflect.MyAnnotation(intValue=0)  
  138.           
  139.   
  140.         // 7、方法抛出的异常  
  141.         Class<?>[] exceptionTypes = method.getExceptionTypes();  
  142.         // 打印 [class java.lang.RuntimeException, class java.lang.Exception]  
  143.         Type[] genericExceptionTypes = method.getGenericExceptionTypes();  
  144.         // 打印 [class java.lang.RuntimeException, class java.lang.Exception]  
  145.   
  146.     }  
  147. }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值