Browse Source

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
pull/26025/head
Sam Brannen 4 years ago
parent
commit
68934f1b79
  1. 17
      spring-test/src/main/java/org/springframework/test/context/TestContextAnnotationUtils.java

17
spring-test/src/main/java/org/springframework/test/context/TestContextAnnotationUtils.java

@ -19,14 +19,15 @@ package org.springframework.test.context; @@ -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 { @@ -146,7 +147,8 @@ public abstract class TestContextAnnotationUtils {
// Present (via @Inherited semantics), directly present, or meta-present?
Set<T> 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 { @@ -545,19 +547,18 @@ public abstract class TestContextAnnotationUtils {
/**
* Find <strong>all</strong> 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<T> 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));
}
/**

Loading…
Cancel
Save