attributes = this.attributeMap.get(annotationType);
- AnnotationAttributes raw = (attributes == null ? null : attributes.get(0));
+ AnnotationAttributes raw = AnnotationReadingVisitorUtils.getMergedAnnotationAttributes(this.attributeMap,
+ annotationType);
return AnnotationReadingVisitorUtils.convertClassValues(this.classLoader, raw, classValuesAsString);
}
diff --git a/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationReadingVisitorUtils.java b/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationReadingVisitorUtils.java
index 50e7395978..88d47e7433 100644
--- a/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationReadingVisitorUtils.java
+++ b/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationReadingVisitorUtils.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2013 the original author or authors.
+ * Copyright 2002-2014 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.
@@ -16,10 +16,17 @@
package org.springframework.core.type.classreading;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
import java.util.Map;
+import java.util.Set;
import org.springframework.asm.Type;
import org.springframework.core.annotation.AnnotationAttributes;
+import org.springframework.util.LinkedMultiValueMap;
+
+import static org.springframework.core.annotation.AnnotationUtils.*;
/**
* Internal utility class used when reading annotations.
@@ -28,12 +35,13 @@ import org.springframework.core.annotation.AnnotationAttributes;
* @author Mark Fisher
* @author Costin Leau
* @author Phillip Webb
+ * @author Sam Brannen
* @since 4.0
*/
abstract class AnnotationReadingVisitorUtils {
- public static AnnotationAttributes convertClassValues(ClassLoader classLoader,
- AnnotationAttributes original, boolean classValuesAsString) {
+ public static AnnotationAttributes convertClassValues(ClassLoader classLoader, AnnotationAttributes original,
+ boolean classValuesAsString) {
if (original == null) {
return null;
@@ -44,26 +52,24 @@ abstract class AnnotationReadingVisitorUtils {
try {
Object value = entry.getValue();
if (value instanceof AnnotationAttributes) {
- value = convertClassValues(classLoader, (AnnotationAttributes) value,
- classValuesAsString);
+ value = convertClassValues(classLoader, (AnnotationAttributes) value, classValuesAsString);
}
else if (value instanceof AnnotationAttributes[]) {
- AnnotationAttributes[] values = (AnnotationAttributes[])value;
+ AnnotationAttributes[] values = (AnnotationAttributes[]) value;
for (int i = 0; i < values.length; i++) {
- values[i] = convertClassValues(classLoader, values[i],
- classValuesAsString);
+ values[i] = convertClassValues(classLoader, values[i], classValuesAsString);
}
}
else if (value instanceof Type) {
- value = (classValuesAsString ? ((Type) value).getClassName() :
- classLoader.loadClass(((Type) value).getClassName()));
+ value = (classValuesAsString ? ((Type) value).getClassName()
+ : classLoader.loadClass(((Type) value).getClassName()));
}
else if (value instanceof Type[]) {
Type[] array = (Type[]) value;
Object[] convArray = (classValuesAsString ? new String[array.length] : new Class>[array.length]);
for (int i = 0; i < array.length; i++) {
- convArray[i] = (classValuesAsString ? array[i].getClassName() :
- classLoader.loadClass(array[i].getClassName()));
+ convArray[i] = (classValuesAsString ? array[i].getClassName()
+ : classLoader.loadClass(array[i].getClassName()));
}
value = convArray;
}
@@ -83,10 +89,66 @@ abstract class AnnotationReadingVisitorUtils {
result.put(entry.getKey(), value);
}
catch (Exception ex) {
- // Class not found - can't resolve class reference in annotation attribute.
+ // Class not found - can't resolve class reference in annotation
+ // attribute.
}
}
return result;
}
+ /**
+ * Retrieve the merged attributes of the annotation of the given type, if any,
+ * from the supplied {@code attributeMap}.
+ * Annotation attribute values appearing lower in the annotation
+ * hierarchy (i.e., closer to the declaring class) will override those
+ * defined higher in the annotation hierarchy.
+ * @param attributeMap the map of annotation attribute lists, keyed by
+ * annotation type
+ * @param annotationType the annotation type to look for
+ * @return the merged annotation attributes; or {@code null} if no matching
+ * annotation is present in the {@code attributeMap}
+ * @since 4.0.3
+ */
+ public static AnnotationAttributes getMergedAnnotationAttributes(
+ LinkedMultiValueMap attributeMap, String annotationType) {
+
+ // Get the unmerged list of attributes for the target annotation.
+ List attributesList = attributeMap.get(annotationType);
+ if (attributesList == null || attributesList.isEmpty()) {
+ return null;
+ }
+
+ // To start with, we populate the results with all attribute values from the
+ // target annotation.
+ AnnotationAttributes results = attributesList.get(0);
+ Set supportedAttributeNames = results.keySet();
+
+ // Since the map is a LinkedMultiValueMap, we depend on the ordering of
+ // elements in the map and reverse the order of the keys in order to traverse
+ // "down" the meta-annotation hierarchy.
+ List annotationTypes = new ArrayList(attributeMap.keySet());
+ Collections.reverse(annotationTypes);
+
+ for (String currentAnnotationType : annotationTypes) {
+ if (!currentAnnotationType.startsWith("java.lang.annotation")) {
+ for (String attributeName : supportedAttributeNames) {
+ if (!VALUE.equals(attributeName)) {
+ List currentAttributes = attributeMap.get(currentAnnotationType);
+ if (currentAttributes != null && !currentAttributes.isEmpty()) {
+ Object value = currentAttributes.get(0).get(attributeName);
+ if (value != null) {
+ // Overwrite value from target annotation with the value
+ // from an attribute of the same name found lower in the
+ // meta-annotation hierarchy.
+ results.put(attributeName, value);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ return results;
+ }
+
}
diff --git a/spring-core/src/test/java/org/springframework/core/type/AnnotationMetadataTests.java b/spring-core/src/test/java/org/springframework/core/type/AnnotationMetadataTests.java
index 484f8207c5..c60594291a 100644
--- a/spring-core/src/test/java/org/springframework/core/type/AnnotationMetadataTests.java
+++ b/spring-core/src/test/java/org/springframework/core/type/AnnotationMetadataTests.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2013 the original author or authors.
+ * Copyright 2002-2014 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.
@@ -16,15 +16,6 @@
package org.springframework.core.type;
-import static org.hamcrest.CoreMatchers.instanceOf;
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.Matchers.equalTo;
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
-
-import java.io.IOException;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
@@ -43,6 +34,9 @@ import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
import org.springframework.stereotype.Component;
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
/**
* Unit tests demonstrating that the reflection-based {@link StandardAnnotationMetadata}
* and ASM-based {@code AnnotationMetadataReadingVisitor} produce identical output.
@@ -50,18 +44,19 @@ import org.springframework.stereotype.Component;
* @author Juergen Hoeller
* @author Chris Beams
* @author Phillip Webb
+ * @author Sam Brannen
*/
public class AnnotationMetadataTests {
@Test
- public void testStandardAnnotationMetadata() throws IOException {
+ public void standardAnnotationMetadata() throws Exception {
AnnotationMetadata metadata = new StandardAnnotationMetadata(AnnotatedComponent.class, true);
doTestAnnotationInfo(metadata);
doTestMethodAnnotationInfo(metadata);
}
@Test
- public void testAsmAnnotationMetadata() throws IOException {
+ public void asmAnnotationMetadata() throws Exception {
MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(AnnotatedComponent.class.getName());
AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();
@@ -77,14 +72,43 @@ public class AnnotationMetadataTests {
* 'true' as is done in the main test above.
*/
@Test
- public void testStandardAnnotationMetadata_nestedAnnotationsAsMap_false() throws IOException {
+ public void standardAnnotationMetadata_nestedAnnotationsAsMap_false() throws Exception {
AnnotationMetadata metadata = new StandardAnnotationMetadata(AnnotatedComponent.class);
AnnotationAttributes specialAttrs = (AnnotationAttributes) metadata.getAnnotationAttributes(SpecialAttr.class.getName());
- Annotation[] nestedAnnoArray = (Annotation[])specialAttrs.get("nestedAnnoArray");
+ Annotation[] nestedAnnoArray = (Annotation[]) specialAttrs.get("nestedAnnoArray");
assertThat(nestedAnnoArray[0], instanceOf(NestedAnno.class));
}
+ @Test
+ public void metaAnnotationOverridesUsingStandardAnnotationMetadata() {
+ AnnotationMetadata metadata = new StandardAnnotationMetadata(
+ ComposedConfigurationWithAttributeOverridesClass.class);
+
+ assertMetaAnnotationOverrides(metadata);
+ }
+
+ @Test
+ public void metaAnnotationOverridesUsingAnnotationMetadataReadingVisitor() throws Exception {
+ MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
+ MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(ComposedConfigurationWithAttributeOverridesClass.class.getName());
+ AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();
+
+ assertMetaAnnotationOverrides(metadata);
+ }
+
+ private void assertMetaAnnotationOverrides(AnnotationMetadata metadata) {
+ AnnotationAttributes attributes = (AnnotationAttributes) metadata.getAnnotationAttributes(
+ TestComponentScan.class.getName(), false);
+ String[] basePackages = attributes.getStringArray("basePackages");
+ assertThat("length of basePackages[]", basePackages.length, is(1));
+ assertThat("basePackages[0]", basePackages[0], is("org.example.componentscan"));
+ String[] value = attributes.getStringArray("value");
+ assertThat("length of value[]", value.length, is(0));
+ Class>[] basePackageClasses = attributes.getClassArray("basePackageClasses");
+ assertThat("length of basePackageClasses[]", basePackageClasses.length, is(0));
+ }
+
private void doTestAnnotationInfo(AnnotationMetadata metadata) {
assertThat(metadata.getClassName(), is(AnnotatedComponent.class.getName()));
assertThat(metadata.isInterface(), is(false));
@@ -127,58 +151,59 @@ public class AnnotationMetadataTests {
AnnotationAttributes nestedAnno = specialAttrs.getAnnotation("nestedAnno");
assertThat("na", is(nestedAnno.getString("value")));
assertTrue(nestedAnno.getEnum("anEnum").equals(SomeEnum.LABEL1));
- assertArrayEquals(new Class[]{String.class}, (Class[])nestedAnno.get("classArray"));
+ assertArrayEquals(new Class[] { String.class }, (Class[]) nestedAnno.get("classArray"));
AnnotationAttributes[] nestedAnnoArray = specialAttrs.getAnnotationArray("nestedAnnoArray");
assertThat(nestedAnnoArray.length, is(2));
assertThat(nestedAnnoArray[0].getString("value"), is("default"));
assertTrue(nestedAnnoArray[0].getEnum("anEnum").equals(SomeEnum.DEFAULT));
- assertArrayEquals(new Class[]{Void.class}, (Class[])nestedAnnoArray[0].get("classArray"));
+ assertArrayEquals(new Class[] { Void.class }, (Class[]) nestedAnnoArray[0].get("classArray"));
assertThat(nestedAnnoArray[1].getString("value"), is("na1"));
assertTrue(nestedAnnoArray[1].getEnum("anEnum").equals(SomeEnum.LABEL2));
- assertArrayEquals(new Class[]{Number.class}, (Class[])nestedAnnoArray[1].get("classArray"));
- assertArrayEquals(new Class[]{Number.class}, nestedAnnoArray[1].getClassArray("classArray"));
+ assertArrayEquals(new Class[] { Number.class }, (Class[]) nestedAnnoArray[1].get("classArray"));
+ assertArrayEquals(new Class[] { Number.class }, nestedAnnoArray[1].getClassArray("classArray"));
AnnotationAttributes optional = specialAttrs.getAnnotation("optional");
assertThat(optional.getString("value"), is("optional"));
assertTrue(optional.getEnum("anEnum").equals(SomeEnum.DEFAULT));
- assertArrayEquals(new Class[]{Void.class}, (Class[])optional.get("classArray"));
- assertArrayEquals(new Class[]{Void.class}, optional.getClassArray("classArray"));
+ assertArrayEquals(new Class[] { Void.class }, (Class[]) optional.get("classArray"));
+ assertArrayEquals(new Class[] { Void.class }, optional.getClassArray("classArray"));
AnnotationAttributes[] optionalArray = specialAttrs.getAnnotationArray("optionalArray");
assertThat(optionalArray.length, is(1));
assertThat(optionalArray[0].getString("value"), is("optional"));
assertTrue(optionalArray[0].getEnum("anEnum").equals(SomeEnum.DEFAULT));
- assertArrayEquals(new Class[]{Void.class}, (Class[])optionalArray[0].get("classArray"));
- assertArrayEquals(new Class[]{Void.class}, optionalArray[0].getClassArray("classArray"));
+ assertArrayEquals(new Class[] { Void.class }, (Class[]) optionalArray[0].get("classArray"));
+ assertArrayEquals(new Class[] { Void.class }, optionalArray[0].getClassArray("classArray"));
assertEquals("direct", metadata.getAnnotationAttributes(DirectAnnotation.class.getName()).get("value"));
allMeta = metadata.getAllAnnotationAttributes(DirectAnnotation.class.getName()).get("value");
assertThat(new HashSet