Browse Source

Favor local, composed annotations in the TCF

Prior to this commit, AnnotationAttributes retrieved from
MetaAnnotationUtils's AnnotationDescriptor could contain attributes
from the wrong annotation if an inherited annotation shadowed a locally
declared composed annotation.

This commit addresses this issue by invoking the new
getAnnotationAttributes() method in AnnotatedElementUtils that provides
a flag to control whether superclasses should be searched -- which
coincidentally processes local annotations before searching the class
hierarchy.

Issue: SPR-12749, SPR-11598
pull/783/head
Sam Brannen 10 years ago
parent
commit
69b0791926
  1. 6
      spring-test/src/main/java/org/springframework/test/util/MetaAnnotationUtils.java
  2. 28
      spring-test/src/test/java/org/springframework/test/util/MetaAnnotationUtilsTests.java

6
spring-test/src/main/java/org/springframework/test/util/MetaAnnotationUtils.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -286,8 +286,8 @@ public abstract class MetaAnnotationUtils { @@ -286,8 +286,8 @@ public abstract class MetaAnnotationUtils {
this.declaringClass = declaringClass;
this.composedAnnotation = composedAnnotation;
this.annotation = annotation;
this.annotationAttributes = AnnotatedElementUtils.getAnnotationAttributes(
rootDeclaringClass, annotation.annotationType().getName());
this.annotationAttributes = AnnotatedElementUtils.getAnnotationAttributes(rootDeclaringClass,
annotation.annotationType().getName(), true, true, false, false);
}
public Class<?> getRootDeclaringClass() {

28
spring-test/src/test/java/org/springframework/test/util/MetaAnnotationUtilsTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -29,6 +29,8 @@ import org.springframework.core.annotation.Order; @@ -29,6 +29,8 @@ import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.util.MetaAnnotationUtils.AnnotationDescriptor;
import org.springframework.test.util.MetaAnnotationUtils.UntypedAnnotationDescriptor;
import org.springframework.transaction.annotation.Transactional;
import static org.junit.Assert.*;
@ -161,6 +163,22 @@ public class MetaAnnotationUtilsTests { @@ -161,6 +163,22 @@ public class MetaAnnotationUtilsTests {
assertNull(findAnnotationDescriptor(ClassWithMetaAnnotatedInterface.class, Component.class));
}
@Test
public void findAnnotationDescriptorForClassWithLocalMetaAnnotationAndAnnotatedSuperclass() {
AnnotationDescriptor<ContextConfiguration> descriptor = findAnnotationDescriptor(
MetaAnnotatedAndSuperAnnotatedContextConfigClass.class, ContextConfiguration.class);
assertNotNull("AnnotationDescriptor should not be null", descriptor);
assertEquals("rootDeclaringClass", MetaAnnotatedAndSuperAnnotatedContextConfigClass.class, descriptor.getRootDeclaringClass());
assertEquals("declaringClass", MetaConfig.class, descriptor.getDeclaringClass());
assertEquals("annotationType", ContextConfiguration.class, descriptor.getAnnotationType());
assertNotNull("composedAnnotation should not be null", descriptor.getComposedAnnotation());
assertEquals("composedAnnotationType", MetaConfig.class, descriptor.getComposedAnnotationType());
assertArrayEquals("configured classes", new Class[] { String.class },
descriptor.getAnnotationAttributes().getClassArray("classes"));
}
@Test
public void findAnnotationDescriptorForClassWithLocalMetaAnnotationAndMetaAnnotatedInterface() {
assertAtComponentOnComposedAnnotation(ClassWithLocalMetaAnnotationAndMetaAnnotatedInterface.class, "meta2",
@ -546,4 +564,12 @@ public class MetaAnnotationUtilsTests { @@ -546,4 +564,12 @@ public class MetaAnnotationUtilsTests {
static class SubNonInheritedAnnotationClass extends NonInheritedAnnotationClass {
}
@ContextConfiguration(classes = Number.class)
static class AnnotatedContextConfigClass {
}
@MetaConfig(classes = String.class)
static class MetaAnnotatedAndSuperAnnotatedContextConfigClass extends AnnotatedContextConfigClass {
}
}

Loading…
Cancel
Save