Introduce getAnnotationAttributes(..,Class) in AnnoElUtils

This commit is contained in:
Sam Brannen
2015-05-29 21:36:00 +02:00
parent 46be176875
commit 9afcd17c71
5 changed files with 38 additions and 19 deletions

View File

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

View File

@@ -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
* 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
* within a single annotation and within the annotation hierarchy.
*
* <p>This method delegates to {@link #findAnnotationAttributes(AnnotatedElement, String, boolean, boolean)}
* (supplying {@code false} for {@code classValuesAsString} and {@code nestedAnnotationsAsMap})
* and {@link AnnotationUtils#synthesizeAnnotation(Map, Class, AnnotatedElement)}.
* <p>This method delegates to {@link #findAnnotation(AnnotatedElement, String)}.
*
* @param element the annotated element; never {@code null}
* @param annotationType the annotation type to find; never {@code null}
@@ -308,7 +331,7 @@ public class AnnotatedElementUtils {
* @since 4.2
* @see #findAnnotation(AnnotatedElement, String)
* @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) {
Assert.notNull(annotationType, "annotationType must not be null");

View File

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

View File

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

View File

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