java中自定義註解並通過反射獲取註解屬性值

直接上代碼,註釋中有說明:

1、定義自定義註解類(類註解和字段註解)

[java] view plain copy
  1. package com.uno.ray;  
  2.   
  3. import java.lang.annotation.Documented;  
  4. import java.lang.annotation.ElementType;  
  5. import java.lang.annotation.Inherited;  
  6. import java.lang.annotation.Retention;  
  7. import java.lang.annotation.RetentionPolicy;  
  8. import java.lang.annotation.Target;  
  9. import java.net.Authenticator.RequestorType;  
  10.   
  11. /** 
  12.  * 自定義註解 
  13.  * @author Uno 
  14.  *@Documented:指明該註解可以用於生成doc 
  15.  *@Inherited:該註解可以被自動繼承 
  16.  *@Retention:指明在什麼級別顯示該註解: 
  17.  *  RetentionPolicy.SOURCE 註解存在於源代碼中,編譯時會被拋棄 
  18.     RetentionPolicy.CLASS 註解會被編譯到class文件中,但是JVM會忽略 
  19.     RetentionPolicy.RUNTIME JVM會讀取註解,同時會保存到class文件中 
  20.   @Target:指明該註解可以註解的程序範圍 
  21.     ElementType.TYPE 用於類,接口,枚舉但不能是註解 
  22.     ElementType.FIELD 作用於字段,包含枚舉值 
  23.     ElementType.METHOD 作用於方法,不包含構造方法 
  24.     ElementType.PARAMETER 作用於方法的參數 
  25.     ElementType.CONSTRUCTOR 作用於構造方法 
  26.     ElementType.LOCAL_VERIABLE 作用於本地變量或者catch語句 
  27.     ElementType.ANNOTATION_TYPE 作用於註解 
  28.     ElementType.PACKAGE 作用於包 
  29.  */  
  30. @Documented  
  31. @Inherited  
  32. @Retention(RetentionPolicy.RUNTIME)  
  33. @Target({ElementType.TYPE, ElementType.FIELD})//次註解作用於類和字段上  
  34. public @interface FieldTypeAnnotation {  
  35.     /** 
  36.      *leip 2016年12月3日 
  37.      *TODO 
  38.     **/  
  39.     String type() default "ignore";  
  40.     int age() default 27;  
  41.     String[] hobby(); //沒有指定defalut的,需要在註解的時候顯式指明  
  42. }  
2、(方法註解)

[java] view plain copy
  1. package com.uno.ray;  
  2.   
  3. import java.lang.annotation.Documented;  
  4. import java.lang.annotation.ElementType;  
  5. import java.lang.annotation.Inherited;  
  6. import java.lang.annotation.Retention;  
  7. import java.lang.annotation.RetentionPolicy;  
  8. import java.lang.annotation.Target;  
  9.   
  10. /** 
  11.  *  
  12.  * @author Uno 
  13.  * 
  14.  */  
  15. @Documented  
  16. @Inherited  
  17. @Retention(RetentionPolicy.RUNTIME)  
  18. @Target(ElementType.METHOD) //次註解只能作用於方法上  
  19. public @interface MethodAnnotation {  
  20.   
  21.     String desc() default "method1";  
  22. }  

3、定義測試類:(反射類)

[java] view plain copy
  1. package com.uno.ray;  
  2.   
  3. import java.awt.Dialog.ModalityType;  
  4. import java.lang.reflect.Field;  
  5. import java.lang.reflect.Method;  
  6. import java.util.Arrays;  
  7.   
  8. @FieldTypeAnnotation(type = "class", hobby = { "smoke" })  
  9. public class ReflectAnnotation {  
  10.     /** 
  11.      * leip 2016年12月3日 TODO 
  12.      **/  
  13.     @FieldTypeAnnotation(hobby = { "sleep""play" })  
  14.     private String maomao;  
  15.   
  16.     @FieldTypeAnnotation(hobby = { "phone""buy" }, age = 27, type = "normal")  
  17.     private String zhangwenping;  
  18.       
  19.     @MethodAnnotation()  
  20.     public void method1() {  
  21.           
  22.     }  
  23.     @MethodAnnotation(desc="method2")  
  24.     public void method2() {  
  25.           
  26.     }  
  27.   
  28.     public static void main(String[] args) {  
  29.         // 此處要用反射將字段中的註解解析出來  
  30.         Class<ReflectAnnotation> clz = ReflectAnnotation.class;  
  31.         // 判斷類上是否有次註解  
  32.         boolean clzHasAnno = clz.isAnnotationPresent(FieldTypeAnnotation.class);  
  33.         if (clzHasAnno) {  
  34.             // 獲取類上的註解  
  35.             FieldTypeAnnotation annotation = clz.getAnnotation(FieldTypeAnnotation.class);  
  36.             // 輸出註解上的屬性  
  37.             int age = annotation.age();  
  38.             String[] hobby = annotation.hobby();  
  39.             String type = annotation.type();  
  40.             System.out.println(clz.getName() + " age = " + age + ", hobby = " + Arrays.asList(hobby).toString() + " type = " + type);  
  41.         }  
  42.         // 解析字段上是否有註解  
  43.         // ps:getDeclaredFields會返回類所有聲明的字段,包括private、protected、public,但是不包括父類的  
  44.         // getFields:則會返回包括父類的所有的public字段,和getMethods()一樣  
  45.         Field[] fields = clz.getDeclaredFields();  
  46.         for(Field field : fields){  
  47.             boolean fieldHasAnno = field.isAnnotationPresent(FieldTypeAnnotation.class);  
  48.             if(fieldHasAnno){  
  49.                 FieldTypeAnnotation fieldAnno = field.getAnnotation(FieldTypeAnnotation.class);  
  50.                 //輸出註解屬性  
  51.                 int age = fieldAnno.age();  
  52.                 String[] hobby = fieldAnno.hobby();  
  53.                 String type = fieldAnno.type();  
  54.                 System.out.println(field.getName() + " age = " + age + ", hobby = " + Arrays.asList(hobby).toString() + " type = " + type);  
  55.             }  
  56.         }  
  57.         //解析方法上的註解  
  58.         Method[] methods = clz.getDeclaredMethods();  
  59.         for(Method method : methods){  
  60.             boolean methodHasAnno = method.isAnnotationPresent(MethodAnnotation.class);  
  61.             if(methodHasAnno){  
  62.                 //得到註解  
  63.                 MethodAnnotation methodAnno = method.getAnnotation(MethodAnnotation.class);  
  64.                 //輸出註解屬性  
  65.                 String desc = methodAnno.desc();  
  66.                 System.out.println(method.getName() + " desc = " + desc);  
  67.             }  
  68.         }  
  69.     }  
  70. }  

4、輸出

[plain] view plain copy
  1. com.uno.ray.ReflectAnnotation age = 27, hobby = [smoke] type = class  
  2. maomao age = 27, hobby = [sleep, play] type = ignore  
  3. zhangwenping age = 27, hobby = [phone, buy] type = normal  
  4. method2 desc = method2  
  5. method1 desc = method1  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章