src/share/classes/javax/lang/model/AnnotatedConstruct.java

Print this page




  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> and <em>present</em> are used

  43  * throughout this interface to describe precisely which annotations
  44  * are returned by methods:
  45  *
  46  * <p>An annotation <i>A</i> is <em>directly present</em> on a
  47  * construct <i>E</i> if <i>E</i> is annotated, and:







































  48  *
  49  * <ul>
  50  *
  51  * <li> for an invocation of {@code getAnnotation(Class<T>)} or
  52  * {@code getAnnotationMirrors()}, <i>E</i>'s annotations contain <i>A</i>.
  53  *
  54  * <li> for an invocation of {@code getAnnotationsByType(Class<T>)},
  55  * <i>E</i>'s annotations either contain <i>A</i> or, if the type of
  56  * <i>A</i> is repeatable, contain exactly one annotation whose value
  57  * element contains <i>A</i> and whose type is the containing
  58  * annotation type of <i>A</i>'s type.
  59  *
  60  * </ul>
  61  *
  62  * <p>An annotation A is <em>present</em> on a construct E if either:

  63  *
  64  * <ul>
  65  *  <li> <i>A</i> is <em>directly present</em> on <i>E</i>; or
  66  *
  67  *  <li> <i>A</i> is not <em>directly present</em> on <i>E</i>, and
  68  *  <i>E</i> is an element representing a class, and <i>A</i>'s type
  69  *  is inheritable, and <i>A</i> is <em>present</em> on the element
  70  *  representing the superclass of <i>E</i>.


  71  *
  72  * </ul>
  73  *
  74  * @since 1.8
  75  * @jls 9.6 Annotation Types
  76  * @jls 9.6.3.3 @Inherited
  77  */
  78 public interface AnnotatedConstruct {
  79     /**
  80      * Returns the annotations that are <em>directly present</em> on
  81      * this construct.
  82      *
  83      * @return the annotations <em>directly present</em> on this
  84      * construct; an empty list if there are none
  85      */
  86     List<? extends AnnotationMirror> getAnnotationMirrors();
  87 
  88     /**
  89      * Returns this construct's annotation of the
  90      * specified type if such an annotation is <em>present</em>, else {@code
  91      * null}.
  92      *
  93      * <p> The annotation returned by this method could contain an element
  94      * whose value is of type {@code Class}.
  95      * This value cannot be returned directly:  information necessary to
  96      * locate and load a class (such as the class loader to use) is
  97      * not available, and the class might not be loadable at all.
  98      * Attempting to read a {@code Class} object by invoking the relevant
  99      * method on the returned annotation
 100      * will result in a {@link MirroredTypeException},
 101      * from which the corresponding {@link TypeMirror} may be extracted.
 102      * Similarly, attempting to read a {@code Class[]}-valued element
 103      * will result in a {@link MirroredTypesException}.
 104      *
 105      * <blockquote>
 106      * <i>Note:</i> This method is unlike others in this and related
 107      * interfaces.  It operates on runtime reflective information &mdash;
 108      * representations of annotation types currently loaded into the
 109      * VM &mdash; rather than on the representations defined by and used
 110      * throughout these interfaces.  Consequently, calling methods on
 111      * the returned annotation object can throw many of the exceptions
 112      * that can be thrown when calling methods on an annotation object
 113      * returned by core reflection.  This method is intended for
 114      * callers that are written to operate on a known, fixed set of
 115      * annotation types.
 116      * </blockquote>
 117      *
 118      * @param <A>  the annotation type
 119      * @param annotationType  the {@code Class} object corresponding to
 120      *          the annotation type
 121      * @return this element's or type use's annotation for the
 122      * specified annotation type if present on this element, else
 123      * {@code null}
 124      *
 125      * @see #getAnnotationMirrors()
 126      * @see java.lang.reflect.AnnotatedElement#getAnnotation
 127      * @see EnumConstantNotPresentException
 128      * @see AnnotationTypeMismatchException
 129      * @see IncompleteAnnotationException
 130      * @see MirroredTypeException
 131      * @see MirroredTypesException
 132      * @jls 9.6.1 Annotation Type Elements
 133      */
 134     <A extends Annotation> A getAnnotation(Class<A> annotationType);
 135 
 136     /**
 137      * Returns annotations that are <em>present</em> on this construct.



 138      *
 139      * If there are no annotations <em>present</em> on this construct,
 140      * the return value is an array of length 0.



 141      *
 142      * The difference between this method and {@link #getAnnotation(Class)}
 143      * is that this method detects if its argument is a <em>repeatable
 144      * annotation type</em>, and if so, attempts to find one or more
 145      * annotations of that type by "looking through" a container annotation.
 146      *
 147      * <p> The annotations returned by this method could contain an element
 148      * whose value is of type {@code Class}.
 149      * This value cannot be returned directly:  information necessary to
 150      * locate and load a class (such as the class loader to use) is
 151      * not available, and the class might not be loadable at all.
 152      * Attempting to read a {@code Class} object by invoking the relevant
 153      * method on the returned annotation
 154      * will result in a {@link MirroredTypeException},
 155      * from which the corresponding {@link TypeMirror} may be extracted.
 156      * Similarly, attempting to read a {@code Class[]}-valued element
 157      * will result in a {@link MirroredTypesException}.
 158      *
 159      * <blockquote>
 160      * <i>Note:</i> This method is unlike others in this and related
 161      * interfaces.  It operates on runtime reflective information &mdash;
 162      * representations of annotation types currently loaded into the
 163      * VM &mdash; rather than on the representations defined by and used
 164      * throughout these interfaces.  Consequently, calling methods on
 165      * the returned annotation object can throw many of the exceptions
 166      * that can be thrown when calling methods on an annotation object
 167      * returned by core reflection.  This method is intended for
 168      * callers that are written to operate on a known, fixed set of
 169      * annotation types.
 170      * </blockquote>
 171      *
 172      * @param <A>  the annotation type
 173      * @param annotationType  the {@code Class} object corresponding to
 174      *          the annotation type
 175      * @return this element's annotations for the specified annotation
 176      *         type if present on this element, else an empty array
 177      *
 178      * @see #getAnnotationMirrors()
 179      * @see #getAnnotation(java.lang.Class)
 180      * @see java.lang.reflect.AnnotatedElement#getAnnotationsByType
 181      * @see EnumConstantNotPresentException
 182      * @see AnnotationTypeMismatchException
 183      * @see IncompleteAnnotationException
 184      * @see MirroredTypeException
 185      * @see MirroredTypesException
 186      * @jls 9.6 Annotation Types
 187      * @jls 9.6.1 Annotation Type Elements
 188      */
 189     <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType);
 190 }


  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
  92  * annotation type <i>ATC</i>.
  93  *
  94  * <li>An annotation of type <i>ATC</i> is directly present on
  95  * <i>C</i> and <i>A</i> is an annotation included in the result of
  96  * calling the {@code value} method of the directly present annotation
  97  * of type <i>ATC</i>.

  98  *
  99  * </ul>
 100  *
 101  * An annotation <i>A</i> is <em>associated</em> with a construct
 102  * <i>C</i> if either:
 103  *
 104  * <ul> 

 105  *
 106  * <li> <i>A</i> is directly or indirectly present on <i>C</i>. 
 107  *
 108  * <li> No annotation of type <i>AT</i> is directly or indirectly
 109  * present on <i>C</i>, and <i>C</i> is a class, and <i>AT</i> is
 110  * inheritable, and <i>A</i> is associated with the superclass of
 111  * <i>C</i>.
 112  *
 113  * </ul>
 114  *
 115  * @since 1.8
 116  * @jls 9.6 Annotation Types
 117  * @jls 9.6.3.3 @Inherited
 118  */
 119 public interface AnnotatedConstruct {
 120     /**
 121      * Returns the annotations that are <em>directly present</em> on
 122      * this construct.
 123      *
 124      * @return the annotations <em>directly present</em> on this
 125      * construct; an empty list if there are none
 126      */
 127     List<? extends AnnotationMirror> getAnnotationMirrors();
 128 
 129     /**
 130      * Returns this construct's annotation of the specified type if
 131      * such an annotation is <em>present</em>, else {@code null}.

 132      *
 133      * <p> The annotation returned by this method could contain an element
 134      * whose value is of type {@code Class}.
 135      * This value cannot be returned directly:  information necessary to
 136      * locate and load a class (such as the class loader to use) is
 137      * not available, and the class might not be loadable at all.
 138      * Attempting to read a {@code Class} object by invoking the relevant
 139      * method on the returned annotation
 140      * will result in a {@link MirroredTypeException},
 141      * from which the corresponding {@link TypeMirror} may be extracted.
 142      * Similarly, attempting to read a {@code Class[]}-valued element
 143      * will result in a {@link MirroredTypesException}.
 144      *
 145      * <blockquote>
 146      * <i>Note:</i> This method is unlike others in this and related
 147      * interfaces.  It operates on runtime reflective information &mdash;
 148      * representations of annotation types currently loaded into the
 149      * VM &mdash; rather than on the representations defined by and used
 150      * throughout these interfaces.  Consequently, calling methods on
 151      * the returned annotation object can throw many of the exceptions
 152      * that can be thrown when calling methods on an annotation object
 153      * returned by core reflection.  This method is intended for
 154      * callers that are written to operate on a known, fixed set of
 155      * annotation types.
 156      * </blockquote>
 157      *
 158      * @param <A>  the annotation type
 159      * @param annotationType  the {@code Class} object corresponding to
 160      *          the annotation type
 161      * @return this construct's annotation for the specified
 162      * annotation type if present, else {@code null}

 163      *
 164      * @see #getAnnotationMirrors()
 165      * @see java.lang.reflect.AnnotatedElement#getAnnotation
 166      * @see EnumConstantNotPresentException
 167      * @see AnnotationTypeMismatchException
 168      * @see IncompleteAnnotationException
 169      * @see MirroredTypeException
 170      * @see MirroredTypesException
 171      * @jls 9.6.1 Annotation Type Elements
 172      */
 173     <A extends Annotation> A getAnnotation(Class<A> annotationType);
 174 
 175     /**
 176      * Returns annotations that are <em>associated</em> with this construct. 
 177      *
 178      * If there are no annotations associated with this construct, the
 179      * return value is an array of length 0.
 180      *
 181      * The order of annotations which are directly or indirectly
 182      * present on a construct <i>C</i> is computed as if indirectly present
 183      * annotations on <i>C</i> are directly present on <i>C</i> in place of their
 184      * container annotation, in the order in which they appear in the
 185      * value element of the container annotation.
 186      *
 187      * The difference between this method and {@link #getAnnotation(Class)}
 188      * is that this method detects if its argument is a <em>repeatable
 189      * annotation type</em>, and if so, attempts to find one or more
 190      * annotations of that type by "looking through" a container annotation.
 191      *
 192      * <p> The annotations returned by this method could contain an element
 193      * whose value is of type {@code Class}.
 194      * This value cannot be returned directly:  information necessary to
 195      * locate and load a class (such as the class loader to use) is
 196      * not available, and the class might not be loadable at all.
 197      * Attempting to read a {@code Class} object by invoking the relevant
 198      * method on the returned annotation
 199      * will result in a {@link MirroredTypeException},
 200      * from which the corresponding {@link TypeMirror} may be extracted.
 201      * Similarly, attempting to read a {@code Class[]}-valued element
 202      * will result in a {@link MirroredTypesException}.
 203      *
 204      * <blockquote>
 205      * <i>Note:</i> This method is unlike others in this and related
 206      * interfaces.  It operates on runtime reflective information &mdash;
 207      * representations of annotation types currently loaded into the
 208      * VM &mdash; rather than on the representations defined by and used
 209      * throughout these interfaces.  Consequently, calling methods on
 210      * the returned annotation object can throw many of the exceptions
 211      * that can be thrown when calling methods on an annotation object
 212      * returned by core reflection.  This method is intended for
 213      * callers that are written to operate on a known, fixed set of
 214      * annotation types.
 215      * </blockquote>
 216      *
 217      * @param <A>  the annotation type
 218      * @param annotationType  the {@code Class} object corresponding to
 219      *          the annotation type
 220      * @return this construct's annotations for the specified annotation
 221      *         type if present on this construct, else an empty array
 222      *
 223      * @see #getAnnotationMirrors()
 224      * @see #getAnnotation(java.lang.Class)
 225      * @see java.lang.reflect.AnnotatedElement#getAnnotationsByType
 226      * @see EnumConstantNotPresentException
 227      * @see AnnotationTypeMismatchException
 228      * @see IncompleteAnnotationException
 229      * @see MirroredTypeException
 230      * @see MirroredTypesException
 231      * @jls 9.6 Annotation Types
 232      * @jls 9.6.1 Annotation Type Elements
 233      */
 234     <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType);
 235 }