1 /* 2 * Copyright (c) 2003, 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 java.lang.reflect; 27 28 import java.lang.annotation.Annotation; 29 import java.lang.annotation.AnnotationFormatError; 30 31 /** 32 * Represents an annotated element of the program currently running in this 33 * VM. This interface allows annotations to be read reflectively. All 34 * annotations returned by methods in this interface are immutable and 35 * serializable. The arrays returned by methods of this interface may be modified 36 * by callers without affecting the arrays returned to other callers. 37 * 38 * <p>The {@link #getAnnotationsByType(Class)} and {@link 39 * #getDeclaredAnnotationsByType(Class)} methods support multiple 40 * annotations of the same type on an element. If the argument to 41 * either method is a repeatable annotation type (JLS 9.6), then the 42 * method will "look through" a container annotation (JLS 9.7), if 43 * present, and return any annotations inside the container. Container 44 * annotations may be generated at compile-time to wrap multiple 45 * annotations of the argument type. 46 * 47 * <p>The terms <em>directly present</em>, <em>indirectly present</em>, 48 * <em>present</em>, and <em>associated</em> are used throughout this 49 * interface to describe precisely which annotations are returned by 50 * methods: 51 * 52 * <ul> 53 * 54 * <li> An annotation <i>A</i> is <em>directly present</em> on an 55 * element <i>E</i> if <i>E</i> has a {@code 56 * RuntimeVisibleAnnotations} or {@code 57 * RuntimeVisibleParameterAnnotations} or {@code 58 * RuntimeVisibleTypeAnnotations} attribute, and the attribute 59 * contains <i>A</i>. 60 * 61 * <li>An annotation <i>A</i> is <em>indirectly present</em> on an 62 * element <i>E</i> if <i>E</i> has a {@code RuntimeVisibleAnnotations} or 63 * {@code RuntimeVisibleParameterAnnotations} or {@code RuntimeVisibleTypeAnnotations} 64 * attribute, and <i>A</i> 's type is repeatable, and the attribute contains 65 * exactly one annotation whose value element contains <i>A</i> and whose 66 * type is the containing annotation type of <i>A</i> 's type. 67 * 68 * <li>An annotation <i>A</i> is present on an element <i>E</i> if either: 69 * 70 * <ul> 71 * 72 * <li><i>A</i> is directly present on <i>E</i>; or 73 * 74 * <li>No annotation of <i>A</i> 's type is directly present on 75 * <i>E</i>, and <i>E</i> is a class, and <i>A</i> 's type is 76 * inheritable, and <i>A</i> is present on the superclass of <i>E</i>. 77 * 78 * </ul> 79 * 80 * <li>An annotation <i>A</i> is <em>associated</em> with an element <i>E</i> 81 * if either: 82 * 83 * <ul> 84 * 85 * <li><i>A</i> is directly or indirectly present on <i>E</i>; or 86 * 87 * <li>No annotation of <i>A</i> 's type is directly or indirectly 88 * present on <i>E</i>, and <i>E</i> is a class, and <i>A</i>'s type 89 * is inheritable, and <i>A</i> is associated with the superclass of 90 * <i>E</i>. 91 * 92 * </ul> 93 * 94 * </ul> 95 * 96 * <p>The table below summarizes which kind of annotation presence 97 * different methods in this interface examine. 98 * 99 * <table border> 100 * <caption>Overview of kind of presence detected by different AnnotatedElement methods</caption> 101 * <tr><th colspan=2></th><th colspan=4>Kind of Presence</th> 102 * <tr><th colspan=2>Method</th><th>Directly Present</th><th>Indirectly Present</th><th>Present</th><th>Associated</th> 103 * <tr><td align=right>{@code T}</td><td>{@link #getAnnotation(Class) getAnnotation(Class<T>)} 104 * <td></td><td></td><td>X</td><td></td> 105 * </tr> 106 * <tr><td align=right>{@code Annotation[]}</td><td>{@link #getAnnotations getAnnotations()} 107 * <td></td><td></td><td>X</td><td></td> 108 * </tr> 109 * <tr><td align=right>{@code T[]}</td><td>{@link #getAnnotationsByType(Class) getAnnotationsByType(Class<T>)} 110 * <td></td><td></td><td></td><td>X</td> 111 * </tr> 112 * <tr><td align=right>{@code T}</td><td>{@link #getDeclaredAnnotation(Class) getDeclaredAnnotation(Class<T>)} 113 * <td>X</td><td></td><td></td><td></td> 114 * </tr> 115 * <tr><td align=right>{@code Annotation[]}</td><td>{@link #getDeclaredAnnotations getDeclaredAnnotations()} 116 * <td>X</td><td></td><td></td><td></td> 117 * </tr> 118 * <tr><td align=right>{@code T[]}</td><td>{@link #getDeclaredAnnotationsByType(Class) getDeclaredAnnotationsByType(Class<T>)} 119 * <td>X</td><td>X</td><td></td><td></td> 120 * </tr> 121 * </table> 122 * 123 * <p>For an invocation of {@code get[Declared]AnnotationsByType( Class < 124 * T >)}, the order of annotations which are directly or indirectly 125 * present on an element <i>E</i> is computed as if indirectly present 126 * annotations on <i>E</i> are directly present on <i>E</i> in place 127 * of their container annotation, in the order in which they appear in 128 * the value element of the container annotation. 129 130 * <p>If an annotation returned by a method in this interface contains 131 * (directly or indirectly) a {@link Class}-valued member referring to 132 * a class that is not accessible in this VM, attempting to read the class 133 * by calling the relevant Class-returning method on the returned annotation 134 * will result in a {@link TypeNotPresentException}. 135 * 136 * <p>Similarly, attempting to read an enum-valued member will result in 137 * a {@link EnumConstantNotPresentException} if the enum constant in the 138 * annotation is no longer present in the enum type. 139 * 140 * <p>If an annotation type <i>T</i> is (meta-)annotated with an 141 * {@code @Repeatable} annotation whose value element indicates a type 142 * <i>TC</i>, but <i>TC</i> does not declare a {@code value()} method 143 * with a return type of <i>T</i>{@code []}, then an exception of type 144 * {@link java.lang.annotation.AnnotationFormatError} is thrown. 145 * 146 * <p>Finally, attempting to read a member whose definition has evolved 147 * incompatibly will result in a {@link 148 * java.lang.annotation.AnnotationTypeMismatchException} or an 149 * {@link java.lang.annotation.IncompleteAnnotationException}. 150 * 151 * @see java.lang.EnumConstantNotPresentException 152 * @see java.lang.TypeNotPresentException 153 * @see AnnotationFormatError 154 * @see java.lang.annotation.AnnotationTypeMismatchException 155 * @see java.lang.annotation.IncompleteAnnotationException 156 * @since 1.5 157 * @author Josh Bloch 158 */ 159 public interface AnnotatedElement { 160 /** 161 * Returns true if an annotation for the specified type 162 * is <em>present</em> on this element, else false. This method 163 * is designed primarily for convenient access to marker annotations. 164 * 165 * <p>The truth value returned by this method is equivalent to: 166 * {@code getAnnotation(annotationClass) != null} 167 * 168 * <p>The body of the default method is specified to be the code 169 * above. 170 * 171 * @param annotationClass the Class object corresponding to the 172 * annotation type 173 * @return true if an annotation for the specified annotation 174 * type is present on this element, else false 175 * @throws NullPointerException if the given annotation class is null 176 * @since 1.5 177 */ 178 default boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) { 179 return getAnnotation(annotationClass) != null; 180 } 181 182 /** 183 * Returns this element's annotation for the specified type if 184 * such an annotation is <em>present</em>, else null. 185 * 186 * @param <T> the type of the annotation to query for and return if present 187 * @param annotationClass the Class object corresponding to the 188 * annotation type 189 * @return this element's annotation for the specified annotation type if 190 * present on this element, else null 191 * @throws NullPointerException if the given annotation class is null 192 * @since 1.5 193 */ 194 <T extends Annotation> T getAnnotation(Class<T> annotationClass); 195 196 /** 197 * Returns annotations that are <em>present</em> on this element. 198 * 199 * If there are no annotations <em>present</em> on this element, the return 200 * value is an array of length 0. 201 * 202 * The caller of this method is free to modify the returned array; it will 203 * have no effect on the arrays returned to other callers. 204 * 205 * @return annotations present on this element 206 * @since 1.5 207 */ 208 Annotation[] getAnnotations(); 209 210 /** 211 * Returns annotations that are <em>associated</em> with this element. 212 * 213 * If there are no annotations <em>associated</em> with this element, the return 214 * value is an array of length 0. 215 * 216 * The difference between this method and {@link #getAnnotation(Class)} 217 * is that this method detects if its argument is a <em>repeatable 218 * annotation type</em> (JLS 9.6), and if so, attempts to find one or 219 * more annotations of that type by "looking through" a container 220 * annotation. 221 * 222 * The caller of this method is free to modify the returned array; it will 223 * have no effect on the arrays returned to other callers. 224 * 225 * @param <T> the type of the annotation to query for and return if present 226 * @param annotationClass the Class object corresponding to the 227 * annotation type 228 * @return all this element's annotations for the specified annotation type if 229 * associated with this element, else an array of length zero 230 * @throws NullPointerException if the given annotation class is null 231 * @since 1.8 232 */ 233 <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass); 234 235 /** 236 * Returns this element's annotation for the specified type if 237 * such an annotation is <em>directly present</em>, else null. 238 * 239 * This method ignores inherited annotations. (Returns null if no 240 * annotations are directly present on this element.) 241 * 242 * @param <T> the type of the annotation to query for and return if directly present 243 * @param annotationClass the Class object corresponding to the 244 * annotation type 245 * @return this element's annotation for the specified annotation type if 246 * directly present on this element, else null 247 * @throws NullPointerException if the given annotation class is null 248 * @since 1.8 249 */ 250 <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass); 251 252 /** 253 * Returns this element's annotation(s) for the specified type if 254 * such annotations are either <em>directly present</em> or 255 * <em>indirectly present</em>. This method ignores inherited 256 * annotations. 257 * 258 * If there are no specified annotations directly or indirectly 259 * present on this element, the return value is an array of length 260 * 0. 261 * 262 * The difference between this method and {@link 263 * #getDeclaredAnnotation(Class)} is that this method detects if its 264 * argument is a <em>repeatable annotation type</em> (JLS 9.6), and if so, 265 * attempts to find one or more annotations of that type by "looking 266 * through" a container annotation if one is present. 267 * 268 * The caller of this method is free to modify the returned array; it will 269 * have no effect on the arrays returned to other callers. 270 * 271 * @param <T> the type of the annotation to query for and return 272 * if directly or indirectly present 273 * @param annotationClass the Class object corresponding to the 274 * annotation type 275 * @return all this element's annotations for the specified annotation type if 276 * directly or indirectly present on this element, else an array of length zero 277 * @throws NullPointerException if the given annotation class is null 278 * @since 1.8 279 */ 280 <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass); 281 282 /** 283 * Returns annotations that are <em>directly present</em> on this element. 284 * This method ignores inherited annotations. 285 * 286 * If there are no annotations <em>directly present</em> on this element, 287 * the return value is an array of length 0. 288 * 289 * The caller of this method is free to modify the returned array; it will 290 * have no effect on the arrays returned to other callers. 291 * 292 * @return annotations directly present on this element 293 * @since 1.5 294 */ 295 Annotation[] getDeclaredAnnotations(); 296 }