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>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 either method
  41  * is a repeatable annotation type (JLS 9.6), then the method will "look
  42  * through" a container annotation (JLS 9.7) which was generated at
  43  * compile-time to wrap multiple annotations of the argument type.
  44  *
  45  * <p>The terms <em>directly present</em> and <em>present</em> are used
  46  * throughout this interface to describe precisely which annotations are
  47  * returned by methods:
  48  *
  49  * <ul>
  50  * <li>An annotation A is <em>directly present</em> on an element E if E is
  51  * associated with a RuntimeVisibleAnnotations or
  52  * RuntimeVisibleParameterAnnotations attribute, and:
  53  *
  54  * <ul>
  55  * <li>for an invocation of {@code get[Declared]Annotation(Class<T>)} or
  56  * {@code get[Declared]Annotations()}, the attribute contains A.
  57  *
  58  * <li>for an invocation of {@code get[Declared]AnnotationsByType(Class<T>)}, the
  59  * attribute either contains A or, if the type of A is repeatable, contains
  60  * exactly one annotation whose value element contains A and whose type is the
  61  * containing annotation type of A's type (JLS 9.6).
  62  * </ul>
  63  *
  64  * <p>
  65  * <li>An annotation A is <em>present</em> on an element E if either:
  66  *
  67  * <ul>
  68  * <li>A is <em>directly present</em> on E; or
  69  *
  70  * <li>A is not <em>directly present</em> on E, and E is a class, and A's type
  71  * is inheritable (JLS 9.6.3.3), and A is <em>present</em> on the superclass of
  72  * E.
  73  * </ul>
  74  *
  75  * </ul>
  76  *
  77  * <p>If an annotation returned by a method in this interface contains
  78  * (directly or indirectly) a {@link Class}-valued member referring to
  79  * a class that is not accessible in this VM, attempting to read the class
  80  * by calling the relevant Class-returning method on the returned annotation
  81  * will result in a {@link TypeNotPresentException}.
  82  *
  83  * <p>Similarly, attempting to read an enum-valued member will result in
  84  * a {@link EnumConstantNotPresentException} if the enum constant in the
  85  * annotation is no longer present in the enum type.
  86  *
  87  * <p>Attempting to read annotations of a repeatable annotation type T
  88  * that are contained in an annotation whose type is not, in fact, the
  89  * containing annotation type of T will result in an
  90  * InvalidContainerAnnotationError.
  91  *
  92  * <p>Finally, attempting to read a member whose definition has evolved
  93  * incompatibly will result in a {@link
  94  * java.lang.annotation.AnnotationTypeMismatchException} or an
  95  * {@link java.lang.annotation.IncompleteAnnotationException}.
  96  *
  97  * @see java.lang.EnumConstantNotPresentException
  98  * @see java.lang.TypeNotPresentException
  99  * @see java.lang.annotation.AnnotationFormatError
 100  * @see java.lang.annotation.AnnotationTypeMismatchException
 101  * @see java.lang.annotation.IncompleteAnnotationException
 102  * @see java.lang.annotation.InvalidContainerAnnotationError
 103  * @since 1.5
 104  * @author Josh Bloch
 105  */
 106 public interface AnnotatedElement {
 107     /**
 108      * Returns true if an annotation for the specified type
 109      * is present on this element, else false.  This method
 110      * is designed primarily for convenient access to marker annotations.
 111      *
 112      * <p>The truth value returned by this method is equivalent to:
 113      * {@code getAnnotation(annotationClass) != null}
 114      *
 115      * <p>The body of the default method is specified to be the code
 116      * above.
 117      *
 118      * @param annotationClass the Class object corresponding to the
 119      *        annotation type
 120      * @return true if an annotation for the specified annotation
 121      *     type is present on this element, else false
 122      * @throws NullPointerException if the given annotation class is null
 123      * @since 1.5
 124      */
 125     default boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
 126         return getAnnotation(annotationClass) != null;
 127     }
 128 
 129    /**
 130      * Returns this element's annotation for the specified type if
 131      * such an annotation is present, else null.
 132      *
 133      * @param annotationClass the Class object corresponding to the
 134      *        annotation type
 135      * @return this element's annotation for the specified annotation type if
 136      *     present on this element, else null
 137      * @throws NullPointerException if the given annotation class is null
 138      * @since 1.5
 139      */
 140     <T extends Annotation> T getAnnotation(Class<T> annotationClass);
 141 
 142     /**
 143      * Returns annotations that are <em>present</em> on this element.
 144      *
 145      * If there are no annotations <em>present</em> on this element, the return
 146      * value is an array of length 0.
 147      *
 148      * The difference between this method and {@link #getAnnotation(Class)}
 149      * is that this method detects if its argument is a <em>repeatable
 150      * annotation type</em> (JLS 9.6), and if so, attempts to find one or
 151      * more annotations of that type by "looking through" a container
 152      * annotation.
 153      *
 154      * The caller of this method is free to modify the returned array; it will
 155      * have no effect on the arrays returned to other callers.
 156      *
 157      * @param annotationClass the Class object corresponding to the
 158      *        annotation type
 159      * @return all this element's annotations for the specified annotation type if
 160      *     present on this element, else an array of length zero
 161      * @throws NullPointerException if the given annotation class is null
 162      * @since 1.8
 163      */
 164     <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass);
 165 
 166     /**
 167      * Returns annotations that are <em>present</em> on this element.
 168      *
 169      * If there are no annotations <em>present</em> on this element, the return
 170      * value is an array of length 0.
 171      *
 172      * The caller of this method is free to modify the returned array; it will
 173      * have no effect on the arrays returned to other callers.
 174      *
 175      * @return annotations present on this element
 176      * @since 1.5
 177      */
 178     Annotation[] getAnnotations();
 179 
 180     /**
 181      * Returns this element's annotation for the specified type if
 182      * such an annotation is present, else null.
 183      *
 184      * This method ignores inherited annotations. (Returns null if no
 185      * annotations are directly present on this element.)
 186      *
 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.8
 193      */
 194     <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass);
 195 
 196     /**
 197      * Returns annotations that are <em>directly present</em> on this element.
 198      * This method ignores inherited annotations.
 199      *
 200      * If there are no annotations <em>directly present</em> on this element,
 201      * the return value is an array of length 0.
 202      *
 203      * The difference between this method and {@link
 204      * #getDeclaredAnnotation(Class)} is that this method detects if its
 205      * argument is a <em>repeatable annotation type</em> (JLS 9.6), and if so,
 206      * attempts to find one or more annotations of that type by "looking
 207      * through" a container annotation.
 208      *
 209      * The caller of this method is free to modify the returned array; it will
 210      * have no effect on the arrays returned to other callers.
 211      *
 212      * @param annotationClass the Class object corresponding to the
 213      *        annotation type
 214      * @return all this element's annotations for the specified annotation type if
 215      *     present on this element, else an array of length zero
 216      * @throws NullPointerException if the given annotation class is null
 217      * @since 1.8
 218      */
 219     <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass);
 220 
 221     /**
 222      * Returns annotations that are <em>directly present</em> on this element.
 223      * This method ignores inherited annotations.
 224      *
 225      * If there are no annotations <em>directly present</em> on this element,
 226      * the return value is an array of length 0.
 227      *
 228      * The caller of this method is free to modify the returned array; it will
 229      * have no effect on the arrays returned to other callers.
 230      *
 231      * @return annotations directly present on this element
 232      * @since 1.5
 233      */
 234     Annotation[] getDeclaredAnnotations();
 235 }