Skip to content

Commit 9afcd17

Browse files
committed
Introduce getAnnotationAttributes(..,Class) in AnnoElUtils
1 parent 46be176 commit 9afcd17

File tree

5 files changed

+38
-19
lines changed

5 files changed

+38
-19
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,9 @@ public void doWith(Method method) throws IllegalArgumentException, IllegalAccess
442442

443443
private AnnotationAttributes findAutowiredAnnotation(AccessibleObject ao) {
444444
for (Class<? extends Annotation> type : this.autowiredAnnotationTypes) {
445-
AnnotationAttributes ann = AnnotatedElementUtils.getAnnotationAttributes(ao, type.getName());
446-
if (ann != null) {
447-
return ann;
445+
AnnotationAttributes attributes = AnnotatedElementUtils.getAnnotationAttributes(ao, type);
446+
if (attributes != null) {
447+
return attributes;
448448
}
449449
}
450450
return null;

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,31 @@ public Boolean process(AnnotatedElement annotatedElement, Annotation annotation,
218218
}));
219219
}
220220

221+
/**
222+
* Get the first annotation of the specified {@code annotationType} within
223+
* the annotation hierarchy <em>above</em> the supplied {@code element} and
224+
* merge that annotation's attributes with <em>matching</em> attributes from
225+
* annotations in lower levels of the annotation hierarchy.
226+
*
227+
* <p>{@link AliasFor @AliasFor} semantics are fully supported, both
228+
* within a single annotation and within the annotation hierarchy.
229+
*
230+
* <p>This method delegates to {@link #getAnnotationAttributes(AnnotatedElement, String)}.
231+
*
232+
* @param element the annotated element; never {@code null}
233+
* @param annotationType the annotation type to find; never {@code null}
234+
* @return the merged {@code AnnotationAttributes}, or {@code null} if not found
235+
* @see #getAnnotationAttributes(AnnotatedElement, String, boolean, boolean)
236+
* @see #getAllAnnotationAttributes(AnnotatedElement, String)
237+
* @see #findAnnotationAttributes(AnnotatedElement, String, boolean, boolean)
238+
* @see #findAnnotation(AnnotatedElement, Class)
239+
*/
240+
public static AnnotationAttributes getAnnotationAttributes(AnnotatedElement element,
241+
Class<? extends Annotation> annotationType) {
242+
Assert.notNull(annotationType, "annotationType must not be null");
243+
return getAnnotationAttributes(element, annotationType.getName());
244+
}
245+
221246
/**
222247
* Get the first annotation of the specified {@code annotationType} within
223248
* the annotation hierarchy <em>above</em> the supplied {@code element} and
@@ -298,17 +323,15 @@ public static AnnotationAttributes getAnnotationAttributes(AnnotatedElement elem
298323
* <p>{@link AliasFor @AliasFor} semantics are fully supported, both
299324
* within a single annotation and within the annotation hierarchy.
300325
*
301-
* <p>This method delegates to {@link #findAnnotationAttributes(AnnotatedElement, String, boolean, boolean)}
302-
* (supplying {@code false} for {@code classValuesAsString} and {@code nestedAnnotationsAsMap})
303-
* and {@link AnnotationUtils#synthesizeAnnotation(Map, Class, AnnotatedElement)}.
326+
* <p>This method delegates to {@link #findAnnotation(AnnotatedElement, String)}.
304327
*
305328
* @param element the annotated element; never {@code null}
306329
* @param annotationType the annotation type to find; never {@code null}
307330
* @return the merged, synthesized {@code Annotation}, or {@code null} if not found
308331
* @since 4.2
309332
* @see #findAnnotation(AnnotatedElement, String)
310333
* @see #findAnnotationAttributes(AnnotatedElement, String, boolean, boolean)
311-
* @see AnnotationUtils#synthesizeAnnotation(Map, Class, AnnotatedElement)
334+
* @see #getAnnotationAttributes(AnnotatedElement, Class)
312335
*/
313336
public static <A extends Annotation> A findAnnotation(AnnotatedElement element, Class<A> annotationType) {
314337
Assert.notNull(annotationType, "annotationType must not be null");

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -325,30 +325,26 @@ public void getAnnotationAttributesWithAliasedValueComposedAnnotation() {
325325
@Test
326326
public void getAnnotationAttributesWithInvalidConventionBasedComposedAnnotation() {
327327
Class<?> element = InvalidConventionBasedComposedContextConfigClass.class;
328-
String name = ContextConfig.class.getName();
329-
330328
exception.expect(AnnotationConfigurationException.class);
331329
exception.expectMessage(either(containsString("attribute [value] and its alias [locations]")).or(
332330
containsString("attribute [locations] and its alias [value]")));
333331
exception.expectMessage(either(
334332
containsString("values of [{duplicateDeclaration}] and [{requiredLocationsDeclaration}]")).or(
335333
containsString("values of [{requiredLocationsDeclaration}] and [{duplicateDeclaration}]")));
336334
exception.expectMessage(containsString("but only one declaration is permitted"));
337-
getAnnotationAttributes(element, name);
335+
getAnnotationAttributes(element, ContextConfig.class);
338336
}
339337

340338
@Test
341339
public void getAnnotationAttributesWithInvalidAliasedComposedAnnotation() {
342340
Class<?> element = InvalidAliasedComposedContextConfigClass.class;
343-
String name = ContextConfig.class.getName();
344-
345341
exception.expect(AnnotationConfigurationException.class);
346342
exception.expectMessage(either(containsString("attribute [value] and its alias [locations]")).or(
347343
containsString("attribute [locations] and its alias [value]")));
348344
exception.expectMessage(either(containsString("values of [{duplicateDeclaration}] and [{test.xml}]")).or(
349345
containsString("values of [{test.xml}] and [{duplicateDeclaration}]")));
350346
exception.expectMessage(containsString("but only one declaration is permitted"));
351-
getAnnotationAttributes(element, name);
347+
getAnnotationAttributes(element, ContextConfig.class);
352348
}
353349

354350
@Test

spring-tx/src/main/java/org/springframework/transaction/annotation/JtaTransactionAnnotationParser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public class JtaTransactionAnnotationParser implements TransactionAnnotationPars
3939

4040
@Override
4141
public TransactionAttribute parseTransactionAnnotation(AnnotatedElement ae) {
42-
AnnotationAttributes ann = AnnotatedElementUtils.getAnnotationAttributes(ae, javax.transaction.Transactional.class.getName());
43-
if (ann != null) {
44-
return parseTransactionAnnotation(ann);
42+
AnnotationAttributes attributes = AnnotatedElementUtils.getAnnotationAttributes(ae, javax.transaction.Transactional.class);
43+
if (attributes != null) {
44+
return parseTransactionAnnotation(attributes);
4545
}
4646
else {
4747
return null;

spring-tx/src/main/java/org/springframework/transaction/annotation/SpringTransactionAnnotationParser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public class SpringTransactionAnnotationParser implements TransactionAnnotationP
3939

4040
@Override
4141
public TransactionAttribute parseTransactionAnnotation(AnnotatedElement ae) {
42-
AnnotationAttributes ann = AnnotatedElementUtils.getAnnotationAttributes(ae, Transactional.class.getName());
43-
if (ann != null) {
44-
return parseTransactionAnnotation(ann);
42+
AnnotationAttributes attributes = AnnotatedElementUtils.getAnnotationAttributes(ae, Transactional.class);
43+
if (attributes != null) {
44+
return parseTransactionAnnotation(attributes);
4545
}
4646
else {
4747
return null;

0 commit comments

Comments
 (0)