Browse Source

Introduce getAnnotationAttributes(..,Class) in AnnoElUtils

pull/811/head
Sam Brannen 11 years ago
parent
commit
9afcd17c71
  1. 6
      spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java
  2. 31
      spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java
  3. 8
      spring-core/src/test/java/org/springframework/core/annotation/AnnotatedElementUtilsTests.java
  4. 6
      spring-tx/src/main/java/org/springframework/transaction/annotation/JtaTransactionAnnotationParser.java
  5. 6
      spring-tx/src/main/java/org/springframework/transaction/annotation/SpringTransactionAnnotationParser.java

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

@ -442,9 +442,9 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean
private AnnotationAttributes findAutowiredAnnotation(AccessibleObject ao) { private AnnotationAttributes findAutowiredAnnotation(AccessibleObject ao) {
for (Class<? extends Annotation> type : this.autowiredAnnotationTypes) { for (Class<? extends Annotation> type : this.autowiredAnnotationTypes) {
AnnotationAttributes ann = AnnotatedElementUtils.getAnnotationAttributes(ao, type.getName()); AnnotationAttributes attributes = AnnotatedElementUtils.getAnnotationAttributes(ao, type);
if (ann != null) { if (attributes != null) {
return ann; return attributes;
} }
} }
return null; return null;

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

@ -218,6 +218,31 @@ public class AnnotatedElementUtils {
})); }));
} }
/**
* Get the first annotation of the specified {@code annotationType} within
* the annotation hierarchy <em>above</em> the supplied {@code element} and
* merge that annotation's attributes with <em>matching</em> attributes from
* annotations in lower levels of the annotation hierarchy.
*
* <p>{@link AliasFor @AliasFor} semantics are fully supported, both
* within a single annotation and within the annotation hierarchy.
*
* <p>This method delegates to {@link #getAnnotationAttributes(AnnotatedElement, String)}.
*
* @param element the annotated element; never {@code null}
* @param annotationType the annotation type to find; never {@code null}
* @return the merged {@code AnnotationAttributes}, or {@code null} if not found
* @see #getAnnotationAttributes(AnnotatedElement, String, boolean, boolean)
* @see #getAllAnnotationAttributes(AnnotatedElement, String)
* @see #findAnnotationAttributes(AnnotatedElement, String, boolean, boolean)
* @see #findAnnotation(AnnotatedElement, Class)
*/
public static AnnotationAttributes getAnnotationAttributes(AnnotatedElement element,
Class<? extends Annotation> annotationType) {
Assert.notNull(annotationType, "annotationType must not be null");
return getAnnotationAttributes(element, annotationType.getName());
}
/** /**
* Get the first annotation of the specified {@code annotationType} within * Get the first annotation of the specified {@code annotationType} within
* the annotation hierarchy <em>above</em> the supplied {@code element} and * the annotation hierarchy <em>above</em> the supplied {@code element} and
@ -298,9 +323,7 @@ public class AnnotatedElementUtils {
* <p>{@link AliasFor @AliasFor} semantics are fully supported, both * <p>{@link AliasFor @AliasFor} semantics are fully supported, both
* within a single annotation and within the annotation hierarchy. * within a single annotation and within the annotation hierarchy.
* *
* <p>This method delegates to {@link #findAnnotationAttributes(AnnotatedElement, String, boolean, boolean)} * <p>This method delegates to {@link #findAnnotation(AnnotatedElement, String)}.
* (supplying {@code false} for {@code classValuesAsString} and {@code nestedAnnotationsAsMap})
* and {@link AnnotationUtils#synthesizeAnnotation(Map, Class, AnnotatedElement)}.
* *
* @param element the annotated element; never {@code null} * @param element the annotated element; never {@code null}
* @param annotationType the annotation type to find; never {@code null} * @param annotationType the annotation type to find; never {@code null}
@ -308,7 +331,7 @@ public class AnnotatedElementUtils {
* @since 4.2 * @since 4.2
* @see #findAnnotation(AnnotatedElement, String) * @see #findAnnotation(AnnotatedElement, String)
* @see #findAnnotationAttributes(AnnotatedElement, String, boolean, boolean) * @see #findAnnotationAttributes(AnnotatedElement, String, boolean, boolean)
* @see AnnotationUtils#synthesizeAnnotation(Map, Class, AnnotatedElement) * @see #getAnnotationAttributes(AnnotatedElement, Class)
*/ */
public static <A extends Annotation> A findAnnotation(AnnotatedElement element, Class<A> annotationType) { public static <A extends Annotation> A findAnnotation(AnnotatedElement element, Class<A> annotationType) {
Assert.notNull(annotationType, "annotationType must not be null"); Assert.notNull(annotationType, "annotationType must not be null");

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

@ -325,8 +325,6 @@ public class AnnotatedElementUtilsTests {
@Test @Test
public void getAnnotationAttributesWithInvalidConventionBasedComposedAnnotation() { public void getAnnotationAttributesWithInvalidConventionBasedComposedAnnotation() {
Class<?> element = InvalidConventionBasedComposedContextConfigClass.class; Class<?> element = InvalidConventionBasedComposedContextConfigClass.class;
String name = ContextConfig.class.getName();
exception.expect(AnnotationConfigurationException.class); exception.expect(AnnotationConfigurationException.class);
exception.expectMessage(either(containsString("attribute [value] and its alias [locations]")).or( exception.expectMessage(either(containsString("attribute [value] and its alias [locations]")).or(
containsString("attribute [locations] and its alias [value]"))); containsString("attribute [locations] and its alias [value]")));
@ -334,21 +332,19 @@ public class AnnotatedElementUtilsTests {
containsString("values of [{duplicateDeclaration}] and [{requiredLocationsDeclaration}]")).or( containsString("values of [{duplicateDeclaration}] and [{requiredLocationsDeclaration}]")).or(
containsString("values of [{requiredLocationsDeclaration}] and [{duplicateDeclaration}]"))); containsString("values of [{requiredLocationsDeclaration}] and [{duplicateDeclaration}]")));
exception.expectMessage(containsString("but only one declaration is permitted")); exception.expectMessage(containsString("but only one declaration is permitted"));
getAnnotationAttributes(element, name); getAnnotationAttributes(element, ContextConfig.class);
} }
@Test @Test
public void getAnnotationAttributesWithInvalidAliasedComposedAnnotation() { public void getAnnotationAttributesWithInvalidAliasedComposedAnnotation() {
Class<?> element = InvalidAliasedComposedContextConfigClass.class; Class<?> element = InvalidAliasedComposedContextConfigClass.class;
String name = ContextConfig.class.getName();
exception.expect(AnnotationConfigurationException.class); exception.expect(AnnotationConfigurationException.class);
exception.expectMessage(either(containsString("attribute [value] and its alias [locations]")).or( exception.expectMessage(either(containsString("attribute [value] and its alias [locations]")).or(
containsString("attribute [locations] and its alias [value]"))); containsString("attribute [locations] and its alias [value]")));
exception.expectMessage(either(containsString("values of [{duplicateDeclaration}] and [{test.xml}]")).or( exception.expectMessage(either(containsString("values of [{duplicateDeclaration}] and [{test.xml}]")).or(
containsString("values of [{test.xml}] and [{duplicateDeclaration}]"))); containsString("values of [{test.xml}] and [{duplicateDeclaration}]")));
exception.expectMessage(containsString("but only one declaration is permitted")); exception.expectMessage(containsString("but only one declaration is permitted"));
getAnnotationAttributes(element, name); getAnnotationAttributes(element, ContextConfig.class);
} }
@Test @Test

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

