注解介绍

注解@Retention可以用来修饰注解,是注解的注解,称为元注解

 

Retention注解有一个属性value,是RetentionPolicy类型的,Enum RetentionPolicy是一个枚举类型

/** * Annotation retention policy.

The constants of this enumerated type

* describe the various policies for retaining annotations. They are used

* in conjunction with the {@link Retention} meta-annotation type to specify

* how long annotations are to be retained.

* * @author Joshua Bloch

* @since 1.5

*/

public enum RetentionPolicy {

/**

* Annotations are to be discarded by the compiler.

*/ SOURCE,

/**

* Annotations are to be recorded in the class file by the compiler

* but need not be retained by the VM at run time. This is the default

* behavior.

*/ CLASS,

/**

* Annotations are to be recorded in the class file by the compiler and

* retained by the VM at run time, so they may be read reflectively.

* * @see java.lang.reflect.AnnotatedElement

*/ RUNTIME }

/**

*注释保留策略。此枚举类型的常数

*描述保留注释的各种策略。使用它们

*与{@link Retention}元注释类型一起指定 *注释要保留多长时间。

*

* @作者约书亚·布洛赫

* @since 1.5

* / public enum RetentionPolicy{

/** *保留源文件,编译成Class注释将被编译器丢弃。

* / SOURCE,

/** 注解被保留到class文件,但jvm加载class文件时候被遗弃,这是默认的生命周期;

* / Class,

/** 注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在;

* * @see java.lang.reflect.AnnotatedElement

* / RUNTIME }

.java<.class<内存中的字节码

首先要明确生命周期长度 SOURCE < CLASS < RUNTIME ,所以前者能作用的地方后者一定也能作用

 

 

 

 

 

@Inherited:说明子类可以继承父类中的该注解

 

 

 

@Target:注解的作用目标

public @interface Target {

/**

* Returns an array of the kinds of elements an annotation type

* can be applied to.

* @return an array of the kinds of elements an annotation type

* can be applied to

*/ ElementType[] value();

}

  /** *返回可应用于注释类型的元素类型的数组。

* @返回注释类型中元素类型的数组可以应用到

* /

 

 

 ElementType

/* *版权所有(c)2003,2013,Oracle和/或其附属公司。保留所有权利。 *ORACLE专有/机密。使用须遵守许可条款。

*/ 包java.lang.annotation;

/** *这种枚举类型的常量提供了一个简单的语法位置分类,在那里注释可能出现在Java程序中。这些常数用于

{@link Target java.lang.annotation.Target}元注释,指定在何处编写给定类型的注释是合法的。

* *

注释可能出现的语法位置分为声明上下文,其中注释适用于声明,而类型上下文,其中注释适用于声明和表达式中使用的类型。

* *

常数 {@link#ANNOTATION_TYPE}, {@link#构造函数}, {@link#字段}, {@link#局部变量}, {@link#方法}, {@link#包}, {Link Ly*参数}, {@link#类型}, {Link LyTyType参数}对应于JLS.94.4.1中的声明上下文。

* *

例如,类型是用{@code@Target(ElementType.FIELD)}进行元注释的注释只能作为字段声明的修饰符写入。

* *

常量{@link#TYPE USE}对应于JLS 4.11中的15个类型上下文,以及两个声明上下文:类型声明(包括注释类型声明)和类型参数声明。

* *

例如,类型是用{@code@Target(ElementType.type_USE)}进行元注释的注释可以写在字段的类型上(如果是嵌套的、参数化的或数组类型,则可以写在字段的类型内),也可以显示为类声明的修饰符。

* *

常量{@code TYPE_USE}包括类型声明和类型参数声明,以方便为注释类型提供语义的类型检查程序的设计者。

例如,如果注释类型{@code non null}是用{@code@Target(ElementType.Type_ USE)}进行元注释的,

则类型检查器可以将{@code@NonNull}{@code class C{…}}视为类{@code C}的所有变量都是非空的,

同时仍然允许其他类的变量为非空或非空,

这取决于{@code@non null}是否出现在变量的声明中。

* *@作者乔舒亚·布洛赫 *@从1.5开始 *@jls 9.6.4.1@目标 *@jls 4.1类型和值的种类

*/ public enum ElementType{

/**类、接口(包括注释类型)或枚举声明

*/

TYPE,

/**字段声明(包括枚举常量)

*/

FIELD,

/**方法声明

*/

METHOD,

/**形式参数声明

*/

PARAMETER,,

/**构造函数声明

*/

CONSTRUCTOR,

/**局部变量声明

*/

LOCAL_VARIABLE,

/**注释类型声明

*/

ANNOTATION_TYPE,

/**包声明

*/

PACKAGE,

/** *类型参数声明 **@从1.8开始 */

TYPE_PARAMETER

/**

TYPE_PARAMETER

* *@从1.8开始

*

/ TYPE_USE }

@Target(ElementType.TYPE)——接口、类、枚举、注解

@Target(ElementType.FIELD)——字段、枚举的常量

@Target(ElementType.METHOD)——方法

@Target(ElementType.PARAMETER)——方法参数

@Target(ElementType.CONSTRUCTOR) ——构造函数

@Target(ElementType.LOCAL_VARIABLE)——局部变量

@Target(ElementType.ANNOTATION_TYPE)——注解

@Target(ElementType.PACKAGE)——包

 

 

 

 

 @Document

*表示带有类型的注释将由javadoc记录

*和默认的类似工具。应使用此类型来注释

*声明类型的注释会影响到annotated的使用

*客户提供的元素。如果一个类型声明被注释了

*记录,它的注释成为公共API的一部分

*注释的元素。 *

posted @ 2020-03-19 09:36  余生请多指教ANT  阅读(175)  评论(0编辑  收藏  举报