diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java index d5568b2663c..0d6400be264 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java @@ -226,6 +226,8 @@ public class AnnotatedElementUtils { * @return the merged {@code AnnotationAttributes}, or {@code null} if * not found * @see #getAnnotationAttributes(AnnotatedElement, String, boolean, boolean) + * @see #findAnnotationAttributes(AnnotatedElement, Class) + * @see #findAnnotationAttributes(AnnotatedElement, String) */ public static AnnotationAttributes getAnnotationAttributes(AnnotatedElement element, String annotationType) { return getAnnotationAttributes(element, annotationType, false, false); @@ -249,6 +251,9 @@ public class AnnotatedElementUtils { * as Annotation instances * @return the merged {@code AnnotationAttributes}, or {@code null} if * not found + * @see #findAnnotationAttributes(AnnotatedElement, Class) + * @see #findAnnotationAttributes(AnnotatedElement, String) + * @see #findAnnotationAttributes(AnnotatedElement, String, boolean, boolean) */ public static AnnotationAttributes getAnnotationAttributes(AnnotatedElement element, String annotationType, boolean classValuesAsString, boolean nestedAnnotationsAsMap) { diff --git a/spring-core/src/test/java/org/springframework/core/annotation/AnnotatedElementUtilsTests.java b/spring-core/src/test/java/org/springframework/core/annotation/AnnotatedElementUtilsTests.java index b46820fef5b..28da1783eb0 100644 --- a/spring-core/src/test/java/org/springframework/core/annotation/AnnotatedElementUtilsTests.java +++ b/spring-core/src/test/java/org/springframework/core/annotation/AnnotatedElementUtilsTests.java @@ -170,16 +170,24 @@ public class AnnotatedElementUtilsTests { @Test public void getAnnotationAttributesOnClassWithLocalAnnotation() { - AnnotationAttributes attributes = getAnnotationAttributes(TxConfig.class, Transactional.class.getName()); + Class element = TxConfig.class; + String name = Transactional.class.getName(); + AnnotationAttributes attributes = getAnnotationAttributes(element, name); assertNotNull("Annotation attributes for @Transactional on TxConfig", attributes); assertEquals("value for TxConfig.", "TxConfig", attributes.getString("value")); + // Verify contracts between utility methods: + assertTrue(isAnnotated(element, name)); } @Test public void getAnnotationAttributesOnClassWithLocalAnnotationThatShadowsAnnotationFromSuperclass() { - AnnotationAttributes attributes = getAnnotationAttributes(DerivedTxConfig.class, Transactional.class.getName()); + Class element = DerivedTxConfig.class; + String name = Transactional.class.getName(); + AnnotationAttributes attributes = getAnnotationAttributes(element, name); assertNotNull("Annotation attributes for @Transactional on DerivedTxConfig", attributes); assertEquals("value for DerivedTxConfig.", "DerivedTxConfig", attributes.getString("value")); + // Verify contracts between utility methods: + assertTrue(isAnnotated(element, name)); } @Test @@ -191,9 +199,12 @@ public class AnnotatedElementUtilsTests { @Test public void getAnnotationAttributesFavorsInheritedAnnotationsOverMoreLocallyDeclaredComposedAnnotations() { - AnnotationAttributes attributes = getAnnotationAttributes(SubSubClassWithInheritedAnnotation.class, - Transactional.class.getName()); + Class element = SubSubClassWithInheritedAnnotation.class; + String name = Transactional.class.getName(); + AnnotationAttributes attributes = getAnnotationAttributes(element, name); assertNotNull("AnnotationAttributes for @Transactional on SubSubClassWithInheritedAnnotation", attributes); + // Verify contracts between utility methods: + assertTrue(isAnnotated(element, name)); // TODO [SPR-11598] Set expected to true. boolean expected = false; @@ -202,10 +213,12 @@ public class AnnotatedElementUtilsTests { @Test public void getAnnotationAttributesFavorsInheritedComposedAnnotationsOverMoreLocallyDeclaredComposedAnnotations() { - AnnotationAttributes attributes = getAnnotationAttributes(SubSubClassWithInheritedComposedAnnotation.class, - Transactional.class.getName()); - assertNotNull("AnnotationAttributtes for @Transactional on SubSubClassWithInheritedComposedAnnotation.", - attributes); + Class element = SubSubClassWithInheritedComposedAnnotation.class; + String name = Transactional.class.getName(); + AnnotationAttributes attributes = getAnnotationAttributes(element, name); + assertNotNull("AnnotationAttributtes for @Transactional on SubSubClassWithInheritedComposedAnnotation.", attributes); + // Verify contracts between utility methods: + assertTrue(isAnnotated(element, name)); // TODO [SPR-11598] Set expected to true. boolean expected = false; @@ -217,16 +230,32 @@ public class AnnotatedElementUtilsTests { @Ignore("Disabled until SPR-11598 is resolved") @Test public void getAnnotationAttributesFromInterfaceImplementedBySuperclass() { + Class element = ConcreteClassWithInheritedAnnotation.class; String name = Transactional.class.getName(); - AnnotationAttributes attributes = getAnnotationAttributes(ConcreteClassWithInheritedAnnotation.class, name); + AnnotationAttributes attributes = getAnnotationAttributes(element, name); assertNotNull("Should find @Transactional on ConcreteClassWithInheritedAnnotation", attributes); + // Verify contracts between utility methods: + assertTrue(isAnnotated(element, name)); } @Test public void getAnnotationAttributesOnInheritedAnnotationInterface() { + Class element = InheritedAnnotationInterface.class; String name = Transactional.class.getName(); - AnnotationAttributes attributes = getAnnotationAttributes(InheritedAnnotationInterface.class, name); + AnnotationAttributes attributes = getAnnotationAttributes(element, name); assertNotNull("Should get @Transactional on InheritedAnnotationInterface", attributes); + // Verify contracts between utility methods: + assertTrue(isAnnotated(element, name)); + } + + @Test + public void getAnnotationAttributesOnNonInheritedAnnotationInterface() { + Class element = NonInheritedAnnotationInterface.class; + String name = Order.class.getName(); + AnnotationAttributes attributes = getAnnotationAttributes(element, name); + assertNotNull("Should get @Order on NonInheritedAnnotationInterface", attributes); + // Verify contracts between utility methods: + assertTrue(isAnnotated(element, name)); } @Test @@ -253,12 +282,6 @@ public class AnnotatedElementUtilsTests { assertNotNull("Should find @Order on NonInheritedAnnotationInterface", attributes); } - @Test - public void getAnnotationAttributesOnNonInheritedAnnotationInterface() { - AnnotationAttributes attributes = getAnnotationAttributes(NonInheritedAnnotationInterface.class, Order.class.getName()); - assertNotNull("Should get @Order on NonInheritedAnnotationInterface", attributes); - } - @Test public void findAnnotationAttributesOnSubNonInheritedAnnotationInterface() { AnnotationAttributes attributes = findAnnotationAttributes(SubNonInheritedAnnotationInterface.class, Order.class);