From b841e8560c60bd3dae2127b1008177aad06176fd Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Wed, 29 Jul 2020 14:01:29 +0200 Subject: [PATCH] Polish MergedAnnotation API internals --- .../annotation/AnnotationTypeMappings.java | 26 +++++++------------ .../core/annotation/MergedAnnotations.java | 6 ++--- .../AnnotationTypeMappingsTests.java | 8 +++--- 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationTypeMappings.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationTypeMappings.java index e60231ed6dab..7032c736050d 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationTypeMappings.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationTypeMappings.java @@ -30,7 +30,7 @@ * Provides {@link AnnotationTypeMapping} information for a single source * annotation type. Performs a recursive breadth first crawl of all * meta-annotations to ultimately provide a quick way to map the attributes of - * root {@link Annotation}. + * a root {@link Annotation}. * *

Supports convention based merging of meta-annotations as well as implicit * and explicit {@link AliasFor @AliasFor} aliases. Also provides information @@ -81,14 +81,12 @@ private void addAllMappings(Class annotationType) { } private void addMetaAnnotationsToQueue(Deque queue, AnnotationTypeMapping source) { - Annotation[] metaAnnotations = - AnnotationsScanner.getDeclaredAnnotations(source.getAnnotationType(), false); + Annotation[] metaAnnotations = AnnotationsScanner.getDeclaredAnnotations(source.getAnnotationType(), false); for (Annotation metaAnnotation : metaAnnotations) { if (!isMappable(source, metaAnnotation)) { continue; } - Annotation[] repeatedAnnotations = this.repeatableContainers - .findRepeatedAnnotations(metaAnnotation); + Annotation[] repeatedAnnotations = this.repeatableContainers.findRepeatedAnnotations(metaAnnotation); if (repeatedAnnotations != null) { for (Annotation repeatedAnnotation : repeatedAnnotations) { if (!isMappable(source, metaAnnotation)) { @@ -103,9 +101,7 @@ private void addMetaAnnotationsToQueue(Deque queue, Annot } } - private void addIfPossible(Deque queue, - AnnotationTypeMapping source, Annotation ann) { - + private void addIfPossible(Deque queue, AnnotationTypeMapping source, Annotation ann) { addIfPossible(queue, source, ann.annotationType(), ann); } @@ -183,21 +179,20 @@ static AnnotationTypeMappings forAnnotationType(Class anno static AnnotationTypeMappings forAnnotationType( Class annotationType, AnnotationFilter annotationFilter) { - return forAnnotationType(annotationType, - RepeatableContainers.standardRepeatables(), annotationFilter); + return forAnnotationType(annotationType, RepeatableContainers.standardRepeatables(), annotationFilter); } /** * Create {@link AnnotationTypeMappings} for the specified annotation type. * @param annotationType the source annotation type + * @param repeatableContainers the repeatable containers that may be used by + * the meta-annotations * @param annotationFilter the annotation filter used to limit which * annotations are considered * @return type mappings for the annotation type */ - static AnnotationTypeMappings forAnnotationType( - Class annotationType, - RepeatableContainers repeatableContainers, - AnnotationFilter annotationFilter) { + static AnnotationTypeMappings forAnnotationType(Class annotationType, + RepeatableContainers repeatableContainers, AnnotationFilter annotationFilter) { if (repeatableContainers == RepeatableContainers.standardRepeatables()) { return standardRepeatablesCache.computeIfAbsent(annotationFilter, @@ -207,8 +202,7 @@ static AnnotationTypeMappings forAnnotationType( return noRepeatablesCache.computeIfAbsent(annotationFilter, key -> new Cache(repeatableContainers, key)).get(annotationType); } - return new AnnotationTypeMappings(repeatableContainers, annotationFilter, - annotationType); + return new AnnotationTypeMappings(repeatableContainers, annotationFilter, annotationType); } static void clearCache() { diff --git a/spring-core/src/main/java/org/springframework/core/annotation/MergedAnnotations.java b/spring-core/src/main/java/org/springframework/core/annotation/MergedAnnotations.java index 32055b2a8157..97ce25598a4e 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/MergedAnnotations.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/MergedAnnotations.java @@ -326,7 +326,7 @@ static MergedAnnotations from(AnnotatedElement element, SearchStrategy searchStr static MergedAnnotations from(AnnotatedElement element, SearchStrategy searchStrategy, RepeatableContainers repeatableContainers) { - return TypeMappedAnnotations.from(element, searchStrategy, repeatableContainers, AnnotationFilter.PLAIN); + return from(element, searchStrategy, repeatableContainers, AnnotationFilter.PLAIN); } /** @@ -340,7 +340,7 @@ static MergedAnnotations from(AnnotatedElement element, SearchStrategy searchStr * @param annotationFilter an annotation filter used to restrict the * annotations considered * @return a {@link MergedAnnotations} instance containing the merged - * element annotations + * annotations for the supplied element */ static MergedAnnotations from(AnnotatedElement element, SearchStrategy searchStrategy, RepeatableContainers repeatableContainers, AnnotationFilter annotationFilter) { @@ -386,7 +386,7 @@ static MergedAnnotations from(Object source, Annotation... annotations) { * @return a {@link MergedAnnotations} instance containing the annotations */ static MergedAnnotations from(Object source, Annotation[] annotations, RepeatableContainers repeatableContainers) { - return TypeMappedAnnotations.from(source, annotations, repeatableContainers, AnnotationFilter.PLAIN); + return from(source, annotations, repeatableContainers, AnnotationFilter.PLAIN); } /** diff --git a/spring-core/src/test/java/org/springframework/core/annotation/AnnotationTypeMappingsTests.java b/spring-core/src/test/java/org/springframework/core/annotation/AnnotationTypeMappingsTests.java index 950054ef4d5a..571373234bce 100644 --- a/spring-core/src/test/java/org/springframework/core/annotation/AnnotationTypeMappingsTests.java +++ b/spring-core/src/test/java/org/springframework/core/annotation/AnnotationTypeMappingsTests.java @@ -28,6 +28,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.stream.IntStream; import javax.annotation.Nullable; @@ -38,6 +39,7 @@ import org.springframework.lang.UsesSunMisc; import org.springframework.util.ReflectionUtils; +import static java.util.stream.Collectors.toList; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; @@ -500,11 +502,7 @@ private AnnotationTypeMapping getMapping(AnnotationTypeMappings mappings, private List getAll(AnnotationTypeMappings mappings) { // AnnotationTypeMappings does not implement Iterable so we don't create // too many garbage Iterators - List result = new ArrayList<>(mappings.size()); - for (int i = 0; i < mappings.size(); i++) { - result.add(mappings.get(i)); - } - return result; + return IntStream.range(0, mappings.size()).mapToObj(mappings::get).collect(toList()); } private List getNames(MirrorSet mirrorSet) {