Skip to content

Commit d0c0d9f

Browse files
committed
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
1 parent a004024 commit d0c0d9f

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,7 @@ static <A extends Annotation> A synthesizeAnnotation(A annotation) {
12071207
* {@code @AliasFor} is detected
12081208
* @since 4.2
12091209
* @see #synthesizeAnnotation(Map, Class, AnnotatedElement)
1210+
* @see #synthesizeAnnotation(Class)
12101211
*/
12111212
@SuppressWarnings("unchecked")
12121213
public static <A extends Annotation> A synthesizeAnnotation(A annotation, AnnotatedElement annotatedElement) {
@@ -1256,6 +1257,7 @@ public static <A extends Annotation> A synthesizeAnnotation(A annotation, Annota
12561257
* {@code @AliasFor} is detected
12571258
* @since 4.2
12581259
* @see #synthesizeAnnotation(Annotation, AnnotatedElement)
1260+
* @see #synthesizeAnnotation(Class)
12591261
*/
12601262
@SuppressWarnings("unchecked")
12611263
public static <A extends Annotation> A synthesizeAnnotation(Map<String, Object> attributes,
@@ -1275,6 +1277,26 @@ public static <A extends Annotation> A synthesizeAnnotation(Map<String, Object>
12751277
return synthesizedAnnotation;
12761278
}
12771279

1280+
/**
1281+
* <em>Synthesize</em> an annotation from its default attributes values.
1282+
* <p>This method simply delegates to
1283+
* {@link #synthesizeAnnotation(Map, Class, AnnotatedElement)},
1284+
* supplying an empty map for the source attribute values and {@code null}
1285+
* for the {@link AnnotatedElement}.
1286+
*
1287+
* @param annotationType the type of annotation to synthesize; never {@code null}
1288+
* @return the synthesized annotation
1289+
* @throws IllegalArgumentException if a required attribute is missing
1290+
* @throws AnnotationConfigurationException if invalid configuration of
1291+
* {@code @AliasFor} is detected
1292+
* @since 4.2
1293+
* @see #synthesizeAnnotation(Map, Class, AnnotatedElement)
1294+
* @see #synthesizeAnnotation(Annotation, AnnotatedElement)
1295+
*/
1296+
public static <A extends Annotation> A synthesizeAnnotation(Class<A> annotationType) {
1297+
return synthesizeAnnotation(Collections.<String, Object> emptyMap(), annotationType, null);
1298+
}
1299+
12781300
/**
12791301
* <em>Synthesize</em> the supplied array of {@code annotations} by
12801302
* creating a new array of the same size and type and populating it

spring-core/src/test/java/org/springframework/core/annotation/AnnotationUtilsTests.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@
5656
*/
5757
public class AnnotationUtilsTests {
5858

59-
private static final Map<String, Object> EMPTY_ATTRS = Collections.emptyMap();
60-
6159
@Rule
6260
public final ExpectedException exception = ExpectedException.none();
6361

@@ -840,17 +838,17 @@ public void synthesizeAnnotationFromMapWithoutAttributeAliases() throws Exceptio
840838
}
841839

842840
@Test
843-
public void synthesizeAnnotationFromMapWithEmptyAttributesWithDefaultsWithoutAttributeAliases() throws Exception {
844-
AnnotationWithDefaults annotationWithDefaults = synthesizeAnnotation(EMPTY_ATTRS, AnnotationWithDefaults.class, null);
841+
public void synthesizeAnnotationFromDefaultsWithoutAttributeAliases() throws Exception {
842+
AnnotationWithDefaults annotationWithDefaults = synthesizeAnnotation(AnnotationWithDefaults.class);
845843
assertNotNull(annotationWithDefaults);
846844
assertEquals("text: ", "enigma", annotationWithDefaults.text());
847845
assertTrue("predicate: ", annotationWithDefaults.predicate());
848846
assertArrayEquals("characters: ", new char[] { 'a', 'b', 'c' }, annotationWithDefaults.characters());
849847
}
850848

851849
@Test
852-
public void synthesizeAnnotationFromMapWithEmptyAttributesWithDefaultsWithAttributeAliases() throws Exception {
853-
ContextConfig contextConfig = synthesizeAnnotation(EMPTY_ATTRS, ContextConfig.class, null);
850+
public void synthesizeAnnotationFromDefaultsWithAttributeAliases() throws Exception {
851+
ContextConfig contextConfig = synthesizeAnnotation(ContextConfig.class);
854852
assertNotNull(contextConfig);
855853
assertEquals("value: ", "", contextConfig.value());
856854
assertEquals("locations: ", "", contextConfig.locations());
@@ -868,7 +866,7 @@ public void synthesizeAnnotationFromMapWithMinimalAttributesWithAttributeAliases
868866

869867
@Test
870868
public void synthesizeAnnotationFromMapWithMissingAttributeValue() throws Exception {
871-
assertMissingTextAttribute(EMPTY_ATTRS);
869+
assertMissingTextAttribute(Collections.emptyMap());
872870
}
873871

874872
@Test

spring-test/src/main/java/org/springframework/test/context/transaction/TransactionalTestExecutionListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public class TransactionalTestExecutionListener extends AbstractTestExecutionLis
135135
private static final Log logger = LogFactory.getLog(TransactionalTestExecutionListener.class);
136136

137137
private static final TransactionConfiguration defaultTransactionConfiguration =
138-
AnnotationUtils.synthesizeAnnotation(Collections.<String, Object> emptyMap(), TransactionConfiguration.class, null);
138+
AnnotationUtils.synthesizeAnnotation(TransactionConfiguration.class);
139139

140140
protected final TransactionAttributeSource attributeSource = new AnnotationTransactionAttributeSource();
141141

0 commit comments

Comments
 (0)