@ -39,9 +39,9 @@ public class JtaTransactionAnnotationParser implements TransactionAnnotationPars
@Override @Override
public TransactionAttribute parseTransactionAnnotation(AnnotatedElement ae) { public TransactionAttribute parseTransactionAnnotation(AnnotatedElement ae) {
AnnotationAttributes ann = AnnotatedElementUtils.getAnnotationAttributes(ae, javax.transaction.Transactional.class.getName()); AnnotationAttributes attributes = AnnotatedElementUtils.getAnnotationAttributes(ae, javax.transaction.Transactional.class);
if (ann != null) { if (attributes != null) {
return parseTransactionAnnotation(ann); return parseTransactionAnnotation(attributes);
} }
else { else {
return null; return null;

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

@ -39,9 +39,9 @@ public class SpringTransactionAnnotationParser implements TransactionAnnotationP
@Override @Override
public TransactionAttribute parseTransactionAnnotation(AnnotatedElement ae) { public TransactionAttribute parseTransactionAnnotation(AnnotatedElement ae) {
AnnotationAttributes ann = AnnotatedElementUtils.getAnnotationAttributes(ae, Transactional.class.getName()); AnnotationAttributes attributes = AnnotatedElementUtils.getAnnotationAttributes(ae, Transactional.class);
if (ann != null) { if (attributes != null) {
return parseTransactionAnnotation(ann); return parseTransactionAnnotation(attributes);
} }
else { else {
return null; return null;

Loading…
Cancel
Save