-
Notifications
You must be signed in to change notification settings - Fork 26.3k
/
AnnotationUtils.java
482 lines (432 loc) · 20.8 KB
/
AnnotationUtils.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.common.utils;
import java.lang.annotation.Annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.Predicate;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.unmodifiableList;
import static org.apache.dubbo.common.function.Predicates.and;
import static org.apache.dubbo.common.function.Streams.filterAll;
import static org.apache.dubbo.common.function.Streams.filterFirst;
import static org.apache.dubbo.common.utils.ClassUtils.getAllInheritedTypes;
import static org.apache.dubbo.common.utils.ClassUtils.resolveClass;
import static org.apache.dubbo.common.utils.CollectionUtils.first;
import static org.apache.dubbo.common.utils.MethodUtils.findMethod;
import static org.apache.dubbo.common.utils.MethodUtils.invokeMethod;
/**
* Commons Annotation Utilities class
*
* @since 2.7.6
*/
public interface AnnotationUtils {
/**
* Resolve the annotation type by the annotated element and resolved class name
*
* @param annotatedElement the annotated element
* @param annotationClassName the class name of annotation
* @param <A> the type of annotation
* @return If resolved, return the type of annotation, or <code>null</code>
*/
static <A extends Annotation> Class<A> resolveAnnotationType(AnnotatedElement annotatedElement,
String annotationClassName) {
ClassLoader classLoader = annotatedElement.getClass().getClassLoader();
Class<?> annotationType = resolveClass(annotationClassName, classLoader);
if (annotationType == null || !Annotation.class.isAssignableFrom(annotationType)) {
return null;
}
return (Class<A>) annotationType;
}
/**
* Is the specified type a generic {@link Class type}
*
* @param annotatedElement the annotated element
* @return if <code>annotatedElement</code> is the {@link Class}, return <code>true</code>, or <code>false</code>
* @see ElementType#TYPE
*/
static boolean isType(AnnotatedElement annotatedElement) {
return annotatedElement instanceof Class;
}
/**
* Is the type of specified annotation same to the expected type?
*
* @param annotation the specified {@link Annotation}
* @param annotationType the expected annotation type
* @return if same, return <code>true</code>, or <code>false</code>
*/
static boolean isSameType(Annotation annotation, Class<? extends Annotation> annotationType) {
if (annotation == null || annotationType == null) {
return false;
}
return Objects.equals(annotation.annotationType(), annotationType);
}
/**
* Build an instance of {@link Predicate} to excluded annotation type
*
* @param excludedAnnotationType excluded annotation type
* @return non-null
*/
static Predicate<Annotation> excludedType(Class<? extends Annotation> excludedAnnotationType) {
return annotation -> !isSameType(annotation, excludedAnnotationType);
}
/**
* Get the attribute from the specified {@link Annotation annotation}
*
* @param annotation the specified {@link Annotation annotation}
* @param attributeName the attribute name
* @param <T> the type of attribute
* @return the attribute value
* @throws IllegalArgumentException If the attribute name can't be found
*/
static <T> T getAttribute(Annotation annotation, String attributeName) throws IllegalArgumentException {
return annotation == null ? null : invokeMethod(annotation, attributeName);
}
/**
* Get the "value" attribute from the specified {@link Annotation annotation}
*
* @param annotation the specified {@link Annotation annotation}
* @param <T> the type of attribute
* @return the value of "value" attribute
* @throws IllegalArgumentException If the attribute name can't be found
*/
static <T> T getValue(Annotation annotation) throws IllegalArgumentException {
return getAttribute(annotation, "value");
}
/**
* Get the {@link Annotation} from the specified {@link AnnotatedElement the annotated element} and
* {@link Annotation annotation} class name
*
* @param annotatedElement {@link AnnotatedElement}
* @param annotationClassName the class name of annotation
* @param <A> The type of {@link Annotation}
* @return the {@link Annotation} if found
* @throws ClassCastException If the {@link Annotation annotation} type that client requires can't match actual type
*/
static <A extends Annotation> A getAnnotation(AnnotatedElement annotatedElement, String annotationClassName)
throws ClassCastException {
Class<? extends Annotation> annotationType = resolveAnnotationType(annotatedElement, annotationClassName);
if (annotationType == null) {
return null;
}
return (A) annotatedElement.getAnnotation(annotationType);
}
/**
* Get annotations that are <em>directly present</em> on this element.
* This method ignores inherited annotations.
*
* @param annotatedElement the annotated element
* @param annotationsToFilter the annotations to filter
* @return non-null read-only {@link List}
*/
static List<Annotation> getDeclaredAnnotations(AnnotatedElement annotatedElement,
Predicate<Annotation>... annotationsToFilter) {
if (annotatedElement == null) {
return emptyList();
}
return unmodifiableList(filterAll(asList(annotatedElement.getDeclaredAnnotations()), annotationsToFilter));
}
/**
* Get all directly declared annotations of the the annotated element, not including
* meta annotations.
*
* @param annotatedElement the annotated element
* @param annotationsToFilter the annotations to filter
* @return non-null read-only {@link List}
*/
static List<Annotation> getAllDeclaredAnnotations(AnnotatedElement annotatedElement,
Predicate<Annotation>... annotationsToFilter) {
if (isType(annotatedElement)) {
return getAllDeclaredAnnotations((Class) annotatedElement, annotationsToFilter);
} else {
return getDeclaredAnnotations(annotatedElement, annotationsToFilter);
}
}
/**
* Get all directly declared annotations of the specified type and its' all hierarchical types, not including
* meta annotations.
*
* @param type the specified type
* @param annotationsToFilter the annotations to filter
* @return non-null read-only {@link List}
*/
static List<Annotation> getAllDeclaredAnnotations(Class<?> type, Predicate<Annotation>... annotationsToFilter) {
if (type == null) {
return emptyList();
}
List<Annotation> allAnnotations = new LinkedList<>();
// All types
Set<Class<?>> allTypes = new LinkedHashSet<>();
// Add current type
allTypes.add(type);
// Add all inherited types
allTypes.addAll(getAllInheritedTypes(type, t -> !Object.class.equals(t)));
for (Class<?> t : allTypes) {
allAnnotations.addAll(getDeclaredAnnotations(t, annotationsToFilter));
}
return unmodifiableList(allAnnotations);
}
/**
* Get the meta-annotated {@link Annotation annotations} directly, excluding {@link Target}, {@link Retention}
* and {@link Documented}
*
* @param annotationType the {@link Annotation annotation} type
* @param metaAnnotationsToFilter the meta annotations to filter
* @return non-null read-only {@link List}
*/
static List<Annotation> getMetaAnnotations(Class<? extends Annotation> annotationType,
Predicate<Annotation>... metaAnnotationsToFilter) {
return getDeclaredAnnotations(annotationType,
// Excludes the Java native annotation types or it causes the stack overflow, e.g,
// @Target annotates itself
excludedType(Target.class),
excludedType(Retention.class),
excludedType(Documented.class),
// Add other predicates
and(metaAnnotationsToFilter)
);
}
/**
* Get all meta annotations from the specified {@link Annotation annotation} type
*
* @param annotationType the {@link Annotation annotation} type
* @param annotationsToFilter the annotations to filter
* @return non-null read-only {@link List}
*/
static List<Annotation> getAllMetaAnnotations(Class<? extends Annotation> annotationType,
Predicate<Annotation>... annotationsToFilter) {
List<Annotation> allMetaAnnotations = new LinkedList<>();
List<Annotation> metaAnnotations = getMetaAnnotations(annotationType);
allMetaAnnotations.addAll(metaAnnotations);
for (Annotation metaAnnotation : metaAnnotations) {
// Get the nested meta annotations recursively
allMetaAnnotations.addAll(getAllMetaAnnotations(metaAnnotation.annotationType()));
}
return unmodifiableList(filterAll(allMetaAnnotations, annotationsToFilter));
}
/**
* Find the annotation that is annotated on the specified element may be a meta-annotation
*
* @param annotatedElement the annotated element
* @param annotationClassName the class name of annotation
* @param <A> the required type of annotation
* @return If found, return first matched-type {@link Annotation annotation}, or <code>null</code>
*/
static <A extends Annotation> A findAnnotation(AnnotatedElement annotatedElement, String annotationClassName) {
return findAnnotation(annotatedElement, resolveAnnotationType(annotatedElement, annotationClassName));
}
/**
* Find the annotation that is annotated on the specified element may be a meta-annotation
*
* @param annotatedElement the annotated element
* @param annotationType the type of annotation
* @param <A> the required type of annotation
* @return If found, return first matched-type {@link Annotation annotation}, or <code>null</code>
*/
static <A extends Annotation> A findAnnotation(AnnotatedElement annotatedElement, Class<A> annotationType) {
return (A) filterFirst(getAllDeclaredAnnotations(annotatedElement), a -> isSameType(a, annotationType));
}
/**
* Find the meta annotations from the the {@link Annotation annotation} type by meta annotation type
*
* @param annotationType the {@link Annotation annotation} type
* @param metaAnnotationType the meta annotation type
* @param <A> the type of required annotation
* @return if found, return all matched results, or get an {@link Collections#emptyList() empty list}
*/
static <A extends Annotation> List<A> findMetaAnnotations(Class<? extends Annotation> annotationType,
Class<A> metaAnnotationType) {
return (List<A>) getAllMetaAnnotations(annotationType, a -> isSameType(a, metaAnnotationType));
}
/**
* Find the meta annotations from the the the annotated element by meta annotation type
*
* @param annotatedElement the annotated element
* @param metaAnnotationType the meta annotation type
* @param <A> the type of required annotation
* @return if found, return all matched results, or get an {@link Collections#emptyList() empty list}
*/
static <A extends Annotation> List<A> findMetaAnnotations(AnnotatedElement annotatedElement,
Class<A> metaAnnotationType) {
List<A> metaAnnotations = new LinkedList<>();
for (Annotation annotation : getAllDeclaredAnnotations(annotatedElement)) {
metaAnnotations.addAll(findMetaAnnotations(annotation.annotationType(), metaAnnotationType));
}
return unmodifiableList(metaAnnotations);
}
/**
* Find the meta annotation from the annotated element by meta annotation type
*
* @param annotatedElement the annotated element
* @param metaAnnotationClassName the class name of meta annotation
* @param <A> the type of required annotation
* @return {@link #findMetaAnnotation(Class, Class)}
*/
static <A extends Annotation> A findMetaAnnotation(AnnotatedElement annotatedElement,
String metaAnnotationClassName) {
return findMetaAnnotation(annotatedElement, resolveAnnotationType(annotatedElement, metaAnnotationClassName));
}
/**
* Find the meta annotation from the annotation type by meta annotation type
*
* @param annotationType the {@link Annotation annotation} type
* @param metaAnnotationType the meta annotation type
* @param <A> the type of required annotation
* @return If found, return the {@link CollectionUtils#first(Collection)} matched result, return <code>null</code>.
* If it requires more result, please consider to use {@link #findMetaAnnotations(Class, Class)}
* @see #findMetaAnnotations(Class, Class)
*/
static <A extends Annotation> A findMetaAnnotation(Class<? extends Annotation> annotationType,
Class<A> metaAnnotationType) {
return first(findMetaAnnotations(annotationType, metaAnnotationType));
}
/**
* Find the meta annotation from the annotated element by meta annotation type
*
* @param annotatedElement the annotated element
* @param metaAnnotationType the meta annotation type
* @param <A> the type of required annotation
* @return If found, return the {@link CollectionUtils#first(Collection)} matched result, return <code>null</code>.
* If it requires more result, please consider to use {@link #findMetaAnnotations(AnnotatedElement, Class)}
* @see #findMetaAnnotations(AnnotatedElement, Class)
*/
static <A extends Annotation> A findMetaAnnotation(AnnotatedElement annotatedElement, Class<A> metaAnnotationType) {
return first(findMetaAnnotations(annotatedElement, metaAnnotationType));
}
/**
* Tests the annotated element is annotated the specified annotations or not
*
* @param type the annotated type
* @param matchAll If <code>true</code>, checking all annotation types are present or not, or match any
* @param annotationTypes the specified annotation types
* @return If the specified annotation types are present, return <code>true</code>, or <code>false</code>
*/
static boolean isAnnotationPresent(Class<?> type,
boolean matchAll,
Class<? extends Annotation>... annotationTypes) {
int size = annotationTypes == null ? 0 : annotationTypes.length;
if (size < 1) {
return false;
}
int presentCount = 0;
for (int i = 0; i < size; i++) {
Class<? extends Annotation> annotationType = annotationTypes[i];
if (findAnnotation(type, annotationType) != null || findMetaAnnotation(type, annotationType) != null) {
presentCount++;
}
}
return matchAll ? presentCount == size : presentCount > 0;
}
/**
* Tests the annotated element is annotated the specified annotation or not
*
* @param type the annotated type
* @param annotationType the class of annotation
* @return If the specified annotation type is present, return <code>true</code>, or <code>false</code>
*/
static boolean isAnnotationPresent(Class<?> type, Class<? extends Annotation> annotationType) {
return isAnnotationPresent(type, true, annotationType);
}
/**
* Tests the annotated element is present any specified annotation types
*
* @param annotatedElement the annotated element
* @param annotationClassName the class name of annotation
* @return If any specified annotation types are present, return <code>true</code>
*/
static boolean isAnnotationPresent(AnnotatedElement annotatedElement, String annotationClassName) {
ClassLoader classLoader = annotatedElement.getClass().getClassLoader();
Class<?> resolvedType = resolveClass(annotationClassName, classLoader);
if (!Annotation.class.isAssignableFrom(resolvedType)) {
return false;
}
return isAnnotationPresent(annotatedElement, (Class<? extends Annotation>) resolvedType);
}
/**
* Tests the annotated element is present any specified annotation types
*
* @param annotatedElement the annotated element
* @param annotationType the class of annotation
* @return If any specified annotation types are present, return <code>true</code>
*/
static boolean isAnnotationPresent(AnnotatedElement annotatedElement, Class<? extends Annotation> annotationType) {
if (isType(annotatedElement)) {
return isAnnotationPresent((Class) annotatedElement, annotationType);
} else {
return annotatedElement.isAnnotationPresent(annotationType) ||
findMetaAnnotation(annotatedElement, annotationType) != null; // to find meta-annotation
}
}
/**
* Tests the annotated element is annotated all specified annotations or not
*
* @param type the annotated type
* @param annotationTypes the specified annotation types
* @return If the specified annotation types are present, return <code>true</code>, or <code>false</code>
*/
static boolean isAllAnnotationPresent(Class<?> type, Class<? extends Annotation>... annotationTypes) {
return isAnnotationPresent(type, true, annotationTypes);
}
/**
* Tests the annotated element is present any specified annotation types
*
* @param type the annotated type
* @param annotationTypes the specified annotation types
* @return If any specified annotation types are present, return <code>true</code>
*/
static boolean isAnyAnnotationPresent(Class<?> type,
Class<? extends Annotation>... annotationTypes) {
return isAnnotationPresent(type, false, annotationTypes);
}
/**
* Get the default value of attribute on the specified annotation
*
* @param annotation {@link Annotation} object
* @param attributeName the name of attribute
* @param <T> the type of value
* @return <code>null</code> if not found
* @since 2.7.9
*/
static <T> T getDefaultValue(Annotation annotation, String attributeName) {
return getDefaultValue(annotation.annotationType(), attributeName);
}
/**
* Get the default value of attribute on the specified annotation
*
* @param annotationType the type of {@link Annotation}
* @param attributeName the name of attribute
* @param <T> the type of value
* @return <code>null</code> if not found
* @since 2.7.9
*/
static <T> T getDefaultValue(Class<? extends Annotation> annotationType, String attributeName) {
Method method = findMethod(annotationType, attributeName);
return (T) (method == null ? null : method.getDefaultValue());
}
}