`
y806839048
  • 浏览: 1081548 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

java中自定义注解并通过反射获取注解属性值

阅读更多

直接上代码,注释中有说明:

1、定义自定义注解类(类注解和字段注解):

  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、(方法注解)

 

 

  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、定义测试类:(反射类)

 

 

  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、输出

 

  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  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics