From d0c0d9fc5a1c26a78ddfeda5bc511f136f623db6 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sat, 20 Jun 2015 18:27:36 +0200 Subject: [PATCH] Synthesize annotation from defaults This commit introduces a convenience method in AnnotationUtils for synthesizing an annotation from its default attribute values. TransactionalTestExecutionListener has been refactored to invoke this new convenience method. Issue: SPR-13087 --- .../core/annotation/AnnotationUtils.java | 22 +++++++++++++++++++ .../core/annotation/AnnotationUtilsTests.java | 12 +++++----- .../TransactionalTestExecutionListener.java | 2 +- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java index e4469e5c0d..1d969e2f1d 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java @@ -1207,6 +1207,7 @@ public abstract class AnnotationUtils { * {@code @AliasFor} is detected * @since 4.2 * @see #synthesizeAnnotation(Map, Class, AnnotatedElement) + * @see #synthesizeAnnotation(Class) */ @SuppressWarnings("unchecked") public static A synthesizeAnnotation(A annotation, AnnotatedElement annotatedElement) { @@ -1256,6 +1257,7 @@ public abstract class AnnotationUtils { * {@code @AliasFor} is detected * @since 4.2 * @see #synthesizeAnnotation(Annotation, AnnotatedElement) + * @see #synthesizeAnnotation(Class) */ @SuppressWarnings("unchecked") public static A synthesizeAnnotation(Map attributes, @@ -1275,6 +1277,26 @@ public abstract class AnnotationUtils { return synthesizedAnnotation; } + /** + * Synthesize an annotation from its default attributes values. + *

This method simply delegates to + * {@link #synthesizeAnnotation(Map, Class, AnnotatedElement)}, + * supplying an empty map for the source attribute values and {@code null} + * for the {@link AnnotatedElement}. + * + * @param annotationType the type of annotation to synthesize; never {@code null} + * @return the synthesized annotation + * @throws IllegalArgumentException if a required attribute is missing + * @throws AnnotationConfigurationException if invalid configuration of + * {@code @AliasFor} is detected + * @since 4.2 + * @see #synthesizeAnnotation(Map, Class, AnnotatedElement) + * @see #synthesizeAnnotation(Annotation, AnnotatedElement) + */ + public static A synthesizeAnnotation(Class annotationType) { + return synthesizeAnnotation(Collections. emptyMap(), annotationType, null); + } + /** * Synthesize the supplied array of {@code annotations} by * creating a new array of the same size and type and populating it diff --git a/spring-core/src/test/java/org/springframework/core/annotation/AnnotationUtilsTests.java b/spring-core/src/test/java/org/springframework/core/annotation/AnnotationUtilsTests.java index 4c363e7159..416f43d04c 100644 --- a/spring-core/src/test/java/org/springframework/core/annotation/AnnotationUtilsTests.java +++ b/spring-core/src/test/java/org/springframework/core/annotation/AnnotationUtilsTests.java @@ -56,8 +56,6 @@ import static org.springframework.core.annotation.AnnotationUtils.*; */ public class AnnotationUtilsTests { - private static final Map EMPTY_ATTRS = Collections.emptyMap(); - @Rule public final ExpectedException exception = ExpectedException.none(); @@ -840,8 +838,8 @@ public class AnnotationUtilsTests { } @Test - public void synthesizeAnnotationFromMapWithEmptyAttributesWithDefaultsWithoutAttributeAliases() throws Exception { - AnnotationWithDefaults annotationWithDefaults = synthesizeAnnotation(EMPTY_ATTRS, AnnotationWithDefaults.class, null); + public void synthesizeAnnotationFromDefaultsWithoutAttributeAliases() throws Exception { + AnnotationWithDefaults annotationWithDefaults = synthesizeAnnotation(AnnotationWithDefaults.class); assertNotNull(annotationWithDefaults); assertEquals("text: ", "enigma", annotationWithDefaults.text()); assertTrue("predicate: ", annotationWithDefaults.predicate()); @@ -849,8 +847,8 @@ public class AnnotationUtilsTests { } @Test - public void synthesizeAnnotationFromMapWithEmptyAttributesWithDefaultsWithAttributeAliases() throws Exception { - ContextConfig contextConfig = synthesizeAnnotation(EMPTY_ATTRS, ContextConfig.class, null); + public void synthesizeAnnotationFromDefaultsWithAttributeAliases() throws Exception { + ContextConfig contextConfig = synthesizeAnnotation(ContextConfig.class); assertNotNull(contextConfig); assertEquals("value: ", "", contextConfig.value()); assertEquals("locations: ", "", contextConfig.locations()); @@ -868,7 +866,7 @@ public class AnnotationUtilsTests { @Test public void synthesizeAnnotationFromMapWithMissingAttributeValue() throws Exception { - assertMissingTextAttribute(EMPTY_ATTRS); + assertMissingTextAttribute(Collections.emptyMap()); } @Test diff --git a/spring-test/src/main/java/org/springframework/test/context/transaction/TransactionalTestExecutionListener.java b/spring-test/src/main/java/org/springframework/test/context/transaction/TransactionalTestExecutionListener.java index 1e6c5eb853..e108393363 100644 --- a/spring-test/src/main/java/org/springframework/test/context/transaction/TransactionalTestExecutionListener.java +++ b/spring-test/src/main/java/org/springframework/test/context/transaction/TransactionalTestExecutionListener.java @@ -135,7 +135,7 @@ public class TransactionalTestExecutionListener extends AbstractTestExecutionLis private static final Log logger = LogFactory.getLog(TransactionalTestExecutionListener.class); private static final TransactionConfiguration defaultTransactionConfiguration = - AnnotationUtils.synthesizeAnnotation(Collections. emptyMap(), TransactionConfiguration.class, null); + AnnotationUtils.synthesizeAnnotation(TransactionConfiguration.class); protected final TransactionAttributeSource attributeSource = new AnnotationTransactionAttributeSource();