/*
 * Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

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 Target meta-annotations to specify where it is legal to write annotations of a given type.

The syntactic locations where annotations may appear are split into declaration contexts, where annotations apply to declarations, and type contexts, where annotations apply to types used in declarations and expressions.

The constants ANNOTATION_TYPE, CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, MODULE, PARAMETER, TYPE, and TYPE_PARAMETER correspond to the declaration contexts in JLS 9.6.4.1.

For example, an annotation whose type is meta-annotated with @Target(ElementType.FIELD) may only be written as a modifier for a field declaration.

The constant TYPE_USE corresponds to the type contexts in JLS 4.11, as well as to two declaration contexts: type declarations (including annotation type declarations) and type parameter declarations.

For example, an annotation whose type is meta-annotated with @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.

The 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 NonNull is meta-annotated with @Target(ElementType.TYPE_USE), then @NonNull class C {...} could be treated by a type checker as indicating that all variables of class C are non-null, while still allowing variables of other classes to be non-null or not non-null based on whether @NonNull appears at the variable's declaration.

Author: Joshua Bloch
Since:1.5
@jls9.6.4.1 @Target
@jls4.1 The Kinds of Types and Values
/** * 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 java.lang.annotation.Target 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>type contexts</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 * #MODULE}, {@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 type is 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 type contexts in JLS * 4.11, as well as to two declaration contexts: type declarations (including * annotation type declarations) and type parameter declarations. * * <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. * * <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. * * @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), enum, or record declaration
/** Class, interface (including annotation type), enum, or record * declaration */
TYPE,
Field declaration (includes enum constants)
/** Field declaration (includes enum constants) */
FIELD,
Method declaration
/** Method declaration */
METHOD,
Formal parameter declaration
/** Formal parameter declaration */
PARAMETER,
Constructor declaration
/** Constructor declaration */
CONSTRUCTOR,
Local variable declaration
/** Local variable declaration */
LOCAL_VARIABLE,
Annotation type declaration
/** Annotation type declaration */
ANNOTATION_TYPE,
Package declaration
/** Package declaration */
PACKAGE,
Type parameter declaration
Since:1.8
/** * Type parameter declaration * * @since 1.8 */
TYPE_PARAMETER,
Use of a type
Since:1.8
/** * Use of a type * * @since 1.8 */
TYPE_USE,
Module declaration.
Since:9
/** * Module declaration. * * @since 9 */
MODULE,
{@preview Associated with records, a preview feature of the Java language. This constant is associated with records, a preview feature of the Java language. Programs can only use this constant when preview features are enabled. Preview features may be removed in a future release, or upgraded to permanent features of the Java language.} Record component
@jls8.10.3 Record Members
@jls9.7.4 Where Annotations May Appear
Since:14
/** * {@preview Associated with records, a preview feature of the Java language. * * This constant is associated with <i>records</i>, a preview * feature of the Java language. Programs can only use this * constant when preview features are enabled. Preview features * may be removed in a future release, or upgraded to permanent * features of the Java language.} * * Record component * * @jls 8.10.3 Record Members * @jls 9.7.4 Where Annotations May Appear * * @since 14 */
@jdk.internal.PreviewFeature(feature=jdk.internal.PreviewFeature.Feature.RECORDS, essentialAPI=true) RECORD_COMPONENT; }