1 /*
2  * Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package java.lang.annotation;
27 
28 /**
29  * The constants of this enumerated class provide a simple classification of the
30  * syntactic locations where annotations may appear in a Java program. These
31  * constants are used in {@link java.lang.annotation.Target Target}
32  * meta-annotations to specify where it is legal to write annotations of a
33  * given type.
34  *
35  * <p>The syntactic locations where annotations may appear are split into
36  * <em>declaration contexts</em>, where annotations apply to declarations, and
37  * <em>type contexts</em>, where annotations apply to types used in
38  * declarations and expressions.
39  *
40  * <p>The constants {@link #ANNOTATION_TYPE}, {@link #CONSTRUCTOR}, {@link
41  * #FIELD}, {@link #LOCAL_VARIABLE}, {@link #METHOD}, {@link #PACKAGE}, {@link
42  * #MODULE}, {@link #PARAMETER}, {@link #TYPE}, and {@link #TYPE_PARAMETER}
43  * correspond to the declaration contexts in JLS 9.6.4.1.
44  *
45  * <p>For example, an annotation whose interface is meta-annotated with
46  * {@code @Target(ElementType.FIELD)} may only be written as a modifier for a
47  * field declaration.
48  *
49  * <p>The constant {@link #TYPE_USE} corresponds to the type contexts in JLS
50  * 4.11, as well as to two declaration contexts: class and interface
51  * declarations (including annotation declarations) and type parameter
52  * declarations.
53  *
54  * <p>For example, an annotation whose interface is meta-annotated with
55  * {@code @Target(ElementType.TYPE_USE)} may be written on the class or
56  * interface of a field (or within the class or interface of the field, if it
57  * is a nested or parameterized class or interface, or array class), and may
58  * also appear as a modifier for, say, a class declaration.
59  *
60  * <p>The {@code TYPE_USE} constant includes class and interface declarations
61  * and type parameter declarations as a convenience for designers of
62  * type checkers which give semantics to annotation interfaces. For example,
63  * if the annotation interface {@code NonNull} is meta-annotated with
64  * {@code @Target(ElementType.TYPE_USE)}, then {@code @NonNull}
65  * {@code class C {...}} could be treated by a type checker as indicating that
66  * all variables of class {@code C} are non-null, while still allowing
67  * variables of other classes to be non-null or not non-null based on whether
68  * {@code @NonNull} appears at the variable's declaration.
69  *
70  * @author  Joshua Bloch
71  * @since 1.5
72  * @jls 9.6.4.1 @Target
73  * @jls 4.1 The Kinds of Types and Values
74  */
75 public enum ElementType {
76     /** Class, interface (including annotation interface), enum, or record
77      * declaration */
78     TYPE,
79 
80     /** Field declaration (includes enum constants) */
81     FIELD,
82 
83     /** Method declaration */
84     METHOD,
85 
86     /** Formal parameter declaration */
87     PARAMETER,
88 
89     /** Constructor declaration */
90     CONSTRUCTOR,
91 
92     /** Local variable declaration */
93     LOCAL_VARIABLE,
94 
95     /** Annotation interface declaration (Formerly known as an annotation type.) */
96     ANNOTATION_TYPE,
97 
98     /** Package declaration */
99     PACKAGE,
100 
101     /**
102      * Type parameter declaration
103      *
104      * @since 1.8
105      */
106     TYPE_PARAMETER,
107 
108     /**
109      * Use of a type
110      *
111      * @since 1.8
112      */
113     TYPE_USE,
114 
115     /**
116      * Module declaration.
117      *
118      * @since 9
119      */
120     MODULE,
121 
122     /**
123      * Record component
124      *
125      * @jls 8.10.3 Record Members
126      * @jls 9.7.4 Where Annotations May Appear
127      *
128      * @since 16
129      */
130     RECORD_COMPONENT;
131 }
132