1 /* 2 * Copyright (c) 2013, 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 javax.lang.model; 27 28 import java.lang.annotation.*; 29 import java.util.List; 30 import javax.lang.model.element.*; 31 import javax.lang.model.type.*; 32 33 /** 34 * Represents a construct that can be annotated. 35 * 36 * A construct is either an {@linkplain 37 * javax.lang.model.element.Element element} or a {@linkplain 38 * javax.lang.model.type.TypeMirror type}. Annotations on an element 39 * are on a <em>declaration</em>, whereas annotations on a type are on 40 * a specific <em>use</em> of a type name. 41 * 42 * The terms <em>directly present</em>, <em>present</em>, 43 * <em>indirectly present</em>, and <em>associated </em> are used 44 * throughout this interface to describe precisely which annotations 45 * are returned by the methods defined herein. 46 * 47 * <p>In the definitions below, an annotation <i>A</i> has an 48 * annotation type <i>AT</i>. If <i>AT</i> is a repeatable annotation 49 * type, the type of the containing annotation is <i>ATC</i>. 50 * 51 * <p>Annotation <i>A</i> is <em>directly present</em> on a construct 52 * <i>C</i> if either: 53 * 54 * <ul> 55 * 56 * <li><i>A</i> is explicitly or implicitly declared as applying to 57 * the source code representation of <i>C</i>. 58 * 59 * <p>Typically, if exactly one annotation of type <i>AT</i> appears in 60 * the source code of representation of <i>C</i>, then <i>A</i> is 61 * explicitly declared as applying to <i>C</i>. 62 * 63 * If there are multiple annotations of type <i>AT</i> present on 64 * <i>C</i>, then if <i>AT</i> is repeatable annotation type, an 65 * annotation of type <i>ATC</i> is implicitly declared on <i>C</i>. 66 * 67 * <li> A representation of <i>A</i> appears in the executable output 68 * for <i>C</i>, such as the {@code RuntimeVisibleAnnotations} or 69 * {@code RuntimeVisibleParameterAnnotations} attributes of a class 70 * file. 71 * 72 * </ul> 73 * 74 * <p>An annotation <i>A</i> is <em>present</em> on a 75 * construct <i>C</i> if either: 76 * <ul> 77 * 78 * <li><i>A</i> is directly present on <i>C</i>. 79 * 80 * <li>No annotation of type <i>AT</i> is directly present on 81 * <i>C</i>, and <i>C</i> is a class and <i>AT</i> is inheritable 82 * and <i>A</i> is present on the superclass of <i>C</i>. 83 * 84 * </ul> 85 * 86 * An annotation <i>A</i> is <em>indirectly present</em> on a construct 87 * <i>C</i> if both: 88 * 89 * <ul> 90 * 91 * <li><i>AT</i> is a repeatable annotation type with a containing annotation type <i>ATC</i>. 92 * 93 * <li>An annotation of type <i>ATC</i> is directly present on <i>C</i>. 94 * 95 * </ul> 96 * 97 * An annotation <i>A</i> is <em>associated</em> with a construct 98 * <i>C</i> if either: 99 * 100 * <ul> 101 * 102 * <li> <i>A</i> is directly or indirectly present on <i>C</i>. 103 * 104 * <li> No annotation of type <i>AT</i> is directly or indirectly 105 * present on <i>C</i>, <i>C</i> is a class, and <i>AT</i> is 106 * inheritable, and <i>AT</i> is associated with the superclass of 107 * <i>C</i>. 108 * 109 * </ul> 110 * 111 * @since 1.8 112 * @jls 9.6 Annotation Types 113 * @jls 9.6.3.3 @Inherited 114 */ 115 public interface AnnotatedConstruct { 116 /** 117 * Returns the annotations that are <em>directly present</em> on 118 * this construct. 119 * 120 * @return the annotations <em>directly present</em> on this 121 * construct; an empty list if there are none 122 */ 123 List<? extends AnnotationMirror> getAnnotationMirrors(); 124 125 /** 126 * Returns this construct's annotation of the specified type if 127 * such an annotation is <em>present</em>, else {@code null}. 128 * 129 * <p> The annotation returned by this method could contain an element 130 * whose value is of type {@code Class}. 131 * This value cannot be returned directly: information necessary to 132 * locate and load a class (such as the class loader to use) is 133 * not available, and the class might not be loadable at all. 134 * Attempting to read a {@code Class} object by invoking the relevant 135 * method on the returned annotation 136 * will result in a {@link MirroredTypeException}, 137 * from which the corresponding {@link TypeMirror} may be extracted. 138 * Similarly, attempting to read a {@code Class[]}-valued element 139 * will result in a {@link MirroredTypesException}. 140 * 141 * <blockquote> 142 * <i>Note:</i> This method is unlike others in this and related 143 * interfaces. It operates on runtime reflective information — 144 * representations of annotation types currently loaded into the 145 * VM — rather than on the representations defined by and used 146 * throughout these interfaces. Consequently, calling methods on 147 * the returned annotation object can throw many of the exceptions 148 * that can be thrown when calling methods on an annotation object 149 * returned by core reflection. This method is intended for 150 * callers that are written to operate on a known, fixed set of 151 * annotation types. 152 * </blockquote> 153 * 154 * @param <A> the annotation type 155 * @param annotationType the {@code Class} object corresponding to 156 * the annotation type 157 * @return this construct's annotation for the specified 158 * annotation type if present, else {@code null} 159 * 160 * @see #getAnnotationMirrors() 161 * @see java.lang.reflect.AnnotatedElement#getAnnotation 162 * @see EnumConstantNotPresentException 163 * @see AnnotationTypeMismatchException 164 * @see IncompleteAnnotationException 165 * @see MirroredTypeException 166 * @see MirroredTypesException 167 * @jls 9.6.1 Annotation Type Elements 168 */ 169 <A extends Annotation> A getAnnotation(Class<A> annotationType); 170 171 /** 172 * Returns annotations that are <em>associated</em> with this construct. 173 * 174 * If there are no annotations associated with this construct, the 175 * return value is an array of length 0. 176 * 177 * The difference between this method and {@link #getAnnotation(Class)} 178 * is that this method detects if its argument is a <em>repeatable 179 * annotation type</em>, and if so, attempts to find one or more 180 * annotations of that type by "looking through" a container annotation. 181 * 182 * <p> The annotations returned by this method could contain an element 183 * whose value is of type {@code Class}. 184 * This value cannot be returned directly: information necessary to 185 * locate and load a class (such as the class loader to use) is 186 * not available, and the class might not be loadable at all. 187 * Attempting to read a {@code Class} object by invoking the relevant 188 * method on the returned annotation 189 * will result in a {@link MirroredTypeException}, 190 * from which the corresponding {@link TypeMirror} may be extracted. 191 * Similarly, attempting to read a {@code Class[]}-valued element 192 * will result in a {@link MirroredTypesException}. 193 * 194 * <blockquote> 195 * <i>Note:</i> This method is unlike others in this and related 196 * interfaces. It operates on runtime reflective information — 197 * representations of annotation types currently loaded into the 198 * VM — rather than on the representations defined by and used 199 * throughout these interfaces. Consequently, calling methods on 200 * the returned annotation object can throw many of the exceptions 201 * that can be thrown when calling methods on an annotation object 202 * returned by core reflection. This method is intended for 203 * callers that are written to operate on a known, fixed set of 204 * annotation types. 205 * </blockquote> 206 * 207 * @param <A> the annotation type 208 * @param annotationType the {@code Class} object corresponding to 209 * the annotation type 210 * @return this construct's annotations for the specified annotation 211 * type if present on this construct, else an empty array 212 * 213 * @see #getAnnotationMirrors() 214 * @see #getAnnotation(java.lang.Class) 215 * @see java.lang.reflect.AnnotatedElement#getAnnotationsByType 216 * @see EnumConstantNotPresentException 217 * @see AnnotationTypeMismatchException 218 * @see IncompleteAnnotationException 219 * @see MirroredTypeException 220 * @see MirroredTypesException 221 * @jls 9.6 Annotation Types 222 * @jls 9.6.1 Annotation Type Elements 223 */ 224 <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType); 225 }