1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2019 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.
57
57
* <p>Note that most of the features of this class are not provided by the
58
58
* JDK's introspection facilities themselves.
59
59
*
60
- * <p>As a general rule for runtime-retained annotations (e.g. for transaction
61
- * control, authorization, or service exposure), always use the lookup methods
62
- * on this class (e.g., {@link #findAnnotation(Method, Class)},
63
- * {@link #getAnnotation(Method, Class)}, and {@link #getAnnotations(Method)})
64
- * instead of the plain annotation lookup methods in the JDK. You can still
65
- * explicitly choose between a <em>get</em> lookup on the given class level only
66
- * ({@link #getAnnotation(Method, Class)}) and a <em>find</em> lookup in the entire
67
- * inheritance hierarchy of the given method ({@link #findAnnotation(Method, Class)}).
60
+ * <p>As a general rule for runtime-retained application annotations (e.g. for
61
+ * transaction control, authorization, or service exposure), always use the
62
+ * lookup methods on this class (e.g. {@link #findAnnotation(Method, Class)} or
63
+ * {@link #getAnnotation(Method, Class)}) instead of the plain annotation lookup
64
+ * methods in the JDK. You can still explicitly choose between a <em>get</em>
65
+ * lookup on the given class level only ({@link #getAnnotation(Method, Class)})
66
+ * and a <em>find</em> lookup in the entire inheritance hierarchy of the given
67
+ * method ({@link #findAnnotation(Method, Class)}).
68
68
*
69
69
* <h3>Terminology</h3>
70
70
* The terms <em>directly present</em>, <em>indirectly present</em>, and
@@ -449,7 +449,13 @@ public static <A extends Annotation> Set<A> getDeclaredRepeatableAnnotations(Ann
449
449
* @since 4.2
450
450
*/
451
451
@ Nullable
452
- public static <A extends Annotation > A findAnnotation (AnnotatedElement annotatedElement , Class <A > annotationType ) {
452
+ public static <A extends Annotation > A findAnnotation (
453
+ AnnotatedElement annotatedElement , @ Nullable Class <A > annotationType ) {
454
+
455
+ if (annotationType == null ) {
456
+ return null ;
457
+ }
458
+
453
459
// Do NOT store result in the findAnnotationCache since doing so could break
454
460
// findAnnotation(Class, Class) and findAnnotation(Method, Class).
455
461
A ann = findAnnotation (annotatedElement , annotationType , new HashSet <>());
@@ -697,7 +703,7 @@ static Annotation[] getDeclaredAnnotations(AnnotatedElement element) {
697
703
* @return the first matching annotation, or {@code null} if not found
698
704
*/
699
705
@ Nullable
700
- public static <A extends Annotation > A findAnnotation (Class <?> clazz , Class <A > annotationType ) {
706
+ public static <A extends Annotation > A findAnnotation (Class <?> clazz , @ Nullable Class <A > annotationType ) {
701
707
return findAnnotation (clazz , annotationType , true );
702
708
}
703
709
@@ -793,8 +799,8 @@ private static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A>
793
799
* @param annotationType the annotation type to look for
794
800
* @param clazz the class to check for the annotation on (may be {@code null})
795
801
* @return the first {@link Class} in the inheritance hierarchy that
796
- * declares an annotation of the specified {@code annotationType}, or
797
- * {@code null} if not found
802
+ * declares an annotation of the specified {@code annotationType},
803
+ * or {@code null} if not found
798
804
* @see Class#isAnnotationPresent(Class)
799
805
* @see Class#getDeclaredAnnotations()
800
806
* @see #findAnnotationDeclaringClassForTypes(List, Class)
@@ -825,7 +831,7 @@ public static Class<?> findAnnotationDeclaringClass(Class<? extends Annotation>
825
831
* one of several candidate {@linkplain Annotation annotations}, so we
826
832
* need to handle this explicitly.
827
833
* @param annotationTypes the annotation types to look for
828
- * @param clazz the class to check for the annotations on, or {@code null}
834
+ * @param clazz the class to check for the annotation on (may be {@code null})
829
835
* @return the first {@link Class} in the inheritance hierarchy that
830
836
* declares an annotation of at least one of the specified
831
837
* {@code annotationTypes}, or {@code null} if not found
@@ -1044,7 +1050,9 @@ public static Map<String, Object> getAnnotationAttributes(Annotation annotation)
1044
1050
* corresponding attribute values as values (never {@code null})
1045
1051
* @see #getAnnotationAttributes(Annotation, boolean, boolean)
1046
1052
*/
1047
- public static Map <String , Object > getAnnotationAttributes (Annotation annotation , boolean classValuesAsString ) {
1053
+ public static Map <String , Object > getAnnotationAttributes (
1054
+ Annotation annotation , boolean classValuesAsString ) {
1055
+
1048
1056
return getAnnotationAttributes (annotation , classValuesAsString , false );
1049
1057
}
1050
1058
@@ -1064,8 +1072,8 @@ public static Map<String, Object> getAnnotationAttributes(Annotation annotation,
1064
1072
* and corresponding attribute values as values (never {@code null})
1065
1073
* @since 3.1.1
1066
1074
*/
1067
- public static AnnotationAttributes getAnnotationAttributes (Annotation annotation , boolean classValuesAsString ,
1068
- boolean nestedAnnotationsAsMap ) {
1075
+ public static AnnotationAttributes getAnnotationAttributes (
1076
+ Annotation annotation , boolean classValuesAsString , boolean nestedAnnotationsAsMap ) {
1069
1077
1070
1078
return getAnnotationAttributes (null , annotation , classValuesAsString , nestedAnnotationsAsMap );
1071
1079
}
@@ -1083,7 +1091,9 @@ public static AnnotationAttributes getAnnotationAttributes(Annotation annotation
1083
1091
* @since 4.2
1084
1092
* @see #getAnnotationAttributes(AnnotatedElement, Annotation, boolean, boolean)
1085
1093
*/
1086
- public static AnnotationAttributes getAnnotationAttributes (@ Nullable AnnotatedElement annotatedElement , Annotation annotation ) {
1094
+ public static AnnotationAttributes getAnnotationAttributes (
1095
+ @ Nullable AnnotatedElement annotatedElement , Annotation annotation ) {
1096
+
1087
1097
return getAnnotationAttributes (annotatedElement , annotation , false , false );
1088
1098
}
1089
1099
@@ -1149,8 +1159,8 @@ private static AnnotationAttributes getAnnotationAttributes(@Nullable Object ann
1149
1159
* @since 4.2
1150
1160
* @see #postProcessAnnotationAttributes
1151
1161
*/
1152
- static AnnotationAttributes retrieveAnnotationAttributes (@ Nullable Object annotatedElement , Annotation annotation ,
1153
- boolean classValuesAsString , boolean nestedAnnotationsAsMap ) {
1162
+ static AnnotationAttributes retrieveAnnotationAttributes (@ Nullable Object annotatedElement ,
1163
+ Annotation annotation , boolean classValuesAsString , boolean nestedAnnotationsAsMap ) {
1154
1164
1155
1165
Class <? extends Annotation > annotationType = annotation .annotationType ();
1156
1166
AnnotationAttributes attributes = new AnnotationAttributes (annotationType );
@@ -1432,8 +1442,8 @@ public static Object getValue(@Nullable Annotation annotation, @Nullable String
1432
1442
}
1433
1443
catch (InvocationTargetException ex ) {
1434
1444
rethrowAnnotationConfigurationException (ex .getTargetException ());
1435
- throw new IllegalStateException (
1436
- "Could not obtain value for annotation attribute '" + attributeName + "' in " + annotation , ex );
1445
+ throw new IllegalStateException ("Could not obtain value for annotation attribute '" +
1446
+ attributeName + "' in " + annotation , ex );
1437
1447
}
1438
1448
catch (Throwable ex ) {
1439
1449
handleIntrospectionFailure (annotation .getClass (), ex );
@@ -1462,10 +1472,7 @@ public static Object getDefaultValue(Annotation annotation) {
1462
1472
*/
1463
1473
@ Nullable
1464
1474
public static Object getDefaultValue (@ Nullable Annotation annotation , @ Nullable String attributeName ) {
1465
- if (annotation == null ) {
1466
- return null ;
1467
- }
1468
- return getDefaultValue (annotation .annotationType (), attributeName );
1475
+ return (annotation != null ? getDefaultValue (annotation .annotationType (), attributeName ) : null );
1469
1476
}
1470
1477
1471
1478
/**
@@ -2042,7 +2049,7 @@ private static class AnnotationCollector<A extends Annotation> {
2042
2049
2043
2050
private final Set <A > result = new LinkedHashSet <>();
2044
2051
2045
- AnnotationCollector (Class <A > annotationType ,@ Nullable Class <? extends Annotation > containerAnnotationType ) {
2052
+ AnnotationCollector (Class <A > annotationType , @ Nullable Class <? extends Annotation > containerAnnotationType ) {
2046
2053
this .annotationType = annotationType ;
2047
2054
this .containerAnnotationType = (containerAnnotationType != null ? containerAnnotationType :
2048
2055
resolveContainerAnnotationType (annotationType ));
0 commit comments