利用反射获取类或者方法或者字段上的注解的值

import java.lang.reflect.Field;  
import java.lang.reflect.InvocationTargetException;  
import java.lang.reflect.Method;  
  
/** 
 * 测试Annotation 
 * @author zkn 
 * 
 */  
@AnnotationTest02(getUserName="zhangsan")  
public class AnnotationTest03 {  
  
    @AnnotationTest01(color="red")  
    public static String testColor(String color){  
        System.out.println(color);  
        return color;  
    }  
      
    @AnnotationTest04(getAddress="北京市海淀区")  
    String address;  
      
    public static void main(String[] args) {  
        //获取方法上的注解值  
        Method[] methods = AnnotationTest03.class.getDeclaredMethods();  
        if(methods != null){  
            for(Method method : methods){  
                AnnotationTest01 annotation = method.getAnnotation(AnnotationTest01.class);  
                if(annotation == null)  
                    continue;  
                Method[] me = annotation.annotationType().getDeclaredMethods();  
                for(Method meth : me){  
                    try {  
                        String color = (String) meth.invoke(annotation,null);  
                        System.out.println(color);  
                    } catch (IllegalAccessException e) {  
                        e.printStackTrace();  
                    } catch (IllegalArgumentException e) {  
                        e.printStackTrace();  
                    } catch (InvocationTargetException e) {  
                        e.printStackTrace();  
                    }  
                }  
            }  
        }  
  
        //获取类上的注解值  
        AnnotationTest02 anno = AnnotationTest03.class.getAnnotation(AnnotationTest02.class);  
        if(anno != null){  
            Method[] met = anno.annotationType().getDeclaredMethods();  
            for(Method me : met ){  
                if(!me.isAccessible()){  
                    me.setAccessible(true);  
                }  
                try {  
                    System.out.println(me.invoke(anno, null));  
                } catch (IllegalAccessException e) {  
                    e.printStackTrace();  
                } catch (IllegalArgumentException e) {  
                    e.printStackTrace();  
                } catch (InvocationTargetException e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
          
        //获取字段上的注解值  
        AnnotationTest03 noon = new AnnotationTest03();  
        Field[] field = AnnotationTest03.class.getDeclaredFields();  
        if(field != null){  
            for(Field fie : field){  
                if(!fie.isAccessible()){  
                    fie.setAccessible(true);  
                }  
                AnnotationTest04 annon = fie.getAnnotation(AnnotationTest04.class);  
                Method[] meth = annon.annotationType().getDeclaredMethods();  
                for(Method me : meth){  
                    if(!me.isAccessible()){  
                        me.setAccessible(true);  
                    }  
                    try {  
                        //给字段重新赋值  
                        fie.set(noon, me.invoke(annon, null));  
                        System.out.println(fie.get(noon));  
                    } catch (IllegalAccessException e) {  
                        e.printStackTrace();  
                    } catch (IllegalArgumentException e) {  
                        e.printStackTrace();  
                    } catch (InvocationTargetException e) {  
                        e.printStackTrace();  
                    }  
                }  
                  
            }  
        }  
    }  
}  
  1. import java.lang.reflect.Field;  
  2. import java.lang.reflect.InvocationTargetException;  
  3. import java.lang.reflect.Method;  
  4.   
  5. /** 
  6.  * 测试Annotation 
  7.  * @author zkn 
  8.  * 
  9.  */  
  10. @AnnotationTest02(getUserName="zhangsan")  
  11. public class AnnotationTest03 {  
  12.   
  13.     @AnnotationTest01(color="red")  
  14.     public static String testColor(String color){  
  15.         System.out.println(color);  
  16.         return color;  
  17.     }  
  18.       
  19.     @AnnotationTest04(getAddress="北京市海淀区")  
  20.     String address;  
  21.       
  22.     public static void main(String[] args) {  
  23.         //获取方法上的注解值  
  24.         Method[] methods = AnnotationTest03.class.getDeclaredMethods();  
  25.         if(methods != null){  
  26.             for(Method method : methods){  
  27.                 AnnotationTest01 annotation = method.getAnnotation(AnnotationTest01.class);  
  28.                 if(annotation == null)  
  29.                     continue;  
  30.                 Method[] me = annotation.annotationType().getDeclaredMethods();  
  31.                 for(Method meth : me){  
  32.                     try {  
  33.                         String color = (String) meth.invoke(annotation,null);  
  34.                         System.out.println(color);  
  35.                     } catch (IllegalAccessException e) {  
  36.                         e.printStackTrace();  
  37.                     } catch (IllegalArgumentException e) {  
  38.                         e.printStackTrace();  
  39.                     } catch (InvocationTargetException e) {  
  40.                         e.printStackTrace();  
  41.                     }  
  42.                 }  
  43.             }  
  44.         }  
  45.   
  46.         //获取类上的注解值  
  47.         AnnotationTest02 anno = AnnotationTest03.class.getAnnotation(AnnotationTest02.class);  
  48.         if(anno != null){  
  49.             Method[] met = anno.annotationType().getDeclaredMethods();  
  50.             for(Method me : met ){  
  51.                 if(!me.isAccessible()){  
  52.                     me.setAccessible(true);  
  53.                 }  
  54.                 try {  
  55.                     System.out.println(me.invoke(anno, null));  
  56.                 } catch (IllegalAccessException e) {  
  57.                     e.printStackTrace();  
  58.                 } catch (IllegalArgumentException e) {  
  59.                     e.printStackTrace();  
  60.                 } catch (InvocationTargetException e) {  
  61.                     e.printStackTrace();  
  62.                 }  
  63.             }  
  64.         }  
  65.           
  66.         //获取字段上的注解值  
  67.         AnnotationTest03 noon = new AnnotationTest03();  
  68.         Field[] field = AnnotationTest03.class.getDeclaredFields();  
  69.         if(field != null){  
  70.             for(Field fie : field){  
  71.                 if(!fie.isAccessible()){  
  72.                     fie.setAccessible(true);  
  73.                 }  
  74.                 AnnotationTest04 annon = fie.getAnnotation(AnnotationTest04.class);  
  75.                 Method[] meth = annon.annotationType().getDeclaredMethods();  
  76.                 for(Method me : meth){  
  77.                     if(!me.isAccessible()){  
  78.                         me.setAccessible(true);  
  79.                     }  
  80.                     try {  
  81.                         //给字段重新赋值  
  82.                         fie.set(noon, me.invoke(annon, null));  
  83.                         System.out.println(fie.get(noon));  
  84.                     } catch (IllegalAccessException e) {  
  85.                         e.printStackTrace();  
  86.                     } catch (IllegalArgumentException e) {  
  87.                         e.printStackTrace();  
  88.                     } catch (InvocationTargetException e) {  
  89.                         e.printStackTrace();  
  90.                     }  
  91.                 }  
  92.                   
  93.             }  
  94.         }  
  95.     }  
  96. }  

有追求,才有动力!

向每一个软件工程师致敬!

by wujf

mail:921252375@qq.com

原文地址:https://www.cnblogs.com/wujf/p/9115329.html