diff --git a/spring-core/src/main/java/org/springframework/core/annotation/RepeatableContainers.java b/spring-core/src/main/java/org/springframework/core/annotation/RepeatableContainers.java index b8462d8f379..d569d318132 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/RepeatableContainers.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/RepeatableContainers.java @@ -274,9 +274,11 @@ public abstract class RepeatableContainers { Class returnType = method.getReturnType(); if (returnType.isArray()) { Class componentType = returnType.componentType(); - if (Annotation.class.isAssignableFrom(componentType) && - componentType.isAnnotationPresent(Repeatable.class)) { - return method; + if (Annotation.class.isAssignableFrom(componentType)) { + Repeatable repeatable = componentType.getAnnotation(Repeatable.class); + if (repeatable != null && repeatable.value() == annotationType) { + return method; + } } } } diff --git a/spring-core/src/test/java/org/springframework/core/annotation/RepeatableContainersTests.java b/spring-core/src/test/java/org/springframework/core/annotation/RepeatableContainersTests.java index d4c886e4de2..712017f00c8 100644 --- a/spring-core/src/test/java/org/springframework/core/annotation/RepeatableContainersTests.java +++ b/spring-core/src/test/java/org/springframework/core/annotation/RepeatableContainersTests.java @@ -37,6 +37,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException * * @author Phillip Webb * @author Sam Brannen + * @author Chengang Guan */ class RepeatableContainersTests { @@ -79,6 +80,13 @@ class RepeatableContainersTests { assertThat(values).containsExactly("a", "b"); } + @Test + void standardRepeatablesWhenContainerNotReferencedByRepeatableReturnsNull() { + Object[] values = findRepeatedAnnotationValues(RepeatableContainers.standardRepeatables(), + FakeRepeatablesTestCase.class, FakeStandardContainer.class); + assertThat(values).isNull(); + } + } @Nested @@ -211,6 +219,12 @@ class RepeatableContainersTests { StandardRepeatable[] value(); } + @Retention(RetentionPolicy.RUNTIME) + @interface FakeStandardContainer { + + StandardRepeatable[] value(); + } + @Retention(RetentionPolicy.RUNTIME) @Repeatable(StandardContainer.class) @interface StandardRepeatable { @@ -263,4 +277,8 @@ class RepeatableContainersTests { static class ExplicitRepeatablesTestCase { } + @FakeStandardContainer({ @StandardRepeatable("a"), @StandardRepeatable("b") }) + static class FakeRepeatablesTestCase { + } + }