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 30 /** 31 * Represents an annotated element of the program currently running in this 32 * VM. This interface allows annotations to be read reflectively. All 33 * annotations returned by methods in this interface are immutable and 34 * serializable. It is permissible for the caller to modify the 35 * arrays returned by accessors for array-valued enum members; it will 36 * have no affect on the arrays returned to other callers. 37 * 38 * <p>An annotation A is <em>directly present</em> on an element E if the 39 * RuntimeVisibleAnnotations or RuntimeVisibleParameterAnnotations attribute 40 * associated with E either: 41 * <ul> 42 * <li>contains A; or 43 * <li>for invocations of get[Declared]Annotations(Class<T>), 44 * contains A or exactly one annotation C whose type is the containing 45 * annotation type of A's type (JLS 9.6) and whose value element contains A 46 * </ul> 47 * 48 * <p>An annotation A is <em>present</em> on an element E if either: 49 * <ul> 50 * <li>A is <em>directly present</em> on E; or 51 * <li>There are no annotations of A's type which are <em>directly present</em> 52 * on E, and E is a class, and A's type is inheritable (JLS 9.6.3.3), and A is 53 * present on the superclass of E 54 * </ul> 55 * 56 * <p>If an annotation returned by a method in this interface contains 57 * (directly or indirectly) a {@link Class}-valued member referring to 58 * a class that is not accessible in this VM, attempting to read the class 59 * by calling the relevant Class-returning method on the returned annotation 60 * will result in a {@link TypeNotPresentException}. 61 * 62 * <p>Similarly, attempting to read an enum-valued member will result in 63 * a {@link EnumConstantNotPresentException} if the enum constant in the 64 * annotation is no longer present in the enum type. 65 * 66 * <p>Attempting to read annotations of a repeatable annotation type T 67 * that are contained in an annotation whose type is not, in fact, the 68 * containing annotation type of T will result in an 69 * InvalidContainerAnnotationError. 70 * 71 * <p>Finally, attempting to read a member whose definition has evolved 72 * incompatibly will result in a {@link 73 * java.lang.annotation.AnnotationTypeMismatchException} or an 74 * {@link java.lang.annotation.IncompleteAnnotationException}. 75 * 76 * @see java.lang.EnumConstantNotPresentException 77 * @see java.lang.TypeNotPresentException 78 * @see java.lang.annotation.AnnotationFormatError 79 * @see java.lang.annotation.AnnotationTypeMismatchException 80 * @see java.lang.annotation.IncompleteAnnotationException 81 * @see java.lang.annotation.InvalidContainerAnnotationError 82 * @since 1.5 83 * @author Josh Bloch 84 */ 85 public interface AnnotatedElement { 86 /** 87 * Returns true if an annotation for the specified type 88 * is present on this element, else false. This method 89 * is designed primarily for convenient access to marker annotations. 90 * 91 * <p>The truth value returned by this method is equivalent to: 92 * {@code getAnnotation(annotationClass) != null} 93 * 94 * @param annotationClass the Class object corresponding to the 95 * annotation type 96 * @return true if an annotation for the specified annotation 97 * type is present on this element, else false 98 * @throws NullPointerException if the given annotation class is null 99 * @since 1.5 100 */ 101 boolean isAnnotationPresent(Class<? extends Annotation> annotationClass); 102 103 /** 104 * Returns this element's annotation for the specified type if 105 * such an annotation is present, else null. 106 * 107 * @param annotationClass the Class object corresponding to the 108 * annotation type 109 * @return this element's annotation for the specified annotation type if 110 * present on this element, else null 111 * @throws NullPointerException if the given annotation class is null 112 * @since 1.5 113 */ 114 <T extends Annotation> T getAnnotation(Class<T> annotationClass); 115 116 /** 117 * Returns an array of all this element's annotations for the 118 * specified type if one or more of such annotation is present, 119 * else an array of length zero. 120 * 121 * The caller of this method is free to modify the returned array; 122 * it will have no effect on the arrays returned to other callers. 123 * 124 * @param annotationClass the Class object corresponding to the 125 * annotation type 126 * @return all this element's annotations for the specified annotation type if 127 * present on this element, else an array of length zero 128 * @throws NullPointerException if the given annotation class is null 129 * @since 1.8 130 */ 131 <T extends Annotation> T[] getAnnotations(Class<T> annotationClass); 132 133 /** 134 * Returns annotations that are <em>present</em> on this element. 135 * 136 * If there are no annotations <em>present</em> on this element, the return 137 * value is an array of length 0. 138 * 139 * The caller of this method is free to modify the returned array; it will 140 * have no effect on the arrays returned to other callers. 141 * 142 * @return annotations present on this element 143 * @since 1.5 144 */ 145 Annotation[] getAnnotations(); 146 147 /** 148 * Returns this element's annotation for the specified type if 149 * such an annotation is present, else null. 150 * 151 * This method ignores inherited annotations. (Returns null if no 152 * annotations are directly present on this element.) 153 * 154 * @param annotationClass the Class object corresponding to the 155 * annotation type 156 * @return this element's annotation for the specified annotation type if 157 * present on this element, else null 158 * @throws NullPointerException if the given annotation class is null 159 * @since 1.8 160 */ 161 <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass); 162 163 /** 164 * Returns an array of all this element's annotations for the 165 * specified type if one or more of such annotation is directly 166 * present, else an array of length zero. 167 * 168 * This method ignores inherited annotations. (Returns 169 * an array of length zero if no annotations are directly present 170 * on this element.) The caller of this method is free to modify 171 * the returned array; it will have no effect on the arrays 172 * returned to other callers. 173 * 174 * @param annotationClass the Class object corresponding to the 175 * annotation type 176 * @return all this element's annotations for the specified annotation type if 177 * present on this element, else an array of length zero 178 * @throws NullPointerException if the given annotation class is null 179 * @since 1.8 180 */ 181 <T extends Annotation> T[] getDeclaredAnnotations(Class<T> annotationClass); 182 183 /** 184 * Returns annotations that are <em>directly present</em> on this element. 185 * This method ignores inherited annotations. 186 * 187 * If there are no annotations <em>directly present</em> on this element, 188 * the return value is an array of length 0. 189 * 190 * The caller of this method is free to modify the returned array; it will 191 * have no effect on the arrays returned to other callers. 192 * 193 * @return annotations directly present on this element 194 * @since 1.5 195 */ 196 Annotation[] getDeclaredAnnotations(); 197 }