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
This commit is contained in:
parent
a0040245ca
commit
d0c0d9fc5a
|
@ -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 extends Annotation> 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 extends Annotation> A synthesizeAnnotation(Map<String, Object> attributes,
|
||||
|
@ -1275,6 +1277,26 @@ public abstract class AnnotationUtils {
|
|||
return synthesizedAnnotation;
|
||||
}
|
||||
|
||||
/**
|
||||
* <em>Synthesize</em> an annotation from its default attributes values.
|
||||
* <p>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 extends Annotation> A synthesizeAnnotation(Class<A> annotationType) {
|
||||
return synthesizeAnnotation(Collections.<String, Object> emptyMap(), annotationType, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* <em>Synthesize</em> the supplied array of {@code annotations} by
|
||||
* creating a new array of the same size and type and populating it
|
||||
|
|
|
@ -56,8 +56,6 @@ import static org.springframework.core.annotation.AnnotationUtils.*;
|
|||
*/
|
||||
public class AnnotationUtilsTests {
|
||||
|
||||
private static final Map<String, Object> 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
|
||||
|
|
|
@ -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.<String, Object> emptyMap(), TransactionConfiguration.class, null);
|
||||
AnnotationUtils.synthesizeAnnotation(TransactionConfiguration.class);
|
||||
|
||||
protected final TransactionAttributeSource attributeSource = new AnnotationTransactionAttributeSource();
|
||||
|
||||
|
|
Loading…
Reference in New Issue