From 68934f1b793ab8c6f01cfb091815374be2d95180 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Tue, 3 Nov 2020 15:59:03 +0100 Subject: [PATCH] Use TYPE_HIERARCHY strategy in AnnoDescr.findAllLocalMergedAnnotations() Prior to this commit, the findAllLocalMergedAnnotations() method in AnnotationDescriptor altered between the use of TYPE_HIERARCHY and TYPE_HIERARCHY_AND_ENCLOSING_CLASSES for the SearchStrategy, depending on @NestedTestConfiguration semantics; however, when searching for "local" annotations, there is no need to search the enclosing class hierarchy since AnnotationDescriptor#next() handles that use case. This commit therefore switches to using only the TYPE_HIERARCHY strategy. This commit also discontinues the use of MergedAnnotationCollectors.toAnnotationSet() in order to avoid the unnecessary creation of a temporary List when collecting synthesized annotations in a LinkedHashSet. Closes gh-25985 --- .../context/TestContextAnnotationUtils.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/TestContextAnnotationUtils.java b/spring-test/src/main/java/org/springframework/test/context/TestContextAnnotationUtils.java index fa7a2e63e0f..be74ddf7210 100644 --- a/spring-test/src/main/java/org/springframework/test/context/TestContextAnnotationUtils.java +++ b/spring-test/src/main/java/org/springframework/test/context/TestContextAnnotationUtils.java @@ -19,14 +19,15 @@ package org.springframework.test.context; import java.lang.annotation.Annotation; import java.util.Collections; import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.Set; import java.util.function.Predicate; +import java.util.stream.Collectors; import org.springframework.core.SpringProperties; import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.annotation.MergedAnnotation; -import org.springframework.core.annotation.MergedAnnotationCollectors; import org.springframework.core.annotation.MergedAnnotationPredicates; import org.springframework.core.annotation.MergedAnnotations; import org.springframework.core.annotation.MergedAnnotations.SearchStrategy; @@ -146,7 +147,8 @@ public abstract class TestContextAnnotationUtils { // Present (via @Inherited semantics), directly present, or meta-present? Set mergedAnnotations = MergedAnnotations.from(clazz, SearchStrategy.INHERITED_ANNOTATIONS) .stream(annotationType) - .collect(MergedAnnotationCollectors.toAnnotationSet()); + .map(MergedAnnotation::synthesize) + .collect(Collectors.toCollection(LinkedHashSet::new)); if (!mergedAnnotations.isEmpty()) { return mergedAnnotations; @@ -545,19 +547,18 @@ public abstract class TestContextAnnotationUtils { /** * Find all annotations of the specified annotation type * that are present or meta-present on the {@linkplain #getRootDeclaringClass() - * root declaring class} of this descriptor. + * root declaring class} of this descriptor or on any interfaces that the + * root declaring class implements. * @return the set of all merged, synthesized {@code Annotations} found, * or an empty set if none were found */ public Set findAllLocalMergedAnnotations() { - SearchStrategy searchStrategy = - (getEnclosingConfiguration(getRootDeclaringClass()) == EnclosingConfiguration.INHERIT ? - SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES : - SearchStrategy.TYPE_HIERARCHY); + SearchStrategy searchStrategy = SearchStrategy.TYPE_HIERARCHY; return MergedAnnotations.from(getRootDeclaringClass(), searchStrategy, RepeatableContainers.none()) .stream(getAnnotationType()) .filter(MergedAnnotationPredicates.firstRunOf(MergedAnnotation::getAggregateIndex)) - .collect(MergedAnnotationCollectors.toAnnotationSet()); + .map(MergedAnnotation::synthesize) + .collect(Collectors.toCollection(LinkedHashSet::new)); } /**