-
Notifications
You must be signed in to change notification settings - Fork 668
/
AnnotatedConstruct.java
243 lines (237 loc) · 10.1 KB
/
AnnotatedConstruct.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.lang.model;
import java.lang.annotation.*;
import java.util.List;
import javax.lang.model.element.*;
import javax.lang.model.type.*;
/**
* Represents a construct that can be annotated.
*
* A construct is either an {@linkplain javax.lang.model.element.Element element}
* or a {@linkplain javax.lang.model.type.TypeMirror type}.
* Annotations on an element are on a <em>declaration</em>,
* whereas annotations on a type are on a specific <em>use</em> of a type name.
*
* The terms <em>directly present</em>, <em>present</em>,
* <em>indirectly present</em>, and <em>associated </em> are used
* throughout this interface to describe precisely which annotations
* are returned by the methods defined herein.
*
* <p>In the definitions below, an annotation <i>A</i> has an
* annotation type <i>AT</i>. If <i>AT</i> is a repeatable annotation
* type, the type of the containing annotation is <i>ATC</i>.
*
* <p>Annotation <i>A</i> is <em>directly present</em> on a construct
* <i>C</i> if either:
*
* <ul>
*
* <li><i>A</i> is explicitly or implicitly declared as applying to
* the source code representation of <i>C</i>.
*
* <p>Typically, if exactly one annotation of type <i>AT</i> appears in
* the source code of representation of <i>C</i>, then <i>A</i> is
* explicitly declared as applying to <i>C</i>.
*
* If there are multiple annotations of type <i>AT</i> present on
* <i>C</i>, then if <i>AT</i> is repeatable annotation type, an
* annotation of type <i>ATC</i> is {@linkplain
* javax.lang.model.util.Elements#getOrigin(AnnotatedConstruct, AnnotationMirror) implicitly declared}
* on <i>C</i>.
*
* <li> A representation of <i>A</i> appears in the executable output
* for <i>C</i>, such as the {@code RuntimeVisibleAnnotations} or
* {@code RuntimeVisibleParameterAnnotations} attributes of a class
* file.
*
* </ul>
*
* <p>An annotation <i>A</i> is <em>present</em> on a
* construct <i>C</i> if either:
* <ul>
*
* <li><i>A</i> is directly present on <i>C</i>.
*
* <li>No annotation of type <i>AT</i> is directly present on
* <i>C</i>, and <i>C</i> is a class and <i>AT</i> is inheritable
* and <i>A</i> is present on the superclass of <i>C</i>.
*
* </ul>
*
* An annotation <i>A</i> is <em>indirectly present</em> on a construct
* <i>C</i> if both:
*
* <ul>
*
* <li><i>AT</i> is a repeatable annotation type with a containing
* annotation type <i>ATC</i>.
*
* <li>An annotation of type <i>ATC</i> is directly present on
* <i>C</i> and <i>A</i> is an annotation included in the result of
* calling the {@code value} method of the directly present annotation
* of type <i>ATC</i>.
*
* </ul>
*
* An annotation <i>A</i> is <em>associated</em> with a construct
* <i>C</i> if either:
*
* <ul>
*
* <li> <i>A</i> is directly or indirectly present on <i>C</i>.
*
* <li> No annotation of type <i>AT</i> is directly or indirectly
* present on <i>C</i>, and <i>C</i> is a class, and <i>AT</i> is
* inheritable, and <i>A</i> is associated with the superclass of
* <i>C</i>.
*
* </ul>
*
* @jls 9.6 Annotation Types
* @jls 9.6.3.3 @Inherited
* @since 1.8
*/
// 表示一个可以被注解的结构,这个结构可以是Element或TypeMirror
public interface AnnotatedConstruct {
/**
* Returns the annotations that are <em>directly present</em> on
* this construct.
*
* @return the annotations <em>directly present</em> on this
* construct; an empty list if there are none
*/
// 返回直接声明在该结构上的注解,如果没有直接声明的注解,则返回空集合.
List<? extends AnnotationMirror> getAnnotationMirrors();
/**
* Returns this construct's annotation of the specified type if
* such an annotation is <em>present</em>, else {@code null}.
*
* <p> The annotation returned by this method could contain an element
* whose value is of type {@code Class}.
* This value cannot be returned directly: information necessary to
* locate and load a class (such as the class loader to use) is
* not available, and the class might not be loadable at all.
* Attempting to read a {@code Class} object by invoking the relevant
* method on the returned annotation
* will result in a {@link MirroredTypeException},
* from which the corresponding {@link TypeMirror} may be extracted.
* Similarly, attempting to read a {@code Class[]}-valued element
* will result in a {@link MirroredTypesException}.
*
* <blockquote>
* <i>Note:</i> This method is unlike others in this and related
* interfaces. It operates on runtime reflective information —
* representations of annotation types currently loaded into the
* VM — rather than on the representations defined by and used
* throughout these interfaces. Consequently, calling methods on
* the returned annotation object can throw many of the exceptions
* that can be thrown when calling methods on an annotation object
* returned by core reflection. This method is intended for
* callers that are written to operate on a known, fixed set of
* annotation types.
* </blockquote>
*
* @param <A> the annotation type
* @param annotationType the {@code Class} object corresponding to
* the annotation type
*
* @return this construct's annotation for the specified
* annotation type if present, else {@code null}
*
* @jls 9.6.1 Annotation Type Elements
* @see #getAnnotationMirrors()
* @see java.lang.reflect.AnnotatedElement#getAnnotation
* @see EnumConstantNotPresentException
* @see AnnotationTypeMismatchException
* @see IncompleteAnnotationException
* @see MirroredTypeException
* @see MirroredTypesException
*/
// 返回指定类型的注解
<A extends Annotation> A getAnnotation(Class<A> annotationType);
/**
* Returns annotations that are <em>associated</em> with this construct.
*
* If there are no annotations associated with this construct, the
* return value is an array of length 0.
*
* The order of annotations which are directly or indirectly
* present on a construct <i>C</i> is computed as if indirectly present
* annotations on <i>C</i> are directly present on <i>C</i> in place of their
* container annotation, in the order in which they appear in the
* value element of the container annotation.
*
* The difference between this method and {@link #getAnnotation(Class)}
* is that this method detects if its argument is a <em>repeatable
* annotation type</em>, and if so, attempts to find one or more
* annotations of that type by "looking through" a container annotation.
*
* <p> The annotations returned by this method could contain an element
* whose value is of type {@code Class}.
* This value cannot be returned directly: information necessary to
* locate and load a class (such as the class loader to use) is
* not available, and the class might not be loadable at all.
* Attempting to read a {@code Class} object by invoking the relevant
* method on the returned annotation
* will result in a {@link MirroredTypeException},
* from which the corresponding {@link TypeMirror} may be extracted.
* Similarly, attempting to read a {@code Class[]}-valued element
* will result in a {@link MirroredTypesException}.
*
* <blockquote>
* <i>Note:</i> This method is unlike others in this and related
* interfaces. It operates on runtime reflective information —
* representations of annotation types currently loaded into the
* VM — rather than on the representations defined by and used
* throughout these interfaces. Consequently, calling methods on
* the returned annotation object can throw many of the exceptions
* that can be thrown when calling methods on an annotation object
* returned by core reflection. This method is intended for
* callers that are written to operate on a known, fixed set of
* annotation types.
* </blockquote>
*
* @param <A> the annotation type
* @param annotationType the {@code Class} object corresponding to
* the annotation type
*
* @return this construct's annotations for the specified annotation
* type if present on this construct, else an empty array
*
* @jls 9.6 Annotation Types
* @jls 9.6.1 Annotation Type Elements
* @see #getAnnotationMirrors()
* @see #getAnnotation(Class)
* @see java.lang.reflect.AnnotatedElement#getAnnotationsByType(Class)
* @see EnumConstantNotPresentException
* @see AnnotationTypeMismatchException
* @see IncompleteAnnotationException
* @see MirroredTypeException
* @see MirroredTypesException
*/
// 返回指定类型的注解,支持可重复注解
<A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType);
}