1
1
/*
2
- * Copyright 2002-2013 the original author or authors.
2
+ * Copyright 2002-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
25
25
26
26
import org .springframework .core .BridgeMethodResolver ;
27
27
import org .springframework .util .Assert ;
28
+ import org .springframework .util .ReflectionUtils ;
28
29
29
30
/**
30
31
* General utility methods for working with annotations, handling bridge methods (which the compiler
@@ -60,8 +61,8 @@ public abstract class AnnotationUtils {
60
61
* Method, Constructor or Field. Meta-annotations will be searched if the annotation
61
62
* is not declared locally on the supplied element.
62
63
* @param ae the Method, Constructor or Field from which to get the annotation
63
- * @param annotationType the annotation class to look for, both locally and as a meta-annotation
64
- * @return the matching annotation or {@code null} if not found
64
+ * @param annotationType the annotation type to look for, both locally and as a meta-annotation
65
+ * @return the matching annotation, or {@code null} if none found
65
66
* @since 3.1
66
67
*/
67
68
public static <T extends Annotation > T getAnnotation (AnnotatedElement ae , Class <T > annotationType ) {
@@ -92,7 +93,7 @@ public static Annotation[] getAnnotations(Method method) {
92
93
* Get a single {@link Annotation} of {@code annotationType} from the supplied {@link Method}.
93
94
* <p>Correctly handles bridge {@link Method Methods} generated by the compiler.
94
95
* @param method the method to look for annotations on
95
- * @param annotationType the annotation class to look for
96
+ * @param annotationType the annotation type to look for
96
97
* @return the annotations found
97
98
* @see org.springframework.core.BridgeMethodResolver#findBridgedMethod(Method)
98
99
*/
@@ -115,8 +116,8 @@ public static <A extends Annotation> A getAnnotation(Method method, Class<A> ann
115
116
* traversing its super methods if no annotation can be found on the given method itself.
116
117
* <p>Annotations on methods are not inherited by default, so we need to handle this explicitly.
117
118
* @param method the method to look for annotations on
118
- * @param annotationType the annotation class to look for
119
- * @return the annotation found, or {@code null} if none found
119
+ * @param annotationType the annotation type to look for
120
+ * @return the annotation found, or {@code null} if none
120
121
*/
121
122
public static <A extends Annotation > A findAnnotation (Method method , Class <A > annotationType ) {
122
123
A annotation = getAnnotation (method , annotationType );
@@ -192,7 +193,7 @@ private static boolean isInterfaceWithAnnotatedMethods(Class<?> iface) {
192
193
* with the interfaces that the superclass declares. Recursing up through the entire superclass
193
194
* hierarchy if no match is found.
194
195
* @param clazz the class to look for annotations on
195
- * @param annotationType the annotation class to look for
196
+ * @param annotationType the annotation type to look for
196
197
* @return the annotation found, or {@code null} if none found
197
198
*/
198
199
public static <A extends Annotation > A findAnnotation (Class <?> clazz , Class <A > annotationType ) {
@@ -215,11 +216,11 @@ public static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> a
215
216
}
216
217
}
217
218
}
218
- Class <?> superClass = clazz .getSuperclass ();
219
- if (superClass == null || superClass .equals (Object .class )) {
219
+ Class <?> superclass = clazz .getSuperclass ();
220
+ if (superclass == null || superclass .equals (Object .class )) {
220
221
return null ;
221
222
}
222
- return findAnnotation (superClass , annotationType );
223
+ return findAnnotation (superclass , annotationType );
223
224
}
224
225
225
226
/**
@@ -232,9 +233,8 @@ public static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> a
232
233
* <p>The standard {@link Class} API does not provide a mechanism for determining which class
233
234
* in an inheritance hierarchy actually declares an {@link Annotation}, so we need to handle
234
235
* this explicitly.
235
- * @param annotationType the Class object corresponding to the annotation type
236
- * @param clazz the Class object corresponding to the class on which to check for the annotation,
237
- * or {@code null}
236
+ * @param annotationType the annotation type to look for, both locally and as a meta-annotation
237
+ * @param clazz the class on which to check for the annotation (may be {@code null})
238
238
* @return the first {@link Class} in the inheritance hierarchy of the specified {@code clazz}
239
239
* which declares an annotation for the specified {@code annotationType}, or {@code null}
240
240
* if not found
@@ -248,8 +248,10 @@ public static Class<?> findAnnotationDeclaringClass(Class<? extends Annotation>
248
248
if (clazz == null || clazz .equals (Object .class )) {
249
249
return null ;
250
250
}
251
- return (isAnnotationDeclaredLocally (annotationType , clazz )) ? clazz : findAnnotationDeclaringClass (
252
- annotationType , clazz .getSuperclass ());
251
+ if (isAnnotationDeclaredLocally (annotationType , clazz )) {
252
+ return clazz ;
253
+ }
254
+ return findAnnotationDeclaringClass (annotationType , clazz .getSuperclass ());
253
255
}
254
256
255
257
/**
@@ -413,14 +415,13 @@ else if (value instanceof Class[]) {
413
415
}
414
416
if (nestedAnnotationsAsMap && value instanceof Annotation ) {
415
417
attrs .put (method .getName (),
416
- getAnnotationAttributes ((Annotation ) value , classValuesAsString , nestedAnnotationsAsMap ));
418
+ getAnnotationAttributes ((Annotation ) value , classValuesAsString , true ));
417
419
}
418
420
else if (nestedAnnotationsAsMap && value instanceof Annotation []) {
419
421
Annotation [] realAnnotations = (Annotation []) value ;
420
422
AnnotationAttributes [] mappedAnnotations = new AnnotationAttributes [realAnnotations .length ];
421
423
for (int i = 0 ; i < realAnnotations .length ; i ++) {
422
- mappedAnnotations [i ] = getAnnotationAttributes (realAnnotations [i ], classValuesAsString ,
423
- nestedAnnotationsAsMap );
424
+ mappedAnnotations [i ] = getAnnotationAttributes (realAnnotations [i ], classValuesAsString , true );
424
425
}
425
426
attrs .put (method .getName (), mappedAnnotations );
426
427
}
@@ -456,7 +457,8 @@ public static Object getValue(Annotation annotation) {
456
457
*/
457
458
public static Object getValue (Annotation annotation , String attributeName ) {
458
459
try {
459
- Method method = annotation .annotationType ().getDeclaredMethod (attributeName , new Class [0 ]);
460
+ Method method = annotation .annotationType ().getDeclaredMethod (attributeName );
461
+ ReflectionUtils .makeAccessible (method );
460
462
return method .invoke (annotation );
461
463
}
462
464
catch (Exception ex ) {
@@ -506,8 +508,7 @@ public static Object getDefaultValue(Class<? extends Annotation> annotationType)
506
508
*/
507
509
public static Object getDefaultValue (Class <? extends Annotation > annotationType , String attributeName ) {
508
510
try {
509
- Method method = annotationType .getDeclaredMethod (attributeName , new Class [0 ]);
510
- return method .getDefaultValue ();
511
+ return annotationType .getDeclaredMethod (attributeName ).getDefaultValue ();
511
512
}
512
513
catch (Exception ex ) {
513
514
return null ;
0 commit comments