Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/javax/lang/model/AnnotatedConstruct.java
38890 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.lang.model;2627import java.lang.annotation.*;28import java.util.List;29import javax.lang.model.element.*;30import javax.lang.model.type.*;3132/**33* Represents a construct that can be annotated.34*35* A construct is either an {@linkplain36* javax.lang.model.element.Element element} or a {@linkplain37* javax.lang.model.type.TypeMirror type}. Annotations on an element38* are on a <em>declaration</em>, whereas annotations on a type are on39* a specific <em>use</em> of a type name.40*41* The terms <em>directly present</em>, <em>present</em>,42* <em>indirectly present</em>, and <em>associated </em> are used43* throughout this interface to describe precisely which annotations44* are returned by the methods defined herein.45*46* <p>In the definitions below, an annotation <i>A</i> has an47* annotation type <i>AT</i>. If <i>AT</i> is a repeatable annotation48* type, the type of the containing annotation is <i>ATC</i>.49*50* <p>Annotation <i>A</i> is <em>directly present</em> on a construct51* <i>C</i> if either:52*53* <ul>54*55* <li><i>A</i> is explicitly or implicitly declared as applying to56* the source code representation of <i>C</i>.57*58* <p>Typically, if exactly one annotation of type <i>AT</i> appears in59* the source code of representation of <i>C</i>, then <i>A</i> is60* explicitly declared as applying to <i>C</i>.61*62* If there are multiple annotations of type <i>AT</i> present on63* <i>C</i>, then if <i>AT</i> is repeatable annotation type, an64* annotation of type <i>ATC</i> is implicitly declared on <i>C</i>.65*66* <li> A representation of <i>A</i> appears in the executable output67* for <i>C</i>, such as the {@code RuntimeVisibleAnnotations} or68* {@code RuntimeVisibleParameterAnnotations} attributes of a class69* file.70*71* </ul>72*73* <p>An annotation <i>A</i> is <em>present</em> on a74* construct <i>C</i> if either:75* <ul>76*77* <li><i>A</i> is directly present on <i>C</i>.78*79* <li>No annotation of type <i>AT</i> is directly present on80* <i>C</i>, and <i>C</i> is a class and <i>AT</i> is inheritable81* and <i>A</i> is present on the superclass of <i>C</i>.82*83* </ul>84*85* An annotation <i>A</i> is <em>indirectly present</em> on a construct86* <i>C</i> if both:87*88* <ul>89*90* <li><i>AT</i> is a repeatable annotation type with a containing91* annotation type <i>ATC</i>.92*93* <li>An annotation of type <i>ATC</i> is directly present on94* <i>C</i> and <i>A</i> is an annotation included in the result of95* calling the {@code value} method of the directly present annotation96* of type <i>ATC</i>.97*98* </ul>99*100* An annotation <i>A</i> is <em>associated</em> with a construct101* <i>C</i> if either:102*103* <ul>104*105* <li> <i>A</i> is directly or indirectly present on <i>C</i>.106*107* <li> No annotation of type <i>AT</i> is directly or indirectly108* present on <i>C</i>, and <i>C</i> is a class, and <i>AT</i> is109* inheritable, and <i>A</i> is associated with the superclass of110* <i>C</i>.111*112* </ul>113*114* @since 1.8115* @jls 9.6 Annotation Types116* @jls 9.6.3.3 @Inherited117*/118public interface AnnotatedConstruct {119/**120* Returns the annotations that are <em>directly present</em> on121* this construct.122*123* @return the annotations <em>directly present</em> on this124* construct; an empty list if there are none125*/126List<? extends AnnotationMirror> getAnnotationMirrors();127128/**129* Returns this construct's annotation of the specified type if130* such an annotation is <em>present</em>, else {@code null}.131*132* <p> The annotation returned by this method could contain an element133* whose value is of type {@code Class}.134* This value cannot be returned directly: information necessary to135* locate and load a class (such as the class loader to use) is136* not available, and the class might not be loadable at all.137* Attempting to read a {@code Class} object by invoking the relevant138* method on the returned annotation139* will result in a {@link MirroredTypeException},140* from which the corresponding {@link TypeMirror} may be extracted.141* Similarly, attempting to read a {@code Class[]}-valued element142* will result in a {@link MirroredTypesException}.143*144* <blockquote>145* <i>Note:</i> This method is unlike others in this and related146* interfaces. It operates on runtime reflective information —147* representations of annotation types currently loaded into the148* VM — rather than on the representations defined by and used149* throughout these interfaces. Consequently, calling methods on150* the returned annotation object can throw many of the exceptions151* that can be thrown when calling methods on an annotation object152* returned by core reflection. This method is intended for153* callers that are written to operate on a known, fixed set of154* annotation types.155* </blockquote>156*157* @param <A> the annotation type158* @param annotationType the {@code Class} object corresponding to159* the annotation type160* @return this construct's annotation for the specified161* annotation type if present, else {@code null}162*163* @see #getAnnotationMirrors()164* @see java.lang.reflect.AnnotatedElement#getAnnotation165* @see EnumConstantNotPresentException166* @see AnnotationTypeMismatchException167* @see IncompleteAnnotationException168* @see MirroredTypeException169* @see MirroredTypesException170* @jls 9.6.1 Annotation Type Elements171*/172<A extends Annotation> A getAnnotation(Class<A> annotationType);173174/**175* Returns annotations that are <em>associated</em> with this construct.176*177* If there are no annotations associated with this construct, the178* return value is an array of length 0.179*180* The order of annotations which are directly or indirectly181* present on a construct <i>C</i> is computed as if indirectly present182* annotations on <i>C</i> are directly present on <i>C</i> in place of their183* container annotation, in the order in which they appear in the184* value element of the container annotation.185*186* The difference between this method and {@link #getAnnotation(Class)}187* is that this method detects if its argument is a <em>repeatable188* annotation type</em>, and if so, attempts to find one or more189* annotations of that type by "looking through" a container annotation.190*191* <p> The annotations returned by this method could contain an element192* whose value is of type {@code Class}.193* This value cannot be returned directly: information necessary to194* locate and load a class (such as the class loader to use) is195* not available, and the class might not be loadable at all.196* Attempting to read a {@code Class} object by invoking the relevant197* method on the returned annotation198* will result in a {@link MirroredTypeException},199* from which the corresponding {@link TypeMirror} may be extracted.200* Similarly, attempting to read a {@code Class[]}-valued element201* will result in a {@link MirroredTypesException}.202*203* <blockquote>204* <i>Note:</i> This method is unlike others in this and related205* interfaces. It operates on runtime reflective information —206* representations of annotation types currently loaded into the207* VM — rather than on the representations defined by and used208* throughout these interfaces. Consequently, calling methods on209* the returned annotation object can throw many of the exceptions210* that can be thrown when calling methods on an annotation object211* returned by core reflection. This method is intended for212* callers that are written to operate on a known, fixed set of213* annotation types.214* </blockquote>215*216* @param <A> the annotation type217* @param annotationType the {@code Class} object corresponding to218* the annotation type219* @return this construct's annotations for the specified annotation220* type if present on this construct, else an empty array221*222* @see #getAnnotationMirrors()223* @see #getAnnotation(Class)224* @see java.lang.reflect.AnnotatedElement#getAnnotationsByType(Class)225* @see EnumConstantNotPresentException226* @see AnnotationTypeMismatchException227* @see IncompleteAnnotationException228* @see MirroredTypeException229* @see MirroredTypesException230* @jls 9.6 Annotation Types231* @jls 9.6.1 Annotation Type Elements232*/233<A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType);234}235236237