/** * The constants of this enumerated typeprovide 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. * * <p>The syntactic locations where annotations may appear are split into * <em>declaration contexts</em> , where annotations apply to declarations, and * <em>typecontexts</em> , where annotations apply to types used in * declarations and expressions. * * <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. * * <p>For example, an annotation whose typeis meta-annotated with * {@code @Target(ElementType.FIELD)} may only be written as a modifier for a * field declaration. * * <p>The constant {@link #TYPE_USE} corresponds to the 15typecontexts in JLS * 4.11, as well as to two declaration contexts: typedeclarations (including * annotation typedeclarations) andtypeparameter declarations. * * <p>For example, an annotation whose typeis meta-annotated with * {@code @Target(ElementType.TYPE_USE)} may be written on the typeof a field * (or within the typeof the field, if it is a nested, parameterized, orarray * type), and may also appear as a modifier for, say, a class declaration. * * <p>The {@code TYPE_USE} constant includes typedeclarations andtype * parameter declarations as a convenience for designers oftypecheckers 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 typechecker as indicating that * all variables of class {@code C} are non-null, while still allowing * variables of other classes to be non-null ornot non-null based on whether * {@code @NonNull} appears at the variable's declaration. * * @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,
/** * Annotation retention policy. The constants of this enumerated type * describe the various policies for retaining annotations. They are used * in conjunction withthe {@link Retention} meta-annotation type to specify * how long annotations are to be retained. * * @author Joshua Bloch * @since1.5 */ public enum RetentionPolicy { /** * Annotations are to be discarded bythe compiler. */ SOURCE,
/** * Annotations are to be recorded intheclassfilebythe compiler * but need not be retained bythe VM atruntime. This isthe default * behavior. */ CLASS,
/** * Annotations are to be recorded intheclassfilebythe compiler and * retained bythe VM atruntime, so they may be read reflectively. * * @see java.lang.reflect.AnnotatedElement */ RUNTIME }