Java注解学习:注解术语以及自定义注解

一.元注解

  1. @Target:表示该注解可以用于什么地方。可选的ElementType参数包括:
  • TYPE:类,接口(包括注解类型),enum的声明。
  • FIELD:域的声明(包括enum实例)。
  • METHOD:方法的声明。
  • PARAMETER:参数的声明。
  • CONSTRUCTOR:构造器的声明。
  • LOCAL_VARIABLE:局部变量的声明。
  • ANNOTATION_TYPE:注解的声明。
  • PACKAGE:包的声明。
  • @Retention:表示需要在什么级别保存该注解学习。可选的RetentionPolicy参数包括:
    • SOURCE:注解将被编译器丢弃。
    • CLASS:注解在class文件中可用,被VM丢弃。
    • RUNTIME:VM会把注解加载到内存里,运行期间可见,所以可以通过反射读取注解的信息。
  • @Documented:将此注解包含在Javadoc中。
  • @Inherited:允许子类继承父类的注解。并不是说允许子注解类继承父注解类。

     

     

    二.标准注解

         J2SE5里内置了三种

    1. @Override
    2. @Deprecated
    3. @SuppressWarnings

    三.自定义注解

    1. 标记注解:没有元素的注解称为标记注解。
    2. 其它注解:定义了元素的注解。

    四.注解元素

         自定义一个注解类时,类里面的元素只能是如下类型,如果使用其它类型,编译器报错。

    1. 所有基本类型(注意不能使用包装类型)
    2. String
    3. Class
    4. enum
    5. Annotation
    6. 以上类型的数组



自定义注解


二.代码

 

Java代码   收藏代码
  1. package com.jyz.study.jdk.annotation;  
  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.  * 1.演示四种元注解的用法 
  12.  * @Target 
  13.  * @Retention 
  14.  * @Document 
  15.  * @Inherited 
  16.  *  
  17.  * @author JoyoungZhang@gmail.com 
  18.  * 
  19.  */  
  20. @JyzTargetType  
  21. @JyzRetentionRuntime  
  22. @JyzDocument  
  23. @JyzInherited  
  24. public class MetaAnnotation {  
  25.     @JyzTargetField  
  26.     private String info;  
  27.       
  28.     @JyzTargetConstructor  
  29.     public MetaAnnotation(@JyzTargetParamter String info) {  
  30.         this.info = info;  
  31.     }  
  32.       
  33.     @JyzTargetMethod  
  34.     public void test(){  
  35.         @JyzTargetLocalVariable  
  36.         String infoInner = "sa";  
  37.     }  
  38. }  
  39.   
  40. @Target(ElementType.TYPE) @interface JyzTargetType{}                        //接口、类、枚举、注解  
  41. @Target(ElementType.FIELD) @interface JyzTargetField{}                      //字段、枚举的常量  
  42. @Target(ElementType.METHOD) @interface JyzTargetMethod{}                    //方法  
  43. @Target(ElementType.PARAMETER) @interface JyzTargetParamter{}               //方法参数  
  44. @Target(ElementType.CONSTRUCTOR) @interface JyzTargetConstructor{}          //构造函数  
  45. @Target(ElementType.LOCAL_VARIABLE) @interface JyzTargetLocalVariable{}     //局部变量  
  46. @Target(ElementType.ANNOTATION_TYPE) @interface JyzTargetAnnotationType{}   //注解  
  47. @Target(ElementType.PACKAGE) @Retention(RetentionPolicy.RUNTIME) @interface JyzTargetPackage{public String version() default "";}   //包   
  48. @JyzTargetAnnotationType @interface JyzTargetAll{}                                                    
  49.   
  50. @Retention(RetentionPolicy.SOURCE) @interface JyzRetentionSource{}  
  51. @Retention(RetentionPolicy.CLASS) @interface JyzRetentionClass{}  
  52. @Retention(RetentionPolicy.RUNTIME) @interface JyzRetentionRuntime{}  
  53.   
  54. @Documented @interface JyzDocument{}  
  55.   
  56. @Inherited @interface JyzInherited{}  

 

 

 

