Path: blob/master/src/java.compiler/share/classes/javax/lang/model/AnnotatedConstruct.java
41159 views
/*1* Copyright (c) 2013, 2020, 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* As defined by <cite>The Java Language Specification</cite>42* section {@jls 9.7.4}, an annotation on an element is a43* <em>declaration annotation</em> and an annotation on a type is a44* <em>type annotation</em>.45*46* The terms <em>directly present</em>, <em>present</em>,47* <em>indirectly present</em>, and <em>associated </em> are used48* throughout this interface to describe precisely which annotations,49* either declaration annotations or type annotations, are returned by50* the methods in this interface.51*52* <p>In the definitions below, an annotation <i>A</i> has an53* annotation type <i>AT</i>. If <i>AT</i> is a repeatable annotation54* type, the type of the containing annotation is <i>ATC</i>.55*56* <p>Annotation <i>A</i> is <em>directly present</em> on a construct57* <i>C</i> if either:58*59* <ul>60*61* <li><i>A</i> is {@linkplain62* javax.lang.model.util.Elements#getOrigin(AnnotatedConstruct,63* AnnotationMirror) explicitly or implicitly}64* declared as applying to65* the source code representation of <i>C</i>.66*67* <p>Typically, if exactly one annotation of type <i>AT</i> appears in68* the source code of representation of <i>C</i>, then <i>A</i> is69* explicitly declared as applying to <i>C</i>.70*71* An annotation of type <i>AT</i> on a {@linkplain72* RecordComponentElement record component} can be implicitly propagated73* down to affiliated mandated members. Type annotations modifying the74* type of a record component can be also propagated to mandated75* members. Propagation of the annotations to mandated members is76* governed by rules given in the <cite>The Java Language77* Specification</cite>.78*79* If there are multiple annotations of type <i>AT</i> present on80* <i>C</i>, then if <i>AT</i> is repeatable annotation type, an81* annotation of type <i>ATC</i> is {@linkplain javax.lang.model.util.Elements#getOrigin(AnnotatedConstruct, AnnotationMirror) implicitly declared} on <i>C</i>.82* <li> A representation of <i>A</i> appears in the executable output83* for <i>C</i>, such as the {@code RuntimeVisibleAnnotations} (JVMS {@jvms 4.7.16}) or84* {@code RuntimeVisibleParameterAnnotations} (JVMS {@jvms 4.7.17}) attributes of a class85* file.86*87* </ul>88*89* <p>An annotation <i>A</i> is <em>present</em> on a90* construct <i>C</i> if either:91* <ul>92*93* <li><i>A</i> is directly present on <i>C</i>.94*95* <li>No annotation of type <i>AT</i> is directly present on96* <i>C</i>, and <i>C</i> is a class and <i>AT</i> is inheritable97* and <i>A</i> is present on the superclass of <i>C</i>.98*99* </ul>100*101* An annotation <i>A</i> is <em>indirectly present</em> on a construct102* <i>C</i> if both:103*104* <ul>105*106* <li><i>AT</i> is a repeatable annotation type with a containing107* annotation type <i>ATC</i>.108*109* <li>An annotation of type <i>ATC</i> is directly present on110* <i>C</i> and <i>A</i> is an annotation included in the result of111* calling the {@code value} method of the directly present annotation112* of type <i>ATC</i>.113*114* </ul>115*116* An annotation <i>A</i> is <em>associated</em> with a construct117* <i>C</i> if either:118*119* <ul>120*121* <li> <i>A</i> is directly or indirectly present on <i>C</i>.122*123* <li> No annotation of type <i>AT</i> is directly or indirectly124* present on <i>C</i>, and <i>C</i> is a class, and <i>AT</i> is125* inheritable, and <i>A</i> is associated with the superclass of126* <i>C</i>.127*128* </ul>129*130* @since 1.8131* @jls 9.6 Annotation Interfaces132* @jls 9.6.4.3 {@code @Inherited}133* @jls 9.7.4 Where Annotations May Appear134* @jls 9.7.5 Multiple Annotations of the Same Interface135*/136public interface AnnotatedConstruct {137/**138* Returns the annotations that are <em>directly present</em> on139* this construct.140*141* @return the annotations <em>directly present</em> on this142* construct; an empty list if there are none143*/144List<? extends AnnotationMirror> getAnnotationMirrors();145146/**147* {@return this construct's annotation of the specified type if148* such an annotation is <em>present</em>, else {@code null}}149*150* <p> The annotation returned by this method could contain an element151* whose value is of type {@code Class}.152* This value cannot be returned directly: information necessary to153* locate and load a class (such as the class loader to use) is154* not available, and the class might not be loadable at all.155* Attempting to read a {@code Class} object by invoking the relevant156* method on the returned annotation157* will result in a {@link MirroredTypeException},158* from which the corresponding {@link TypeMirror} may be extracted.159* Similarly, attempting to read a {@code Class[]}-valued element160* will result in a {@link MirroredTypesException}.161*162* <blockquote>163* <i>Note:</i> This method is unlike others in this and related164* interfaces. It operates on runtime reflective information —165* representations of annotation types currently loaded into the166* VM — rather than on the representations defined by and used167* throughout these interfaces. Consequently, calling methods on168* the returned annotation object can throw many of the exceptions169* that can be thrown when calling methods on an annotation object170* returned by core reflection. This method is intended for171* callers that are written to operate on a known, fixed set of172* annotation types.173* </blockquote>174*175* @param <A> the annotation type176* @param annotationType the {@code Class} object corresponding to177* the annotation type178*179* @see #getAnnotationMirrors()180* @see java.lang.reflect.AnnotatedElement#getAnnotation181* @see EnumConstantNotPresentException182* @see AnnotationTypeMismatchException183* @see IncompleteAnnotationException184* @see MirroredTypeException185* @see MirroredTypesException186* @jls 9.6.1 Annotation Interface Elements187*/188<A extends Annotation> A getAnnotation(Class<A> annotationType);189190/**191* Returns annotations that are <em>associated</em> with this construct.192*193* If there are no annotations associated with this construct, the194* return value is an array of length 0.195*196* The order of annotations which are directly or indirectly197* present on a construct <i>C</i> is computed as if indirectly present198* annotations on <i>C</i> are directly present on <i>C</i> in place of their199* container annotation, in the order in which they appear in the200* value element of the container annotation.201*202* The difference between this method and {@link #getAnnotation(Class)}203* is that this method detects if its argument is a <em>repeatable204* annotation type</em>, and if so, attempts to find one or more205* annotations of that type by "looking through" a container annotation.206*207* <p> The annotations returned by this method could contain an element208* whose value is of type {@code Class}.209* This value cannot be returned directly: information necessary to210* locate and load a class (such as the class loader to use) is211* not available, and the class might not be loadable at all.212* Attempting to read a {@code Class} object by invoking the relevant213* method on the returned annotation214* will result in a {@link MirroredTypeException},215* from which the corresponding {@link TypeMirror} may be extracted.216* Similarly, attempting to read a {@code Class[]}-valued element217* will result in a {@link MirroredTypesException}.218*219* <blockquote>220* <i>Note:</i> This method is unlike others in this and related221* interfaces. It operates on runtime reflective information —222* representations of annotation types currently loaded into the223* VM — rather than on the representations defined by and used224* throughout these interfaces. Consequently, calling methods on225* the returned annotation object can throw many of the exceptions226* that can be thrown when calling methods on an annotation object227* returned by core reflection. This method is intended for228* callers that are written to operate on a known, fixed set of229* annotation types.230* </blockquote>231*232* @param <A> the annotation type233* @param annotationType the {@code Class} object corresponding to234* the annotation type235* @return this construct's annotations for the specified annotation236* type if present on this construct, else an empty array237*238* @see #getAnnotationMirrors()239* @see #getAnnotation(Class)240* @see java.lang.reflect.AnnotatedElement#getAnnotationsByType(Class)241* @see EnumConstantNotPresentException242* @see AnnotationTypeMismatchException243* @see IncompleteAnnotationException244* @see MirroredTypeException245* @see MirroredTypesException246* @jls 9.6 Annotation Interfaces247* @jls 9.6.1 Annotation Interface Elements248*/249<A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType);250}251252253