Skip to content

Commit 7fdf775

Browse files
committed
Test status quo for @inherited annotations in AnnotationMetadata
See gh-24077
1 parent 59084c6 commit 7fdf775

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

spring-core/src/test/java/org/springframework/core/type/AnnotationMetadataTests.java

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.lang.annotation.Annotation;
2121
import java.lang.annotation.Documented;
2222
import java.lang.annotation.ElementType;
23+
import java.lang.annotation.Inherited;
2324
import java.lang.annotation.Retention;
2425
import java.lang.annotation.RetentionPolicy;
2526
import java.lang.annotation.Target;
@@ -37,7 +38,7 @@
3738
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
3839
import org.springframework.stereotype.Component;
3940

40-
import static org.hamcrest.CoreMatchers.*;
41+
import static org.hamcrest.Matchers.*;
4142
import static org.junit.Assert.*;
4243

4344
/**
@@ -70,18 +71,18 @@ public void asmAnnotationMetadata() throws Exception {
7071
@Test
7172
public void standardAnnotationMetadataForSubclass() {
7273
AnnotationMetadata metadata = new StandardAnnotationMetadata(AnnotatedComponentSubClass.class, true);
73-
doTestSubClassAnnotationInfo(metadata);
74+
doTestSubClassAnnotationInfo(metadata, false);
7475
}
7576

7677
@Test
7778
public void asmAnnotationMetadataForSubclass() throws Exception {
7879
MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
7980
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(AnnotatedComponentSubClass.class.getName());
8081
AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();
81-
doTestSubClassAnnotationInfo(metadata);
82+
doTestSubClassAnnotationInfo(metadata, true);
8283
}
8384

84-
private void doTestSubClassAnnotationInfo(AnnotationMetadata metadata) {
85+
private void doTestSubClassAnnotationInfo(AnnotationMetadata metadata, boolean asm) {
8586
assertThat(metadata.getClassName(), is(AnnotatedComponentSubClass.class.getName()));
8687
assertThat(metadata.isInterface(), is(false));
8788
assertThat(metadata.isAnnotation(), is(false));
@@ -93,12 +94,23 @@ private void doTestSubClassAnnotationInfo(AnnotationMetadata metadata) {
9394
assertThat(metadata.isAnnotated(Component.class.getName()), is(false));
9495
assertThat(metadata.isAnnotated(Scope.class.getName()), is(false));
9596
assertThat(metadata.isAnnotated(SpecialAttr.class.getName()), is(false));
97+
98+
if (asm) {
99+
assertThat(metadata.isAnnotated(NamedComposedAnnotation.class.getName()), is(false));
100+
assertThat(metadata.hasAnnotation(NamedComposedAnnotation.class.getName()), is(false));
101+
assertThat(metadata.getAnnotationTypes(), is(emptyCollectionOf(String.class)));
102+
}
103+
else {
104+
assertThat(metadata.isAnnotated(NamedComposedAnnotation.class.getName()), is(true));
105+
assertThat(metadata.hasAnnotation(NamedComposedAnnotation.class.getName()), is(true));
106+
assertThat(metadata.getAnnotationTypes(), containsInAnyOrder(NamedComposedAnnotation.class.getName()));
107+
}
108+
96109
assertThat(metadata.hasAnnotation(Component.class.getName()), is(false));
97110
assertThat(metadata.hasAnnotation(Scope.class.getName()), is(false));
98111
assertThat(metadata.hasAnnotation(SpecialAttr.class.getName()), is(false));
99112
assertThat(metadata.hasMetaAnnotation(Component.class.getName()), is(false));
100113
assertThat(metadata.hasMetaAnnotation(MetaAnnotation.class.getName()), is(false));
101-
assertThat(metadata.getAnnotationTypes().size(), is(0));
102114
assertThat(metadata.getAnnotationAttributes(Component.class.getName()), nullValue());
103115
assertThat(metadata.getAnnotationAttributes(MetaAnnotation.class.getName(), false), nullValue());
104116
assertThat(metadata.getAnnotationAttributes(MetaAnnotation.class.getName(), true), nullValue());
@@ -266,13 +278,18 @@ private void doTestAnnotationInfo(AnnotationMetadata metadata) {
266278
assertThat(metadata.getInterfaceNames().length, is(1));
267279
assertThat(metadata.getInterfaceNames()[0], is(Serializable.class.getName()));
268280

281+
assertThat(metadata.isAnnotated(NamedComposedAnnotation.class.getName()), is(true));
282+
assertThat(metadata.isAnnotated(Component.class.getName()), is(true));
269283
assertThat(metadata.hasAnnotation(Component.class.getName()), is(true));
270284
assertThat(metadata.hasAnnotation(Scope.class.getName()), is(true));
271285
assertThat(metadata.hasAnnotation(SpecialAttr.class.getName()), is(true));
272-
assertThat(metadata.getAnnotationTypes().size(), is(6));
273-
assertThat(metadata.getAnnotationTypes().contains(Component.class.getName()), is(true));
274-
assertThat(metadata.getAnnotationTypes().contains(Scope.class.getName()), is(true));
275-
assertThat(metadata.getAnnotationTypes().contains(SpecialAttr.class.getName()), is(true));
286+
assertThat(metadata.hasAnnotation(NamedComposedAnnotation.class.getName()), is(true));
287+
assertThat(metadata.getAnnotationTypes(),
288+
containsInAnyOrder(Component.class.getName(), Scope.class.getName(),
289+
SpecialAttr.class.getName(), DirectAnnotation.class.getName(),
290+
MetaMetaAnnotation.class.getName(),
291+
EnumSubclasses.class.getName(),
292+
NamedComposedAnnotation.class.getName()));
276293

277294
AnnotationAttributes compAttrs = (AnnotationAttributes) metadata.getAnnotationAttributes(Component.class.getName());
278295
assertThat(compAttrs.size(), is(1));
@@ -469,6 +486,7 @@ public enum SubclassEnum {
469486
@DirectAnnotation(value = "direct", additional = "", additionalArray = {})
470487
@MetaMetaAnnotation
471488
@EnumSubclasses({SubclassEnum.FOO, SubclassEnum.BAR})
489+
@NamedComposedAnnotation
472490
private static class AnnotatedComponent implements Serializable {
473491

474492
@TestAutowired
@@ -549,6 +567,7 @@ public static class NamedAnnotationsClass {
549567
@NamedAnnotation3(name = "name 3")
550568
@Retention(RetentionPolicy.RUNTIME)
551569
@Target(ElementType.TYPE)
570+
@Inherited
552571
public @interface NamedComposedAnnotation {
553572
}
554573

0 commit comments

Comments
 (0)