三.代码解释

  1. Java内置了四种元注解。或许你要问了,这四种元注解又是哪里来的呢?我们来看看这三种元注解,得到的答案是每一种元注解又是建立在四个元注解的基础之上的。有点自己定义自己的意思。
    Java代码   收藏代码
    1. package java.lang.annotation;  
    2.   
    3. @Documented  
    4. @Retention(RetentionPolicy.RUNTIME)  
    5. @Target(ElementType.ANNOTATION_TYPE)  
    6. public @interface Target {  
    7.     ElementType[] value();  
    8. }  
    9.   
    10. @Documented  
    11. @Retention(RetentionPolicy.RUNTIME)  
    12. @Target(ElementType.ANNOTATION_TYPE)  
    13. public @interface Retention {  
    14.     RetentionPolicy value();  
    15. }  
    16.   
    17.   
    18. @Documented  
    19. @Retention(RetentionPolicy.RUNTIME)  
    20. @Target(ElementType.ANNOTATION_TYPE)  
    21. public @interface Documented {  
    22. }  
    23.   
    24. @Documented  
    25. @Retention(RetentionPolicy.RUNTIME)  
    26. @Target(ElementType.ANNOTATION_TYPE)  
    27. public @interface Inherited {  
    28. }  
     
  2. 什么时候使用这四个元注解?仅仅在你需要定义自己的注解类时,如代码里的Jyz*这些类都是自定义的注解类。
  3. 什么时候使用自定义注解类或Java内置的几个标准注解类?当然是在普通Java类(指的是不是注解类的类如MetaAnnotation,有时候我也叫它被注解类)里使用。需要记住的是,普通Java类里并不能直接使用元注解,如果你在MetaAnnotation上面加上@Documented,肯定编译报错。
  4. @Target的八种参数,前六种很好理解,该用什么地方就用什么地方。PACKAGE包的声明会在下文http://zy19982004.iteye.com/blog/1979308单独讲解。于是就是剩下一个ANNOTATION_TYPE了,这个代表了自定义的注解只能用在注解上,看看四个元注解便知,看看
    Java代码   收藏代码
    1. @JyzTargetAnnotationType @interface JyzTargetAll{}        
     也是这个意思。在自定义的注解类里@Target未指定任何参数的话,代表八种都包括,在代码合适的地方我都能使用@JyzTargetAll。
  5. @Inherited的作用也在下文说http://zy19982004.iteye.com/blog/1979520
  6. @Documented:如果Java普通类里使用@JyzDocumented,则普通Java类的Javadoc里保留@JyzDocumented。

 

四.使用总结

  1. 使用四种元注解定义自己的注解类
      1. @Target(ElementType.?)就根据需要定义吧,不太同意定义成@JyzTargetAll。
      2. 除非你能保证你的注解类永远被使用在不需要反射的类上,但这个似乎不太可能,所以你最好@Retention(RetentionPolicy.RUNTIME)。
      3. 至于@Documented也加上吧,对jvm来说完全没什么负担。
      4. 也建议加上@Inherited,尽管你觉得现在是不需要的,指不定两个被注解类就存在继承关系。
  2. 为注解类定义需要的属性(上面的Jyz*这些注解类主要是为了演示元注解的使用,并未加上任何元素),并设置默认值。
  3. 普通Java类里合适的地方使用合适的注解类,并为需要赋值的元素赋值,不赋值将使用默认值。为元素赋值的方式为名-值对,如@JyzTargetPackage(version="1.0") 。如果恰好只需要为一个元素赋值,而这个元素定义为value(),无需使用名值对,只需在括号内给出value元素所需的值即可,如@JyzTargetPackage("1.0") 。






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值