Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java Annotation学习 #3

Open
soarpenguin opened this issue Jul 2, 2017 · 0 comments
Open

Java Annotation学习 #3

soarpenguin opened this issue Jul 2, 2017 · 0 comments

Comments

@soarpenguin
Copy link
Contributor

soarpenguin commented Jul 2, 2017

1. Annotation架构

先看看Annotation的架构图:


从中,我们可以看出:

(01) 1个Annotation 和 1个RetentionPolicy关联。
          可以理解为:每1个Annotation对象,都会有唯一的RetentionPolicy属性。
(02) 1个Annotation 和 1-n个ElementType关联。
         可以理解为:对于每1个Annotation对象,可以有若干个ElementType属性。
(03) Annotation 有许多实现类,包括:Deprecated, Documented, Inherited, Override等等。
         Annotation 的每一个实现类,都“和1个RetentionPolicy关联”并且“和1-n个ElementType关联”。

 下面,我先介绍框架图的左半边(如下图),即Annotation, RetentionPolicy, ElementType;然后在就Annotation的实现类进行举例说明。

2. Annotation组成部分

1 annotation组成成分
 java annotation 的组成中,有3个非常重要的主干类。它们分别是:
   (01) Annotation.java
   (02) ElementType.java
   (03) RetentionPolicy.java

 说明:
  (01) Annotation 就是个接口。
       “每1个Annotation” 都与 “1个RetentionPolicy”关联,并且与 “1~n个ElementType”关联。可以通俗的理解为:每1个Annotation对象,都会有唯一的RetentionPolicy属性;至于ElementType属性,则有1~n个。

  (02) ElementType 是Enum枚举类型,它用来指定Annotation的类型。
        “每1个Annotation” 都与 “1~n个ElementType”关联。当Annotation与某个ElementType关联时,就意味着:Annotation有了某种用途。
        例如,若一个Annotation对象是METHOD类型,则该Annotation只能用来修饰方法。

  (03) RetentionPolicy 是Enum枚举类型,它用来指定Annotation的策略。通俗点说,就是不同RetentionPolicy类型的Annotation的作用域不同。
        “每1个Annotation” 都与 “1个RetentionPolicy”关联。

         a) 若Annotation的类型为 SOURCE,则意味着:Annotation仅存在于编译器处理期间,编译器处理完之后,该Annotation就没用了。
             例如,“ @Override ”标志就是一个Annotation。当它修饰一个方法的时候,就意味着该方法覆盖父类的方法;并且在编译期间会进行语法检查!编译器处理完后,“@Override”就没有任何作用了。

         b) 若Annotation的类型为 CLASS,则意味着:编译器将Annotation存储于类对应的.class文件中,它是Annotation的默认行为。

         c) 若Annotation的类型为 RUNTIME,则意味着:编译器将Annotation存储于class文件中,并且可由JVM读入。

       这时,只需要记住“每1个Annotation” 都与 “1个RetentionPolicy”关联,并且与 “1~n个ElementType”关联。学完后面的内容之后,再回头看这些内容,会更容易理解。

3.java自带的Annotation

  理解了上面的3个类的作用之后,我们接下来可以讲解Annotation实现类的语法定义了。

  1. Annotation通用定义

  @Documented
  @Target(ElementType.TYPE)
  @Retention(RetentionPolicy.RUNTIME)
  public @interface MyAnnotation1 {
  }

  说明:
  上面的作用是定义一个Annotation,它的名字是MyAnnotation1。定义了MyAnnotation1之后,我们可以在代码中通过“@MyAnnotation1”来使用它。
  其它的,@Documented, @Target, @Retention, @interface都是来修饰MyAnnotation1的。下面分别说说它们的含义:

  (01) @interface
         使用@interface定义注解时,意味着它实现了java.lang.annotation.Annotation接口,即该注解就是一个Annotation。
         定义Annotation时,@interface是必须的。
         注意:它和我们通常的implemented实现接口的方法不同。Annotation接口的实现细节都由编译器完成。通过@interface定义注解后,该注解不能继承其他的注解或接口。

  (02) @Documented
         类和方法的Annotation在缺省情况下是不出现在javadoc中的。如果使用@Documented修饰该Annotation,则表示它可以出现在javadoc中。
         定义Annotation时,@Documented可有可无;若没有定义,则Annotation不会出现在javadoc中。

  (03) @Target(ElementType.TYPE)
        前面我们说过,ElementType 是Annotation的类型属性。而@Target的作用,就是来指定Annotation的类型属性。
        @Target(ElementType.TYPE) 的意思就是指定该Annotation的类型是ElementType.TYPE。这就意味着,MyAnnotation1是来修饰“类、接口(包括注释类型)或枚举声明”的注解。
        定义Annotation时,@Target可有可无。若有@Target,则该Annotation只能用于它所指定的地方;若没有@Target,则该Annotation可以用于任何地方。

  (04) @Retention(RetentionPolicy.RUNTIME)
        前面我们说过,RetentionPolicy 是Annotation的策略属性,而@Retention的作用,就是指定Annotation的策略属性。
        @Retention(RetentionPolicy.RUNTIME) 的意思就是指定该Annotation的策略是      RetentionPolicy.RUNTIME。这就意味着,编译器会将该Annotation信息保留在.class文件中,并且能被虚拟机读取。
        定义Annotation时,@Retention可有可无。若没有@Retention,则默认是RetentionPolicy.CLASS。

  2. java自带的Annotation
  通过上面的示例,我们能理解:@interface用来声明Annotation,@Documented用来表示该Annotation是否会出现在javadoc中, @Target用来指定Annotation的类型,@Retention用来指定Annotation的策略。

  理解这一点之后,我们就很容易理解java中自带的Annotation的实现类,即Annotation架构图的右半边。如下图:

  java 常用的Annotation:
        @Deprecated  -- @Deprecated 所标注内容,不再被建议使用。
        @Override    -- @Override 只能标注方法,表示该方法覆盖父类中的方法。
        @Documented  -- @Documented 所标注内容,可以出现在javadoc中。
        @Inherited   -- @Inherited只能被用来标注“Annotation类型”,它所标注的Annotation具有继承性。
        @Retention   -- @Retention只能被用来标注“Annotation类型”,而且它被用来指定Annotation的RetentionPolicy属性。
        @Target      -- @Target只能被用来标注“Annotation类型”,而且它被用来指定Annotation的ElementType属性。
        @SuppressWarnings -- @SuppressWarnings 所标注内容产生的警告,编译器会对这些警告保持静默。

  由于“@Deprecated和@Override”类似,“@Documented, @Inherited, @Retention, @Target”类似;下面,我们只对@Deprecated, @Inherited, @SuppressWarnings 这3个Annotation进行说明。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant