Skip to content

Commit f44217a

Browse files
committed
Clarified getAllAnnotationAttributes behavior
Issue: SPR-12473 (cherry picked from commit 5ac8680)
1 parent b3bfa95 commit f44217a

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

spring-core/src/main/java/org/springframework/core/type/AnnotatedTypeMetadata.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
public interface AnnotatedTypeMetadata {
3939

4040
/**
41-
* Determine whether the underlying type has an annotation or
42-
* meta-annotation of the given type defined.
41+
* Determine whether the underlying element has an annotation or meta-annotation
42+
* of the given type defined.
4343
* <p>If this method returns {@code true}, then
4444
* {@link #getAnnotationAttributes} will return a non-null Map.
4545
* @param annotationType the annotation type to look for
@@ -48,9 +48,9 @@ public interface AnnotatedTypeMetadata {
4848
boolean isAnnotated(String annotationType);
4949

5050
/**
51-
* Retrieve the attributes of the annotation of the given type,
52-
* if any (i.e. if defined on the underlying class, as direct
53-
* annotation or as meta-annotation).
51+
* Retrieve the attributes of the annotation of the given type, if any (i.e. if
52+
* defined on the underlying element, as direct annotation or meta-annotation),
53+
* also taking attribute overrides on composed annotations into account.
5454
* @param annotationType the annotation type to look for
5555
* @return a Map of attributes, with the attribute name as key (e.g. "value")
5656
* and the defined attribute value as Map value. This return value will be
@@ -59,9 +59,9 @@ public interface AnnotatedTypeMetadata {
5959
Map<String, Object> getAnnotationAttributes(String annotationType);
6060

6161
/**
62-
* Retrieve the attributes of the annotation of the given type,
63-
* if any (i.e. if defined on the underlying class, as direct
64-
* annotation or as meta-annotation).
62+
* Retrieve the attributes of the annotation of the given type, if any (i.e. if
63+
* defined on the underlying element, as direct annotation or meta-annotation),
64+
* also taking attribute overrides on composed annotations into account.
6565
* @param annotationType the annotation type to look for
6666
* @param classValuesAsString whether to convert class references to String
6767
* class names for exposure as values in the returned Map, instead of Class
@@ -74,8 +74,8 @@ public interface AnnotatedTypeMetadata {
7474

7575
/**
7676
* Retrieve all attributes of all annotations of the given type, if any (i.e. if
77-
* defined on the underlying type ({@link AnnotationMetadata class} or
78-
* {@link MethodMetadata method}), as direct annotation or as meta-annotation).
77+
* defined on the underlying element, as direct annotation or meta-annotation).
78+
* Note that this variant does <i>not</i> take attribute overrides into account.
7979
* @param annotationType the annotation type to look for
8080
* @return a MultiMap of attributes, with the attribute name as key (e.g. "value")
8181
* and a list of the defined attribute values as Map value. This return value will
@@ -86,8 +86,8 @@ public interface AnnotatedTypeMetadata {
8686

8787
/**
8888
* Retrieve all attributes of all annotations of the given type, if any (i.e. if
89-
* defined on the underlying type ({@link AnnotationMetadata class} or
90-
* {@link MethodMetadata method}), as direct annotation or as meta-annotation).
89+
* defined on the underlying element, as direct annotation or meta-annotation).
90+
* Note that this variant does <i>not</i> take attribute overrides into account.
9191
* @param annotationType the annotation type to look for
9292
* @param classValuesAsString whether to convert class references to String
9393
* @return a MultiMap of attributes, with the attribute name as key (e.g. "value")

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

+18-8
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ public void composedAnnotationWithMetaAnnotationsWithIdenticalAttributeNamesUsin
244244
assertMultipleAnnotationsWithIdenticalAttributeNames(metadata);
245245
}
246246

247+
247248
private void assertMultipleAnnotationsWithIdenticalAttributeNames(AnnotationMetadata metadata) {
248249
AnnotationAttributes attributes1 = (AnnotationAttributes) metadata.getAnnotationAttributes(
249250
NamedAnnotation1.class.getName(), false);
@@ -292,6 +293,8 @@ private void doTestAnnotationInfo(AnnotationMetadata metadata) {
292293
assertEquals("direct", method.getAnnotationAttributes(DirectAnnotation.class.getName()).get("value"));
293294
List<Object> allMeta = method.getAllAnnotationAttributes(DirectAnnotation.class.getName()).get("value");
294295
assertThat(new HashSet<Object>(allMeta), is(equalTo(new HashSet<Object>(Arrays.asList("direct", "meta")))));
296+
allMeta = method.getAllAnnotationAttributes(DirectAnnotation.class.getName()).get("additional");
297+
assertThat(new HashSet<Object>(allMeta), is(equalTo(new HashSet<Object>(Arrays.asList("direct")))));
295298

296299
assertTrue(metadata.isAnnotated(IsAnnotatedAnnotation.class.getName()));
297300

@@ -332,6 +335,8 @@ private void doTestAnnotationInfo(AnnotationMetadata metadata) {
332335
assertEquals("direct", metadata.getAnnotationAttributes(DirectAnnotation.class.getName()).get("value"));
333336
allMeta = metadata.getAllAnnotationAttributes(DirectAnnotation.class.getName()).get("value");
334337
assertThat(new HashSet<Object>(allMeta), is(equalTo(new HashSet<Object>(Arrays.asList("direct", "meta")))));
338+
allMeta = metadata.getAllAnnotationAttributes(DirectAnnotation.class.getName()).get("additional");
339+
assertThat(new HashSet<Object>(allMeta), is(equalTo(new HashSet<Object>(Arrays.asList("direct")))));
335340
}
336341
{ // perform tests with classValuesAsString = true
337342
AnnotationAttributes specialAttrs = (AnnotationAttributes) metadata.getAnnotationAttributes(
@@ -407,14 +412,16 @@ public static enum SomeEnum {
407412
NestedAnno[] optionalArray() default { @NestedAnno(value = "optional", anEnum = SomeEnum.DEFAULT, classArray = Void.class) };
408413
}
409414

410-
@Target({ ElementType.TYPE, ElementType.METHOD })
415+
@Target({ElementType.TYPE, ElementType.METHOD})
411416
@Retention(RetentionPolicy.RUNTIME)
412417
public @interface DirectAnnotation {
413418

414419
String value();
420+
421+
String additional() default "direct";
415422
}
416423

417-
@Target({ ElementType.TYPE })
424+
@Target(ElementType.TYPE)
418425
@Retention(RetentionPolicy.RUNTIME)
419426
public @interface IsAnnotatedAnnotation {
420427
}
@@ -424,9 +431,11 @@ public static enum SomeEnum {
424431
@DirectAnnotation("meta")
425432
@IsAnnotatedAnnotation
426433
public @interface MetaAnnotation {
434+
435+
String additional() default "meta";
427436
}
428437

429-
@Target({ ElementType.TYPE, ElementType.METHOD })
438+
@Target({ElementType.TYPE, ElementType.METHOD})
430439
@Retention(RetentionPolicy.RUNTIME)
431440
@MetaAnnotation
432441
public @interface MetaMetaAnnotation {
@@ -446,17 +455,18 @@ public static enum SubclassEnum {
446455
},
447456
BAR {
448457
/* Do not delete! This subclassing is intentional. */
449-
};
458+
}
450459
}
451460

452461
@Component("myName")
453462
@Scope("myScope")
454-
@SpecialAttr(clazz = String.class, state = Thread.State.NEW, nestedAnno = @NestedAnno(value = "na", anEnum = SomeEnum.LABEL1, classArray = { String.class }), nestedAnnoArray = {
455-
@NestedAnno, @NestedAnno(value = "na1", anEnum = SomeEnum.LABEL2, classArray = { Number.class }) })
456-
@SuppressWarnings({ "serial", "unused" })
463+
@SpecialAttr(clazz = String.class, state = Thread.State.NEW,
464+
nestedAnno = @NestedAnno(value = "na", anEnum = SomeEnum.LABEL1, classArray = {String.class}),
465+
nestedAnnoArray = {@NestedAnno, @NestedAnno(value = "na1", anEnum = SomeEnum.LABEL2, classArray = {Number.class})})
466+
@SuppressWarnings({"serial", "unused"})
457467
@DirectAnnotation("direct")
458468
@MetaMetaAnnotation
459-
@EnumSubclasses({ SubclassEnum.FOO, SubclassEnum.BAR })
469+
@EnumSubclasses({SubclassEnum.FOO, SubclassEnum.BAR})
460470
private static class AnnotatedComponent implements Serializable {
461471

462472
@TestAutowired

0 commit comments

Comments
 (0)