jdk 1.8
- 注解API
package java.lang.annotation;
/**
* Indicates the contexts in which an annotation type is applicable. The
* declaration contexts and type contexts in which an annotation type may be
* applicable are specified in JLS 9.6.4.1, and denoted in source code by enum
* constants of {@link ElementType java.lang.annotation.ElementType}.
* <trans>@Target用来指明注解类型适用的上下文。注解类型适用的声明上下文、类型上
* 下文,在JLS 9.6.4.1 做了相关声明,并在源码中采用枚举常量来限定
* java.lang.annotation.ElementType 表示。
*
* <p>If an {@code @Target} meta-annotation is not present on an annotation type
* {@code T} , then an annotation of type {@code T} may be written as a
* modifier for any declaration except a type parameter declaration.
* <trans> 如果一个注解类型T上不存在元注解@Target,那么该注解类型可以用在除类型参
* 数声明外的任何修饰符声明
*
* <p>If an {@code @Target} meta-annotation is present, the compiler will enforce
* the usage restrictions indicated by {@code ElementType}
* enum constants, in line with JLS 9.7.4.
* <trans> 如果存在元注解@Target,则编译器将强制限制此注解类型仅用于枚举常量声明的
* 上下文,以符合JLS 9.7.4 的要求
*
* <p>For example, this {@code @Target} meta-annotation indicates that the
* declared type is itself a meta-annotation type. It can only be used on
* annotation type declarations:
* <trans>例如,这里的@Target元注解表示声明的类型本身就是元注解类型。它只能用
* 于注解类型声明:
* <pre>
* @Target(ElementType.ANNOTATION_TYPE)
* public @interface MetaAnnotationType {
* ...
* }
* </pre>
*
* <p>This {@code @Target} meta-annotation indicates that the declared type is
* intended solely for use as a member type in complex annotation type
* declarations. It cannot be used to annotate anything directly:
* <trans>此除@Target元注解表示声明的类型仅用于复杂的注解类型声明中的成员类型。
* 它不能用于直接注解任何内容:
* <pre>
* @Target({})
* public @interface MemberType {
* ...
* }
* </pre>
*
* <p>It is a compile-time error for a single {@code ElementType} constant to
* appear more than once in an {@code @Target} annotation. For example, the
* following {@code @Target} meta-annotation is illegal:
* <trans>同一ElementType常量声明多次是一个编译时错误,例如下面的注解就是非法的
* <pre>
* @Target({ElementType.FIELD, ElementType.METHOD, ElementType.FIELD})
* public @interface Bogus {
* ...
* }
* </pre>
*
* @since 1.5
* @jls 9.6.4.1 @Target
* @jls 9.7.4 Where Annotations May Appear
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
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
* <trans>注解可以使用的各类元素类型组成的数组
*/
ElementType[] value();
}
- 枚举适用上下文(位置)
package java.lang.annotation;
/**
* The constants of this enumerated type provide a simple classification of the
* syntactic locations where annotations may appear in a Java program. These
* constants are used in {@link Target java.lang.annotation.Target}
* meta-annotations to specify where it is legal to write annotations of a
* given type.
* <trans>此枚举类的常量提供了对java程序中可能出现注解的句法位置的简单分类;
* 这些常量用于@Target元注解中,以指定何处编写给定类型的注解是合法的。
*
* <p>The syntactic locations where annotations may appear are split into
* <em>declaration contexts</em> , where annotations apply to declarations, and
* <em>type contexts</em> , where annotations apply to types used in
* declarations and expressions.
* <trans>可能出现注解的句法位置分为“声明上下文”(注解适用于声明)和“类型上下文”
* (注解适用于声明和表达式中使用的类型)。
*
* <p>The constants {@link #ANNOTATION_TYPE} , {@link #CONSTRUCTOR} , {@link
* #FIELD} , {@link #LOCAL_VARIABLE} , {@link #METHOD} , {@link #PACKAGE} ,
* {@link #PARAMETER} , {@link #TYPE} , and {@link #TYPE_PARAMETER} correspond
* to the declaration contexts in JLS 9.6.4.1.
* <trans>上述8种对用于JLS 9.6.4.1 中是声明上下文
* 包含:注解、构造器、字段、局部变量、方法、包、参数、类型、类型参数这些类型
*
* <p>For example, an annotation whose type is meta-annotated with
* {@code @Target(ElementType.FIELD)} may only be written as a modifier for a
* field declaration.
* <trans>例如:一个用@Target(ElementType.FIELD)注解的注解只能写作字段声明的修饰符
*
* <p>The constant {@link #TYPE_USE} corresponds to the 15 type contexts in JLS
* 4.11, as well as to two declaration contexts: type declarations (including
* annotation type declarations) and type parameter declarations.
* <trans>常量 #TYPE_USE 对应了JLS 4.11 中的15个类型上下文,同时也包括两个声明
* 上下文:类型声明(包括注解类型声明)和类型参数声明。
*
* <p>For example, an annotation whose type is meta-annotated with
* {@code @Target(ElementType.TYPE_USE)} may be written on the type of a field
* (or within the type of the field, if it is a nested, parameterized, or array
* type), and may also appear as a modifier for, say, a class declaration.
* <trans>例如,可以在字段类型上使用@Target(ElementType.TYPE_USE)进行注释(或在
* 字段内,如果它是嵌套的、参数化的或数组类型的),并且也可能作为类声明的修饰符出现
*
* <p>The {@code TYPE_USE} constant includes type declarations and type
* parameter declarations as a convenience for designers of type checkers which
* give semantics to annotation types. For example, if the annotation type
* {@code NonNull} is meta-annotated with
* {@code @Target(ElementType.TYPE_USE)}, then {@code @NonNull}
* {@code class C {...}} could be treated by a type checker as indicating that
* all variables of class {@code C} are non-null, while still allowing
* variables of other classes to be non-null or not non-null based on whether
* {@code @NonNull} appears at the variable's declaration.
* <trans> #TYPE_USE常量包含类型声明和类型参数声明这两个声明上下文,以方便类型
* 检查器的设计人员,这些检查器为注解类型提供了语义;例如,如果在@NonNull注解上
* 用@Target(ElementType.TYPE_USE)注解了,那么把将类class C {...}用@NonNull注解
* 就可以当做是一个类型检查器,表示所C中所有的变量都不为空,同时也允许其他类的
* 变量为无null或非无null,这取决于@NonNull是否出现在变量的声明上。
*
* @author Joshua Bloch
* @since 1.5
* @jls 9.6.4.1 @Target
* @jls 4.1 The Kinds of Types and Values
*/
public enum ElementType {
/** Class, interface (including annotation type), or enum declaration */
/** 类,接口(包括注解),枚举声明 */
TYPE,
/** Field declaration (includes enum constants) */
/** 字段声明(包括枚举常量) */
FIELD,
/** Method declaration */
METHOD,
/** Formal parameter declaration */
/** 形式参数声明 */
PARAMETER,
/** Constructor declaration */
CONSTRUCTOR,
/** Local variable declaration */
LOCAL_VARIABLE,
/** Annotation type declaration */
ANNOTATION_TYPE,
/** Package declaration */
PACKAGE,
/**
* Type parameter declaration
* 类型参数声明
* @since 1.8
*/
TYPE_PARAMETER,
/**
* Use of a type
* 使用类型
* @since 1.8
*/
TYPE_USE
}