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      * <p>The body of default method is specified to be the code
  95      * above.
  96      *
  97      * @param annotationClass the Class object corresponding to the
  98      *        annotation type
  99      * @return true if an annotation for the specified annotation
 100      *     type is present on this element, else false
 101      * @throws NullPointerException if the given annotation class is null
 102      * @since 1.5
 103      */
 104     default boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
 105         return getAnnotation(annotationClass) != null;
 106     }
 107 
 108    /**
 109      * Returns this element's annotation for the specified type if
 110      * such an annotation is present, else null.
 111      *
 112      * @param annotationClass the Class object corresponding to the
 113      *        annotation type
 114      * @return this element's annotation for the specified annotation type if
 115      *     present on this element, else null
 116      * @throws NullPointerException if the given annotation class is null
 117      * @since 1.5
 118      */
 119     <T extends Annotation> T getAnnotation(Class<T> annotationClass);
 120 
 121     /**
 122      * Returns an array of all this element's annotations for the
 123      * specified type if one or more of such annotation is present,
 124      * else an array of length zero.
 125      *
 126      * The caller of this method is free to modify the returned array;
 127      * it will have no effect on the arrays returned to other callers.
 128      *
 129      * @param annotationClass the Class object corresponding to the
 130      *        annotation type
 131      * @return all this element's annotations for the specified annotation type if
 132      *     present on this element, else an array of length zero
 133      * @throws NullPointerException if the given annotation class is null
 134      * @since 1.8
 135      */
 136     <T extends Annotation> T[] getAnnotations(Class<T> annotationClass);
 137 
 138     /**
 139      * Returns annotations that are <em>present</em> on this element.
 140      *
 141      * If there are no annotations <em>present</em> on this element, the return
 142      * value is an array of length 0.
 143      *
 144      * The caller of this method is free to modify the returned array; it will
 145      * have no effect on the arrays returned to other callers.
 146      *
 147      * @return annotations present on this element
 148      * @since 1.5
 149      */
 150     Annotation[] getAnnotations();
 151 
 152     /**
 153      * Returns this element's annotation for the specified type if
 154      * such an annotation is present, else null.
 155      *
 156      * This method ignores inherited annotations. (Returns null if no
 157      * annotations are directly present on this element.)
 158      *
 159      * @param annotationClass the Class object corresponding to the
 160      *        annotation type
 161      * @return this element's annotation for the specified annotation type if
 162      *     present on this element, else null
 163      * @throws NullPointerException if the given annotation class is null
 164      * @since 1.8
 165      */
 166     <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass);
 167 
 168    /**
 169      * Returns an array of all this element's annotations for the
 170      * specified type if one or more of such annotation is directly
 171      * present, else an array of length zero.
 172      *
 173      * This method ignores inherited annotations. (Returns
 174      * an array of length zero if no annotations are directly present
 175      * on this element.)  The caller of this method is free to modify
 176      * the returned array; it will have no effect on the arrays
 177      * returned to other callers.
 178      *
 179      * @param annotationClass the Class object corresponding to the
 180      *        annotation type
 181      * @return all this element's annotations for the specified annotation type if
 182      *     present on this element, else an array of length zero
 183      * @throws NullPointerException if the given annotation class is null
 184      * @since 1.8
 185      */
 186     <T extends Annotation> T[] getDeclaredAnnotations(Class<T> annotationClass);
 187 
 188     /**
 189      * Returns annotations that are <em>directly present</em> on this element.
 190      * This method ignores inherited annotations.
 191      *
 192      * If there are no annotations <em>directly present</em> on this element,
 193      * the return value is an array of length 0.
 194      *
 195      * The caller of this method is free to modify the returned array; it will
 196      * have no effect on the arrays returned to other callers.
 197      *
 198      * @return annotations directly present on this element
 199      * @since 1.5
 200      */
 201     Annotation[] getDeclaredAnnotations();
 202 }