1 /* 2 * Copyright (c) 2003, 2017, 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 type 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 type 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: type declarations (including 51 * annotation type declarations) and type parameter declarations. 52 * 53 * <p>For example, an annotation whose type is meta-annotated with 54 * {@code @Target(ElementType.TYPE_USE)} may be written on the type of a field 55 * (or within the type of the field, if it is a nested, parameterized, or array 56 * type), and may also appear as a modifier for, say, a class declaration. 57 * 58 * <p>The {@code TYPE_USE} constant includes type declarations and type 59 * parameter declarations as a convenience for designers of type checkers which 60 * give semantics to annotation types. For example, if the annotation type 61 * {@code NonNull} is meta-annotated with 62 * {@code @Target(ElementType.TYPE_USE)}, then {@code @NonNull} 63 * {@code class C {...}} could be treated by a type checker as indicating that 64 * all variables of class {@code C} are non-null, while still allowing 65 * variables of other classes to be non-null or not non-null based on whether 66 * {@code @NonNull} appears at the variable's declaration. 67 * 68 * @author Joshua Bloch 69 * @since 1.5 70 * @jls 9.6.4.1 @Target 71 * @jls 4.1 The Kinds of Types and Values 72 */ 73 public enum ElementType { 74 /** Class, interface (including annotation type), or enum declaration */ 75 TYPE, 76 77 /** Field declaration (includes enum constants) */ 78 FIELD, 79 80 /** Method declaration */ 81 METHOD, 82 83 /** Formal parameter declaration */ 84 PARAMETER, 85 86 /** Constructor declaration */ 87 CONSTRUCTOR, 88 89 /** Local variable declaration */ 90 LOCAL_VARIABLE, 91 92 /** Annotation type declaration */ 93 ANNOTATION_TYPE, 94 95 /** Package declaration */ 96 PACKAGE, 97 98 /** 99 * Type parameter declaration 100 * 101 * @since 1.8 102 */ 103 TYPE_PARAMETER, 104 105 /** 106 * Use of a type 107 * 108 * @since 1.8 109 */ 110 TYPE_USE, 111 112 /** 113 * Module declaration. 114 * 115 * @since 9 116 */ 117 MODULE 118 } 119