Skip to content

Commit e20b47c

Browse files
committed
Ensure local aliases don't override meta-annotation
This commit introduces an additional test case to ensure that explicit local attribute aliases (configured via @AliasFor) do not accidentally override attributes of the same names in meta-annotations (i.e., by convention). Issue: SPR-13325
1 parent 4317e76 commit e20b47c

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,18 @@ public void findMergedAnnotationAttributesOnClassWithAttributeAliasInComposedAnn
530530
assertEquals(asList("*Test", "*Tests"), patterns);
531531
}
532532

533+
@Test
534+
public void findMergedAnnotationWithLocalAliasesThatConflictWithAttributesInMetaAnnotationByConvention() {
535+
final String[] EMPTY = new String[] {};
536+
Class<?> element = SpringAppConfigClass.class;
537+
ContextConfig contextConfig = findMergedAnnotation(element, ContextConfig.class);
538+
assertNotNull("Should find @ContextConfig on " + element, contextConfig);
539+
assertArrayEquals("locations for " + element, EMPTY, contextConfig.locations());
540+
// 'value' in @SpringAppConfig should not override 'value' in @ContextConfig
541+
assertArrayEquals("value for " + element, EMPTY, contextConfig.value());
542+
assertArrayEquals("classes for " + element, new Class<?>[] { Number.class }, contextConfig.classes());
543+
}
544+
533545
private Set<String> names(Class<?>... classes) {
534546
return stream(classes).map(Class::getName).collect(toSet());
535547
}
@@ -670,6 +682,8 @@ static class MetaCycleAnnotatedClass {
670682

671683
@AliasFor(attribute = "value")
672684
String[] locations() default {};
685+
686+
Class<?>[] classes() default {};
673687
}
674688

675689
@ContextConfig
@@ -725,6 +739,23 @@ static class MetaCycleAnnotatedClass {
725739
String[] xmlConfigFiles() default "default.xml";
726740
}
727741

742+
/**
743+
* Mock of {@code org.springframework.boot.test.SpringApplicationConfiguration}.
744+
*/
745+
@ContextConfig
746+
@Retention(RetentionPolicy.RUNTIME)
747+
@interface SpringAppConfig {
748+
749+
@AliasFor(annotation = ContextConfig.class, attribute = "locations")
750+
String[] locations() default {};
751+
752+
@AliasFor("value")
753+
Class<?>[] classes() default {};
754+
755+
@AliasFor("classes")
756+
Class<?>[] value() default {};
757+
}
758+
728759
/**
729760
* Mock of {@code org.springframework.context.annotation.ComponentScan}
730761
*/
@@ -909,4 +940,8 @@ static class AliasedComposedContextConfigAndTestPropSourceClass {
909940
static class TestComponentScanClass {
910941
}
911942

943+
@SpringAppConfig(Number.class)
944+
static class SpringAppConfigClass {
945+
}
946+
912947
}

0 commit comments

Comments
 (0)