Skip to content

Commit 7dbf42e

Browse files
committed
Polish contribution
See gh-25483
1 parent 83a9583 commit 7dbf42e

File tree

2 files changed

+51
-19
lines changed

2 files changed

+51
-19
lines changed

spring-core/src/test/java/org/springframework/core/annotation/AnnotationTypeMappingsTests.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import org.springframework.core.annotation.AnnotationTypeMapping.MirrorSets;
3838
import org.springframework.core.annotation.AnnotationTypeMapping.MirrorSets.MirrorSet;
3939
import org.springframework.lang.UsesSunMisc;
40-
import org.springframework.util.ObjectUtils;
4140
import org.springframework.util.ReflectionUtils;
4241

4342
import static java.util.stream.Collectors.toList;
@@ -86,6 +85,14 @@ void forAnnotationTypeWhenHasRepeatingMetaAnnotationReturnsMapping() {
8685
WithRepeatedMetaAnnotations.class, Repeating.class, Repeating.class);
8786
}
8887

88+
@Test
89+
void forAnnotationTypeWhenRepeatableMetaAnnotationIsFiltered() {
90+
AnnotationTypeMappings mappings = AnnotationTypeMappings.forAnnotationType(WithRepeatedMetaAnnotations.class,
91+
Repeating.class.getName()::equals);
92+
assertThat(getAll(mappings)).flatExtracting(AnnotationTypeMapping::getAnnotationType)
93+
.containsExactly(WithRepeatedMetaAnnotations.class);
94+
}
95+
8996
@Test
9097
void forAnnotationTypeWhenSelfAnnotatedReturnsMapping() {
9198
AnnotationTypeMappings mappings = AnnotationTypeMappings.forAnnotationType(SelfAnnotated.class);
@@ -477,16 +484,6 @@ private Method[] resolveMirrorSets(AnnotationTypeMapping mapping, Class<?> eleme
477484
return result;
478485
}
479486

480-
@Test
481-
void forAnnotationTypeWhenRepeatableMetaAnnotationFilterd() {
482-
AnnotationTypeMappings mappings = AnnotationTypeMappings.forAnnotationType(WithRepeatedMetaAnnotations.class,
483-
annotationType ->
484-
ObjectUtils.nullSafeEquals(annotationType, Repeating.class.getName()));
485-
assertThat(getAll(mappings)).flatExtracting(
486-
AnnotationTypeMapping::getAnnotationType).containsExactly(
487-
WithRepeatedMetaAnnotations.class);
488-
}
489-
490487
@Nullable
491488
private Method getAliasMapping(AnnotationTypeMapping mapping, int attributeIndex) {
492489
int mapped = mapping.getAliasMapping(attributeIndex);

spring-core/src/test/java/org/springframework/core/annotation/MergedAnnotationsRepeatableAnnotationTests.java

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -25,6 +25,7 @@
2525
import java.lang.annotation.Target;
2626
import java.lang.reflect.AnnotatedElement;
2727
import java.util.Set;
28+
import java.util.stream.Stream;
2829

2930
import org.assertj.core.api.ThrowableTypeAssert;
3031
import org.junit.jupiter.api.Test;
@@ -222,14 +223,35 @@ void typeHierarchyAnnotationsWhenNoninheritedComposedRepeatableOnSuperclassRetur
222223
"B", "C");
223224
}
224225

225-
private <A extends Annotation> Set<A> getAnnotations(
226-
Class<? extends Annotation> container, Class<A> repeatable,
227-
SearchStrategy searchStrategy, AnnotatedElement element) {
226+
@Test
227+
void typeHierarchyAnnotationsWithLocalComposedAnnotationWhoseRepeatableMetaAnnotationsAreFiltered() {
228+
Class<WithRepeatedMetaAnnotationsClass> element = WithRepeatedMetaAnnotationsClass.class;
229+
SearchStrategy searchStrategy = SearchStrategy.TYPE_HIERARCHY;
230+
AnnotationFilter annotationFilter = PeteRepeat.class.getName()::equals;
231+
232+
Set<PeteRepeat> annotations = getAnnotations(null, PeteRepeat.class, searchStrategy, element, annotationFilter);
233+
assertThat(annotations).isEmpty();
234+
235+
MergedAnnotations mergedAnnotations = MergedAnnotations.from(element, searchStrategy,
236+
RepeatableContainers.standardRepeatables(), annotationFilter);
237+
Stream<Class<? extends Annotation>> annotationTypes = mergedAnnotations.stream()
238+
.map(MergedAnnotation::synthesize)
239+
.map(Annotation::annotationType);
240+
assertThat(annotationTypes).containsExactly(WithRepeatedMetaAnnotations.class, Noninherited.class, Noninherited.class);
241+
}
242+
243+
private <A extends Annotation> Set<A> getAnnotations(Class<? extends Annotation> container,
244+
Class<A> repeatable, SearchStrategy searchStrategy, AnnotatedElement element) {
245+
246+
return getAnnotations(container, repeatable, searchStrategy, element, AnnotationFilter.PLAIN);
247+
}
248+
249+
private <A extends Annotation> Set<A> getAnnotations(Class<? extends Annotation> container,
250+
Class<A> repeatable, SearchStrategy searchStrategy, AnnotatedElement element, AnnotationFilter annotationFilter) {
251+
228252
RepeatableContainers containers = RepeatableContainers.of(repeatable, container);
229-
MergedAnnotations annotations = MergedAnnotations.from(element,
230-
searchStrategy, containers, AnnotationFilter.PLAIN);
231-
return annotations.stream(repeatable).collect(
232-
MergedAnnotationCollectors.toAnnotationSet());
253+
MergedAnnotations annotations = MergedAnnotations.from(element, searchStrategy, containers, annotationFilter);
254+
return annotations.stream(repeatable).collect(MergedAnnotationCollectors.toAnnotationSet());
233255
}
234256

235257
private void nonRepeatableRequirements(Exception ex) {
@@ -414,4 +436,17 @@ static class SubNoninheritedRepeatableClass extends NoninheritedRepeatableClass
414436

415437
}
416438

439+
@Retention(RetentionPolicy.RUNTIME)
440+
@PeteRepeat("A")
441+
@PeteRepeat("B")
442+
@interface WithRepeatedMetaAnnotations {
443+
}
444+
445+
@WithRepeatedMetaAnnotations
446+
@PeteRepeat("C")
447+
@Noninherited("X")
448+
@Noninherited("Y")
449+
static class WithRepeatedMetaAnnotationsClass {
450+
}
451+
417452
}

0 commit comments

Comments
 (0)