AnnotatedConstruct.java revision 2571:10fc81ac75b4
1/*
2 * Copyright (c) 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
26package javax.lang.model;
27
28import java.lang.annotation.*;
29import java.util.List;
30import javax.lang.model.element.*;
31import 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 */
119public 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(Class)
225     * @see java.lang.reflect.AnnotatedElement#getAnnotationsByType(Class)
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}
236