Skip to content

Commit 699d75e

Browse files
committed
Polishing
1 parent c152d24 commit 699d75e

File tree

2 files changed

+38
-29
lines changed

2 files changed

+38
-29
lines changed

spring-beans/src/main/java/org/springframework/beans/annotation/AnnotationBeanUtils.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.lang.annotation.Annotation;
2020
import java.lang.reflect.Method;
2121
import java.util.Arrays;
22+
import java.util.Collections;
2223
import java.util.HashSet;
2324
import java.util.Set;
2425

@@ -62,7 +63,8 @@ public static void copyPropertiesToBean(Annotation ann, Object bean, String... e
6263
public static void copyPropertiesToBean(Annotation ann, Object bean, @Nullable StringValueResolver valueResolver,
6364
String... excludedProperties) {
6465

65-
Set<String> excluded = new HashSet<>(Arrays.asList(excludedProperties));
66+
Set<String> excluded = (excludedProperties.length == 0 ? Collections.emptySet() :
67+
new HashSet<>(Arrays.asList(excludedProperties)));
6668
Method[] annotationProperties = ann.annotationType().getDeclaredMethods();
6769
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(bean);
6870
for (Method annotationProperty : annotationProperties) {

spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -57,14 +57,14 @@
5757
* <p>Note that most of the features of this class are not provided by the
5858
* JDK's introspection facilities themselves.
5959
*
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)}).
6868
*
6969
* <h3>Terminology</h3>
7070
* The terms <em>directly present</em>, <em>indirectly present</em>, and
@@ -449,7 +449,13 @@ public static <A extends Annotation> Set<A> getDeclaredRepeatableAnnotations(Ann
449449
* @since 4.2
450450
*/
451451
@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+
453459
// Do NOT store result in the findAnnotationCache since doing so could break
454460
// findAnnotation(Class, Class) and findAnnotation(Method, Class).
455461
A ann = findAnnotation(annotatedElement, annotationType, new HashSet<>());
@@ -697,7 +703,7 @@ static Annotation[] getDeclaredAnnotations(AnnotatedElement element) {
697703
* @return the first matching annotation, or {@code null} if not found
698704
*/
699705
@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) {
701707
return findAnnotation(clazz, annotationType, true);
702708
}
703709

@@ -793,8 +799,8 @@ private static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A>
793799
* @param annotationType the annotation type to look for
794800
* @param clazz the class to check for the annotation on (may be {@code null})
795801
* @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
798804
* @see Class#isAnnotationPresent(Class)
799805
* @see Class#getDeclaredAnnotations()
800806
* @see #findAnnotationDeclaringClassForTypes(List, Class)
@@ -825,7 +831,7 @@ public static Class<?> findAnnotationDeclaringClass(Class<? extends Annotation>
825831
* one of several candidate {@linkplain Annotation annotations}, so we
826832
* need to handle this explicitly.
827833
* @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})
829835
* @return the first {@link Class} in the inheritance hierarchy that
830836
* declares an annotation of at least one of the specified
831837
* {@code annotationTypes}, or {@code null} if not found
@@ -1044,7 +1050,9 @@ public static Map<String, Object> getAnnotationAttributes(Annotation annotation)
10441050
* corresponding attribute values as values (never {@code null})
10451051
* @see #getAnnotationAttributes(Annotation, boolean, boolean)
10461052
*/
1047-
public static Map<String, Object> getAnnotationAttributes(Annotation annotation, boolean classValuesAsString) {
1053+
public static Map<String, Object> getAnnotationAttributes(
1054+
Annotation annotation, boolean classValuesAsString) {
1055+
10481056
return getAnnotationAttributes(annotation, classValuesAsString, false);
10491057
}
10501058

@@ -1064,8 +1072,8 @@ public static Map<String, Object> getAnnotationAttributes(Annotation annotation,
10641072
* and corresponding attribute values as values (never {@code null})
10651073
* @since 3.1.1
10661074
*/
1067-
public static AnnotationAttributes getAnnotationAttributes(Annotation annotation, boolean classValuesAsString,
1068-
boolean nestedAnnotationsAsMap) {
1075+
public static AnnotationAttributes getAnnotationAttributes(
1076+
Annotation annotation, boolean classValuesAsString, boolean nestedAnnotationsAsMap) {
10691077

10701078
return getAnnotationAttributes(null, annotation, classValuesAsString, nestedAnnotationsAsMap);
10711079
}
@@ -1083,7 +1091,9 @@ public static AnnotationAttributes getAnnotationAttributes(Annotation annotation
10831091
* @since 4.2
10841092
* @see #getAnnotationAttributes(AnnotatedElement, Annotation, boolean, boolean)
10851093
*/
1086-
public static AnnotationAttributes getAnnotationAttributes(@Nullable AnnotatedElement annotatedElement, Annotation annotation) {
1094+
public static AnnotationAttributes getAnnotationAttributes(
1095+
@Nullable AnnotatedElement annotatedElement, Annotation annotation) {
1096+
10871097
return getAnnotationAttributes(annotatedElement, annotation, false, false);
10881098
}
10891099

@@ -1149,8 +1159,8 @@ private static AnnotationAttributes getAnnotationAttributes(@Nullable Object ann
11491159
* @since 4.2
11501160
* @see #postProcessAnnotationAttributes
11511161
*/
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) {
11541164

11551165
Class<? extends Annotation> annotationType = annotation.annotationType();
11561166
AnnotationAttributes attributes = new AnnotationAttributes(annotationType);
@@ -1432,8 +1442,8 @@ public static Object getValue(@Nullable Annotation annotation, @Nullable String
14321442
}
14331443
catch (InvocationTargetException ex) {
14341444
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);
14371447
}
14381448
catch (Throwable ex) {
14391449
handleIntrospectionFailure(annotation.getClass(), ex);
@@ -1462,10 +1472,7 @@ public static Object getDefaultValue(Annotation annotation) {
14621472
*/
14631473
@Nullable
14641474
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);
14691476
}
14701477

14711478
/**
@@ -2042,7 +2049,7 @@ private static class AnnotationCollector<A extends Annotation> {
20422049

20432050
private final Set<A> result = new LinkedHashSet<>();
20442051

2045-
AnnotationCollector(Class<A> annotationType,@Nullable Class<? extends Annotation> containerAnnotationType) {
2052+
AnnotationCollector(Class<A> annotationType, @Nullable Class<? extends Annotation> containerAnnotationType) {
20462053
this.annotationType = annotationType;
20472054
this.containerAnnotationType = (containerAnnotationType != null ? containerAnnotationType :
20482055
resolveContainerAnnotationType(annotationType));

0 commit comments

Comments
 (0)