From 6837111bda5e4df9bdfa617c78c1ea32ced7ab5c Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Sun, 9 Oct 2011 20:32:21 +0000 Subject: [PATCH] Refactor AnnotationUtils#findAllAnnotationAttributes Remove all convenience variants of #findAllAnnotationAttributes and refactor the remaining method to accept a MetadataReaderFactory instead of creating its own SimpleMetadataReaderFactory internally. This allows clients to use non-default class loaders as well as customize the particular MetadataReaderFactory to be used (e.g. 'simple' vs 'caching', etc). Issue: SPR-8752 --- .../annotation/ConfigurationClassParser.java | 2 +- .../core/annotation/AnnotationUtils.java | 34 ++++++------------- .../core/annotation/AnnotationUtilsTests.java | 5 ++- 3 files changed, 16 insertions(+), 25 deletions(-) diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java index 306b957e78..be90a686eb 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java +++ b/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java @@ -210,7 +210,7 @@ class ConfigurationClassParser { // process any @Import annotations List> allImportAttribs = - AnnotationUtils.findAllAnnotationAttributes(Import.class, metadata.getClassName(), true); + AnnotationUtils.findAllAnnotationAttributes(Import.class, metadata.getClassName(), true, metadataReaderFactory); for (Map importAttribs : allImportAttribs) { processImport(configClass, (String[]) importAttribs.get("value"), true); } diff --git a/org.springframework.core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java b/org.springframework.core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java index 8986ab741f..c3584a5e9f 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java +++ b/org.springframework.core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java @@ -30,6 +30,7 @@ import java.util.WeakHashMap; import org.springframework.core.BridgeMethodResolver; import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.classreading.MetadataReader; +import org.springframework.core.type.classreading.MetadataReaderFactory; import org.springframework.core.type.classreading.SimpleMetadataReaderFactory; import org.springframework.util.Assert; @@ -354,22 +355,24 @@ public abstract class AnnotationUtils { } /** - * Return a list of attribute maps for all declarations of the given target annotation - * on the given annotated class. Meta annotations are ordered first in the list, and if - * the target annotation is declared directly on the class, its map of attributes will be + * Return a list of attribute maps for all declarations of the given annotation + * on the given annotated class using the given MetadataReaderFactory to introspect + * annotation metadata. Meta-annotations are ordered first in the list, and if the + * target annotation is declared directly on the class, its map of attributes will be * ordered last in the list. * @param targetAnnotation the annotation to search for, both locally and as a meta-annotation - * @param annotatedClassName the class to search + * @param annotatedClassName the class to inspect * @param classValuesAsString whether class attributes should be returned as strings + * @param metadataReaderFactory factory used to create metadata readers for each type * @since 3.1 - * @see {@link #findAllAnnotationAttributes(Class, String)} */ public static List> findAllAnnotationAttributes( - Class targetAnnotation, String annotatedClassName, boolean classValuesAsString) throws IOException { + Class targetAnnotation, String annotatedClassName, + boolean classValuesAsString, MetadataReaderFactory metadataReaderFactory) throws IOException { List> allAttribs = new ArrayList>(); - MetadataReader reader = new SimpleMetadataReaderFactory().getMetadataReader(annotatedClassName); + MetadataReader reader = metadataReaderFactory.getMetadataReader(annotatedClassName); AnnotationMetadata metadata = reader.getAnnotationMetadata(); String targetAnnotationType = targetAnnotation.getName(); @@ -377,7 +380,7 @@ public abstract class AnnotationUtils { if (annotationType.equals(targetAnnotationType)) { continue; } - MetadataReader metaReader = new SimpleMetadataReaderFactory().getMetadataReader(annotationType); + MetadataReader metaReader = metadataReaderFactory.getMetadataReader(annotationType); Map targetAttribs = metaReader.getAnnotationMetadata().getAnnotationAttributes(targetAnnotationType, classValuesAsString); if (targetAttribs != null) { @@ -394,21 +397,6 @@ public abstract class AnnotationUtils { return allAttribs; } - /** - * Return a list of attribute maps for all declarations of the given target annotation - * on the given annotated class. Meta annotations are ordered first in the list, and if - * the target annotation is declared directly on the class, its map of attributes will be - * ordered last in the list. - * @param targetAnnotation the annotation to search for, both locally and as a meta-annotation - * @param annotatedClassName the class to search - * @since 3.1 - * @see {@link #findAllAnnotationAttributes(Class, String, boolean)} - */ - public static List> findAllAnnotationAttributes( - Class targetAnnotation, String annotatedClassName) throws IOException { - return findAllAnnotationAttributes(targetAnnotation, annotatedClassName, false); - } - /** * Retrieve the value of the "value" attribute of a * single-element Annotation, given an annotation instance. diff --git a/org.springframework.core/src/test/java/org/springframework/core/annotation/AnnotationUtilsTests.java b/org.springframework.core/src/test/java/org/springframework/core/annotation/AnnotationUtilsTests.java index 4e65e232b2..442f31a603 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/annotation/AnnotationUtilsTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/annotation/AnnotationUtilsTests.java @@ -39,6 +39,7 @@ import java.util.Map; import org.junit.Test; import org.springframework.core.Ordered; +import org.springframework.core.type.classreading.SimpleMetadataReaderFactory; import org.springframework.stereotype.Component; /** @@ -221,7 +222,9 @@ public class AnnotationUtilsTests { @Test public void findAllComponentAnnotationAttributes() throws IOException { List> allAttribs = - AnnotationUtils.findAllAnnotationAttributes(Component.class, HasLocalAndMetaComponentAnnotation.class.getName()); + AnnotationUtils.findAllAnnotationAttributes(Component.class, + HasLocalAndMetaComponentAnnotation.class.getName(), false, + new SimpleMetadataReaderFactory()); Object value = null; for (Map attribs : allAttribs) {