20
20
import java .lang .annotation .Annotation ;
21
21
import java .lang .annotation .Documented ;
22
22
import java .lang .annotation .ElementType ;
23
+ import java .lang .annotation .Inherited ;
23
24
import java .lang .annotation .Retention ;
24
25
import java .lang .annotation .RetentionPolicy ;
25
26
import java .lang .annotation .Target ;
37
38
import org .springframework .core .type .classreading .SimpleMetadataReaderFactory ;
38
39
import org .springframework .stereotype .Component ;
39
40
40
- import static org .hamcrest .CoreMatchers .*;
41
+ import static org .hamcrest .Matchers .*;
41
42
import static org .junit .Assert .*;
42
43
43
44
/**
@@ -70,18 +71,18 @@ public void asmAnnotationMetadata() throws Exception {
70
71
@ Test
71
72
public void standardAnnotationMetadataForSubclass () {
72
73
AnnotationMetadata metadata = new StandardAnnotationMetadata (AnnotatedComponentSubClass .class , true );
73
- doTestSubClassAnnotationInfo (metadata );
74
+ doTestSubClassAnnotationInfo (metadata , false );
74
75
}
75
76
76
77
@ Test
77
78
public void asmAnnotationMetadataForSubclass () throws Exception {
78
79
MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory ();
79
80
MetadataReader metadataReader = metadataReaderFactory .getMetadataReader (AnnotatedComponentSubClass .class .getName ());
80
81
AnnotationMetadata metadata = metadataReader .getAnnotationMetadata ();
81
- doTestSubClassAnnotationInfo (metadata );
82
+ doTestSubClassAnnotationInfo (metadata , true );
82
83
}
83
84
84
- private void doTestSubClassAnnotationInfo (AnnotationMetadata metadata ) {
85
+ private void doTestSubClassAnnotationInfo (AnnotationMetadata metadata , boolean asm ) {
85
86
assertThat (metadata .getClassName (), is (AnnotatedComponentSubClass .class .getName ()));
86
87
assertThat (metadata .isInterface (), is (false ));
87
88
assertThat (metadata .isAnnotation (), is (false ));
@@ -93,12 +94,23 @@ private void doTestSubClassAnnotationInfo(AnnotationMetadata metadata) {
93
94
assertThat (metadata .isAnnotated (Component .class .getName ()), is (false ));
94
95
assertThat (metadata .isAnnotated (Scope .class .getName ()), is (false ));
95
96
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
+
96
109
assertThat (metadata .hasAnnotation (Component .class .getName ()), is (false ));
97
110
assertThat (metadata .hasAnnotation (Scope .class .getName ()), is (false ));
98
111
assertThat (metadata .hasAnnotation (SpecialAttr .class .getName ()), is (false ));
99
112
assertThat (metadata .hasMetaAnnotation (Component .class .getName ()), is (false ));
100
113
assertThat (metadata .hasMetaAnnotation (MetaAnnotation .class .getName ()), is (false ));
101
- assertThat (metadata .getAnnotationTypes ().size (), is (0 ));
102
114
assertThat (metadata .getAnnotationAttributes (Component .class .getName ()), nullValue ());
103
115
assertThat (metadata .getAnnotationAttributes (MetaAnnotation .class .getName (), false ), nullValue ());
104
116
assertThat (metadata .getAnnotationAttributes (MetaAnnotation .class .getName (), true ), nullValue ());
@@ -266,13 +278,18 @@ private void doTestAnnotationInfo(AnnotationMetadata metadata) {
266
278
assertThat (metadata .getInterfaceNames ().length , is (1 ));
267
279
assertThat (metadata .getInterfaceNames ()[0 ], is (Serializable .class .getName ()));
268
280
281
+ assertThat (metadata .isAnnotated (NamedComposedAnnotation .class .getName ()), is (true ));
282
+ assertThat (metadata .isAnnotated (Component .class .getName ()), is (true ));
269
283
assertThat (metadata .hasAnnotation (Component .class .getName ()), is (true ));
270
284
assertThat (metadata .hasAnnotation (Scope .class .getName ()), is (true ));
271
285
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 ()));
276
293
277
294
AnnotationAttributes compAttrs = (AnnotationAttributes ) metadata .getAnnotationAttributes (Component .class .getName ());
278
295
assertThat (compAttrs .size (), is (1 ));
@@ -469,6 +486,7 @@ public enum SubclassEnum {
469
486
@ DirectAnnotation (value = "direct" , additional = "" , additionalArray = {})
470
487
@ MetaMetaAnnotation
471
488
@ EnumSubclasses ({SubclassEnum .FOO , SubclassEnum .BAR })
489
+ @ NamedComposedAnnotation
472
490
private static class AnnotatedComponent implements Serializable {
473
491
474
492
@ TestAutowired
@@ -549,6 +567,7 @@ public static class NamedAnnotationsClass {
549
567
@ NamedAnnotation3 (name = "name 3" )
550
568
@ Retention (RetentionPolicy .RUNTIME )
551
569
@ Target (ElementType .TYPE )
570
+ @ Inherited
552
571
public @interface NamedComposedAnnotation {
553
572
}
554
573
0 commit comments