1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This code is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 only, as
8  * published by the Free Software Foundation.  Oracle designates this
9  * particular file as subject to the "Classpath" exception as provided
10  * by Oracle in the LICENSE file that accompanied this code.
11  *
12  * This code is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15  * version 2 for more details (a copy is included in the LICENSE file that
16  * accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License version
19  * 2 along with this work; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23  * or visit www.oracle.com if you need additional information or have any
24  * questions.
25  */
26 
27 package java.lang.reflect;
28 
29 import java.lang.annotation.Annotation;
30 import java.util.Objects;
31 import libcore.reflect.AnnotatedElements;
32 
33 // Android-changed: Removed some references to bytecode spec below that do not
34 // apply and added a note about annotation ordering.
35 /**
36  * Represents an annotated element of the program currently running in this
37  * VM.  This interface allows annotations to be read reflectively.  All
38  * annotations returned by methods in this interface are immutable and
39  * serializable. The arrays returned by methods of this interface may be modified
40  * by callers without affecting the arrays returned to other callers.
41  *
42  * <p>Android note: methods that return multiple annotations of different types such as
43  * {@link #getAnnotations()} and {@link #getDeclaredAnnotations()} can be affected
44  * by the explicit character-code ordering of annotations types specified by the DEX format.
45  * Annotations of different types on a single element are not guaranteed to be returned in the order
46  * they are declared in source.
47  *
48  * <p>The {@link #getAnnotationsByType(Class)} and {@link
49  * #getDeclaredAnnotationsByType(Class)} methods support multiple
50  * annotations of the same type on an element. If the argument to
51  * either method is a repeatable annotation type (JLS 9.6), then the
52  * method will "look through" a container annotation (JLS 9.7), if
53  * present, and return any annotations inside the container. Container
54  * annotations may be generated at compile-time to wrap multiple
55  * annotations of the argument type.
56  *
57  * <p>The terms <em>directly present</em>, <em>indirectly present</em>,
58  * <em>present</em>, and <em>associated</em> are used throughout this
59  * interface to describe precisely which annotations are returned by
60  * methods:
61  *
62  * <ul>
63  *
64  * <li> An annotation <i>A</i> is <em>directly present</em> on an
65  * element <i>E</i> if <i>E</i> is annotated by <i>A</i> in the original source.
66  *
67  * <li>An annotation <i>A</i> is <em>indirectly present</em> on an
68  * element <i>E</i> if <i>E</i> is annotated by a container annotation
69  * of <i>A</i>.
70  *
71  * <li>An annotation <i>A</i> is present on an element <i>E</i> if either:
72  *
73  * <ul>
74  *
75  * <li><i>A</i> is directly present on <i>E</i>; or
76  *
77  * <li>No annotation of <i>A</i> 's type is directly present on
78  * <i>E</i>, and <i>E</i> is a class, and <i>A</i> 's type is
79  * inheritable, and <i>A</i> is present on the superclass of <i>E</i>.
80  *
81  * </ul>
82  *
83  * <li>An annotation <i>A</i> is <em>associated</em> with an element <i>E</i>
84  * if either:
85  *
86  * <ul>
87  *
88  * <li><i>A</i> is directly or indirectly present on <i>E</i>; or
89  *
90  * <li>No annotation of <i>A</i> 's type is directly or indirectly
91  * present on <i>E</i>, and <i>E</i> is a class, and <i>A</i>'s type
92  * is inheritable, and <i>A</i> is associated with the superclass of
93  * <i>E</i>.
94  *
95  * </ul>
96  *
97  * </ul>
98  *
99  * <p>The table below summarizes which kind of annotation presence
100  * different methods in this interface examine.
101  *
102  * <table border>
103  * <caption>Overview of kind of presence detected by different AnnotatedElement methods</caption>
104  * <tr><th colspan=2></th><th colspan=4>Kind of Presence</th>
105  * <tr><th colspan=2>Method</th><th>Directly Present</th><th>Indirectly Present</th><th>Present</th><th>Associated</th>
106  * <tr><td align=right>{@code T}</td><td>{@link #getAnnotation(Class) getAnnotation(Class&lt;T&gt;)}
107  * <td></td><td></td><td>X</td><td></td>
108  * </tr>
109  * <tr><td align=right>{@code Annotation[]}</td><td>{@link #getAnnotations getAnnotations()}
110  * <td></td><td></td><td>X</td><td></td>
111  * </tr>
112  * <tr><td align=right>{@code T[]}</td><td>{@link #getAnnotationsByType(Class) getAnnotationsByType(Class&lt;T&gt;)}
113  * <td></td><td></td><td></td><td>X</td>
114  * </tr>
115  * <tr><td align=right>{@code T}</td><td>{@link #getDeclaredAnnotation(Class) getDeclaredAnnotation(Class&lt;T&gt;)}
116  * <td>X</td><td></td><td></td><td></td>
117  * </tr>
118  * <tr><td align=right>{@code Annotation[]}</td><td>{@link #getDeclaredAnnotations getDeclaredAnnotations()}
119  * <td>X</td><td></td><td></td><td></td>
120  * </tr>
121  * <tr><td align=right>{@code T[]}</td><td>{@link #getDeclaredAnnotationsByType(Class) getDeclaredAnnotationsByType(Class&lt;T&gt;)}
122  * <td>X</td><td>X</td><td></td><td></td>
123  * </tr>
124  * </table>
125  *
126  * <p>For an invocation of {@code get[Declared]AnnotationsByType( Class <
127  * T >)}, the order of annotations which are directly or indirectly
128  * present on an element <i>E</i> is computed as if indirectly present
129  * annotations on <i>E</i> are directly present on <i>E</i> in place
130  * of their container annotation, in the order in which they appear in
131  * the value element of the container annotation.
132  *
133  * <p>There are several compatibility concerns to keep in mind if an
134  * annotation type <i>T</i> is originally <em>not</em> repeatable and
135  * later modified to be repeatable.
136  *
137  * The containing annotation type for <i>T</i> is <i>TC</i>.
138  *
139  * <ul>
140  *
141  * <li>Modifying <i>T</i> to be repeatable is source and binary
142  * compatible with existing uses of <i>T</i> and with existing uses
143  * of <i>TC</i>.
144  *
145  * That is, for source compatibility, source code with annotations of
146  * type <i>T</i> or of type <i>TC</i> will still compile. For binary
147  * compatibility, class files with annotations of type <i>T</i> or of
148  * type <i>TC</i> (or with other kinds of uses of type <i>T</i> or of
149  * type <i>TC</i>) will link against the modified version of <i>T</i>
150  * if they linked against the earlier version.
151  *
152  * (An annotation type <i>TC</i> may informally serve as an acting
153  * containing annotation type before <i>T</i> is modified to be
154  * formally repeatable. Alternatively, when <i>T</i> is made
155  * repeatable, <i>TC</i> can be introduced as a new type.)
156  *
157  * <li>If an annotation type <i>TC</i> is present on an element, and
158  * <i>T</i> is modified to be repeatable with <i>TC</i> as its
159  * containing annotation type then:
160  *
161  * <ul>
162  *
163  * <li>The change to <i>T</i> is behaviorally compatible with respect
164  * to the {@code get[Declared]Annotation(Class<T>)} (called with an
165  * argument of <i>T</i> or <i>TC</i>) and {@code
166  * get[Declared]Annotations()} methods because the results of the
167  * methods will not change due to <i>TC</i> becoming the containing
168  * annotation type for <i>T</i>.
169  *
170  * <li>The change to <i>T</i> changes the results of the {@code
171  * get[Declared]AnnotationsByType(Class<T>)} methods called with an
172  * argument of <i>T</i>, because those methods will now recognize an
173  * annotation of type <i>TC</i> as a container annotation for <i>T</i>
174  * and will "look through" it to expose annotations of type <i>T</i>.
175  *
176  * </ul>
177  *
178  * <li>If an annotation of type <i>T</i> is present on an
179  * element and <i>T</i> is made repeatable and more annotations of
180  * type <i>T</i> are added to the element:
181  *
182  * <ul>
183  *
184  * <li> The addition of the annotations of type <i>T</i> is both
185  * source compatible and binary compatible.
186  *
187  * <li>The addition of the annotations of type <i>T</i> changes the results
188  * of the {@code get[Declared]Annotation(Class<T>)} methods and {@code
189  * get[Declared]Annotations()} methods, because those methods will now
190  * only see a container annotation on the element and not see an
191  * annotation of type <i>T</i>.
192  *
193  * <li>The addition of the annotations of type <i>T</i> changes the
194  * results of the {@code get[Declared]AnnotationsByType(Class<T>)}
195  * methods, because their results will expose the additional
196  * annotations of type <i>T</i> whereas previously they exposed only a
197  * single annotation of type <i>T</i>.
198  *
199  * </ul>
200  *
201  * </ul>
202  *
203  * <p>If an annotation returned by a method in this interface contains
204  * (directly or indirectly) a {@link Class}-valued member referring to
205  * a class that is not accessible in this VM, attempting to read the class
206  * by calling the relevant Class-returning method on the returned annotation
207  * will result in a {@link TypeNotPresentException}.
208  *
209  * <p>Similarly, attempting to read an enum-valued member will result in
210  * a {@link EnumConstantNotPresentException} if the enum constant in the
211  * annotation is no longer present in the enum type.
212  *
213  * <p>If an annotation type <i>T</i> is (meta-)annotated with an
214  * {@code @Repeatable} annotation whose value element indicates a type
215  * <i>TC</i>, but <i>TC</i> does not declare a {@code value()} method
216  * with a return type of <i>T</i>{@code []}, then an exception of type
217  * {@link java.lang.annotation.AnnotationFormatError} is thrown.
218  *
219  * <p>Finally, attempting to read a member whose definition has evolved
220  * incompatibly will result in a {@link
221  * java.lang.annotation.AnnotationTypeMismatchException} or an
222  * {@link java.lang.annotation.IncompleteAnnotationException}.
223  *
224  * @see java.lang.EnumConstantNotPresentException
225  * @see java.lang.TypeNotPresentException
226  * @see AnnotationFormatError
227  * @see java.lang.annotation.AnnotationTypeMismatchException
228  * @see java.lang.annotation.IncompleteAnnotationException
229  * @since 1.5
230  * @author Josh Bloch
231  */
232 public interface AnnotatedElement {
233     /**
234      * Returns true if an annotation for the specified type
235      * is <em>present</em> on this element, else false.  This method
236      * is designed primarily for convenient access to marker annotations.
237      *
238      * <p>The truth value returned by this method is equivalent to:
239      * {@code getAnnotation(annotationClass) != null}
240      *
241      * <p>The body of the default method is specified to be the code
242      * above.
243      *
244      * @param annotationClass the Class object corresponding to the
245      *        annotation type
246      * @return true if an annotation for the specified annotation
247      *     type is present on this element, else false
248      * @throws NullPointerException if the given annotation class is null
249      * @since 1.5
250      */
isAnnotationPresent(Class<? extends Annotation> annotationClass)251     default boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
252         return getAnnotation(annotationClass) != null;
253     }
254 
255    /**
256      * Returns this element's annotation for the specified type if
257      * such an annotation is <em>present</em>, else null.
258      *
259      * @param <T> the type of the annotation to query for and return if present
260      * @param annotationClass the Class object corresponding to the
261      *        annotation type
262      * @return this element's annotation for the specified annotation type if
263      *     present on this element, else null
264      * @throws NullPointerException if the given annotation class is null
265      * @since 1.5
266      */
getAnnotation(Class<T> annotationClass)267     <T extends Annotation> T getAnnotation(Class<T> annotationClass);
268 
269     /**
270      * Returns annotations that are <em>present</em> on this element.
271      *
272      * If there are no annotations <em>present</em> on this element, the return
273      * value is an array of length 0.
274      *
275      * The caller of this method is free to modify the returned array; it will
276      * have no effect on the arrays returned to other callers.
277      *
278      * @return annotations present on this element
279      * @since 1.5
280      */
getAnnotations()281     Annotation[] getAnnotations();
282 
283     /**
284      * Returns annotations that are <em>associated</em> with this element.
285      *
286      * If there are no annotations <em>associated</em> with this element, the return
287      * value is an array of length 0.
288      *
289      * The difference between this method and {@link #getAnnotation(Class)}
290      * is that this method detects if its argument is a <em>repeatable
291      * annotation type</em> (JLS 9.6), and if so, attempts to find one or
292      * more annotations of that type by "looking through" a container
293      * annotation.
294      *
295      * The caller of this method is free to modify the returned array; it will
296      * have no effect on the arrays returned to other callers.
297      *
298      * @implSpec The default implementation first calls {@link
299      * #getDeclaredAnnotationsByType(Class)} passing {@code
300      * annotationClass} as the argument. If the returned array has
301      * length greater than zero, the array is returned. If the returned
302      * array is zero-length and this {@code AnnotatedElement} is a
303      * class and the argument type is an inheritable annotation type,
304      * and the superclass of this {@code AnnotatedElement} is non-null,
305      * then the returned result is the result of calling {@link
306      * #getAnnotationsByType(Class)} on the superclass with {@code
307      * annotationClass} as the argument. Otherwise, a zero-length
308      * array is returned.
309      *
310      * @param <T> the type of the annotation to query for and return if present
311      * @param annotationClass the Class object corresponding to the
312      *        annotation type
313      * @return all this element's annotations for the specified annotation type if
314      *     associated with this element, else an array of length zero
315      * @throws NullPointerException if the given annotation class is null
316      * @since 1.8
317      */
getAnnotationsByType(Class<T> annotationClass)318     default <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
319         // This method does not handle inherited annotations and is intended for use for
320         // {@code Method}, {@code Field}, {@code Package}. The {@link Class#getAnnotationsByType}
321         // is implemented explicitly. Therefore this implementation does not fulfill the documented
322         // default implementation for {@link AnnotatedElement#getAnnotationsByType(Class)} but in an
323         // undetectable way because Class is final.
324         return AnnotatedElements.getDirectOrIndirectAnnotationsByType(this, annotationClass);
325     }
326 
327     /**
328      * Returns this element's annotation for the specified type if
329      * such an annotation is <em>directly present</em>, else null.
330      *
331      * This method ignores inherited annotations. (Returns null if no
332      * annotations are directly present on this element.)
333      *
334      * @implSpec The default implementation first performs a null check
335      * and then loops over the results of {@link
336      * #getDeclaredAnnotations} returning the first annotation whose
337      * annotation type matches the argument type.
338      *
339      * @param <T> the type of the annotation to query for and return if directly present
340      * @param annotationClass the Class object corresponding to the
341      *        annotation type
342      * @return this element's annotation for the specified annotation type if
343      *     directly present on this element, else null
344      * @throws NullPointerException if the given annotation class is null
345      * @since 1.8
346      */
getDeclaredAnnotation(Class<T> annotationClass)347     default <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass) {
348         Objects.requireNonNull(annotationClass);
349         // Loop over all directly-present annotations looking for a matching one
350         for (Annotation annotation : getDeclaredAnnotations()) {
351             if (annotationClass.equals(annotation.annotationType())) {
352                 // More robust to do a dynamic cast at runtime instead
353                 // of compile-time only.
354                 return annotationClass.cast(annotation);
355             }
356         }
357         return null;
358     }
359 
360     /**
361      * Returns this element's annotation(s) for the specified type if
362      * such annotations are either <em>directly present</em> or
363      * <em>indirectly present</em>. This method ignores inherited
364      * annotations.
365      *
366      * If there are no specified annotations directly or indirectly
367      * present on this element, the return value is an array of length
368      * 0.
369      *
370      * The difference between this method and {@link
371      * #getDeclaredAnnotation(Class)} is that this method detects if its
372      * argument is a <em>repeatable annotation type</em> (JLS 9.6), and if so,
373      * attempts to find one or more annotations of that type by "looking
374      * through" a container annotation if one is present.
375      *
376      * The caller of this method is free to modify the returned array; it will
377      * have no effect on the arrays returned to other callers.
378      *
379      * @implSpec The default implementation may call {@link
380      * #getDeclaredAnnotation(Class)} one or more times to find a
381      * directly present annotation and, if the annotation type is
382      * repeatable, to find a container annotation. If annotations of
383      * the annotation type {@code annotationClass} are found to be both
384      * directly and indirectly present, then {@link
385      * #getDeclaredAnnotations()} will get called to determine the
386      * order of the elements in the returned array.
387      *
388      * <p>Alternatively, the default implementation may call {@link
389      * #getDeclaredAnnotations()} a single time and the returned array
390      * examined for both directly and indirectly present
391      * annotations. The results of calling {@link
392      * #getDeclaredAnnotations()} are assumed to be consistent with the
393      * results of calling {@link #getDeclaredAnnotation(Class)}.
394      *
395      * @param <T> the type of the annotation to query for and return
396      * if directly or indirectly present
397      * @param annotationClass the Class object corresponding to the
398      *        annotation type
399      * @return all this element's annotations for the specified annotation type if
400      *     directly or indirectly present on this element, else an array of length zero
401      * @throws NullPointerException if the given annotation class is null
402      * @since 1.8
403      */
getDeclaredAnnotationsByType(Class<T> annotationClass)404     default <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass) {
405         return AnnotatedElements.getDirectOrIndirectAnnotationsByType(this, annotationClass);
406     }
407 
408     /**
409      * Returns annotations that are <em>directly present</em> on this element.
410      * This method ignores inherited annotations.
411      *
412      * If there are no annotations <em>directly present</em> on this element,
413      * the return value is an array of length 0.
414      *
415      * The caller of this method is free to modify the returned array; it will
416      * have no effect on the arrays returned to other callers.
417      *
418      * @return annotations directly present on this element
419      * @since 1.5
420      */
getDeclaredAnnotations()421     Annotation[] getDeclaredAnnotations();
422 }
423