From d6d169ac5614ca10aa7d5bade5a695f599823d8a Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 29 Nov 2011 01:16:09 +0000 Subject: [PATCH] resolved package dependency tangles --- .../annotation/ConfigurationClassParser.java | 53 ++++++++++++++++--- .../core/annotation/AnnotationUtils.java | 47 ---------------- .../{ => support}/ResourcePropertySource.java | 6 +-- .../core/annotation/AnnotationUtilsTests.java | 15 ------ .../ResourcePropertySourceTests.java | 14 ++--- 5 files changed, 57 insertions(+), 78 deletions(-) rename org.springframework.core/src/main/java/org/springframework/core/io/{ => support}/ResourcePropertySource.java (95%) rename org.springframework.core/src/test/java/org/springframework/core/io/{ => support}/ResourcePropertySourceTests.java (92%) 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 585aa9efba4..00528aa87d7 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 @@ -16,9 +16,9 @@ package org.springframework.context.annotation; -import static org.springframework.context.annotation.ConfigurationClassUtils.isConfigurationCandidate; - import java.io.IOException; +import java.lang.annotation.Annotation; +import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; @@ -35,11 +35,10 @@ import org.springframework.beans.factory.parsing.Location; import org.springframework.beans.factory.parsing.Problem; import org.springframework.beans.factory.parsing.ProblemReporter; import org.springframework.beans.factory.support.BeanDefinitionRegistry; -import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.env.Environment; import org.springframework.core.env.PropertySource; import org.springframework.core.io.ResourceLoader; -import org.springframework.core.io.ResourcePropertySource; +import org.springframework.core.io.support.ResourcePropertySource; import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.MethodMetadata; import org.springframework.core.type.StandardAnnotationMetadata; @@ -167,7 +166,7 @@ class ConfigurationClassParser { for (String memberClassName : metadata.getMemberClassNames()) { MetadataReader reader = this.metadataReaderFactory.getMetadataReader(memberClassName); AnnotationMetadata memberClassMetadata = reader.getAnnotationMetadata(); - if (isConfigurationCandidate(memberClassMetadata)) { + if (ConfigurationClassUtils.isConfigurationCandidate(memberClassMetadata)) { processConfigurationClass(new ConfigurationClass(reader, null)); } } @@ -204,7 +203,7 @@ class ConfigurationClassParser { // process any @Import annotations List> allImportAttribs = - AnnotationUtils.findAllAnnotationAttributes(Import.class, metadata.getClassName(), true, metadataReaderFactory); + findAllAnnotationAttributes(Import.class, metadata.getClassName(), true); for (Map importAttribs : allImportAttribs) { processImport(configClass, (String[]) importAttribs.get("value"), true); } @@ -229,6 +228,48 @@ class ConfigurationClassParser { } } + + /** + * 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 inspect + * @param classValuesAsString whether class attributes should be returned as strings + */ + private List> findAllAnnotationAttributes( + Class targetAnnotation, String annotatedClassName, + boolean classValuesAsString) throws IOException { + + List> allAttribs = new ArrayList>(); + + MetadataReader reader = this.metadataReaderFactory.getMetadataReader(annotatedClassName); + AnnotationMetadata metadata = reader.getAnnotationMetadata(); + String targetAnnotationType = targetAnnotation.getName(); + + for (String annotationType : metadata.getAnnotationTypes()) { + if (annotationType.equals(targetAnnotationType)) { + continue; + } + MetadataReader metaReader = this.metadataReaderFactory.getMetadataReader(annotationType); + Map targetAttribs = + metaReader.getAnnotationMetadata().getAnnotationAttributes(targetAnnotationType, classValuesAsString); + if (targetAttribs != null) { + allAttribs.add(targetAttribs); + } + } + + Map localAttribs = + metadata.getAnnotationAttributes(targetAnnotationType, classValuesAsString); + if (localAttribs != null) { + allAttribs.add(localAttribs); + } + + return allAttribs; + } + private void processImport(ConfigurationClass configClass, String[] classesToImport, boolean checkForCircularImports) throws IOException { if (checkForCircularImports && this.importStack.contains(configClass)) { this.problemReporter.error(new CircularImportProblem(configClass, this.importStack, configClass.getMetadata())); 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 c3584a5e9fa..c6ef587216f 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 @@ -28,10 +28,6 @@ import java.util.Map; 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,49 +350,6 @@ public abstract class AnnotationUtils { return attrs; } - /** - * 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 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 - */ - public static List> findAllAnnotationAttributes( - Class targetAnnotation, String annotatedClassName, - boolean classValuesAsString, MetadataReaderFactory metadataReaderFactory) throws IOException { - - List> allAttribs = new ArrayList>(); - - MetadataReader reader = metadataReaderFactory.getMetadataReader(annotatedClassName); - AnnotationMetadata metadata = reader.getAnnotationMetadata(); - String targetAnnotationType = targetAnnotation.getName(); - - for (String annotationType : metadata.getAnnotationTypes()) { - if (annotationType.equals(targetAnnotationType)) { - continue; - } - MetadataReader metaReader = metadataReaderFactory.getMetadataReader(annotationType); - Map targetAttribs = - metaReader.getAnnotationMetadata().getAnnotationAttributes(targetAnnotationType, classValuesAsString); - if (targetAttribs != null) { - allAttribs.add(targetAttribs); - } - } - - Map localAttribs = - metadata.getAnnotationAttributes(targetAnnotationType, classValuesAsString); - if (localAttribs != null) { - allAttribs.add(localAttribs); - } - - return allAttribs; - } - /** * Retrieve the value of the "value" attribute of a * single-element Annotation, given an annotation instance. diff --git a/org.springframework.core/src/main/java/org/springframework/core/io/ResourcePropertySource.java b/org.springframework.core/src/main/java/org/springframework/core/io/support/ResourcePropertySource.java similarity index 95% rename from org.springframework.core/src/main/java/org/springframework/core/io/ResourcePropertySource.java rename to org.springframework.core/src/main/java/org/springframework/core/io/support/ResourcePropertySource.java index d86f53f1fda..f67a4579705 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/io/ResourcePropertySource.java +++ b/org.springframework.core/src/main/java/org/springframework/core/io/support/ResourcePropertySource.java @@ -14,20 +14,20 @@ * limitations under the License. */ -package org.springframework.core.io; +package org.springframework.core.io.support; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.springframework.core.env.PropertiesPropertySource; -import org.springframework.core.io.support.PathMatchingResourcePatternResolver; +import org.springframework.core.io.Resource; import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; /** * Subclass of {@link PropertiesPropertySource} that loads a {@link Properties} - * object from a given {@link Resource} or resource location such as + * object from a given {@link org.springframework.core.io.Resource} or resource location such as * {@code "classpath:/com/myco/foo.properties"} or {@code "file:/path/to/file.properties"}. * * @author Chris Beams 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 442f31a603d..32e35cbd634 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 @@ -219,21 +219,6 @@ public class AnnotationUtilsTests { assertNotNull(order); } - @Test - public void findAllComponentAnnotationAttributes() throws IOException { - List> allAttribs = - AnnotationUtils.findAllAnnotationAttributes(Component.class, - HasLocalAndMetaComponentAnnotation.class.getName(), false, - new SimpleMetadataReaderFactory()); - - Object value = null; - for (Map attribs : allAttribs) { - value = attribs.get("value"); - } - - assertThat(allAttribs.size(), is(3)); - assertEquals("local", value); - } @Component(value="meta1") @Retention(RetentionPolicy.RUNTIME) diff --git a/org.springframework.core/src/test/java/org/springframework/core/io/ResourcePropertySourceTests.java b/org.springframework.core/src/test/java/org/springframework/core/io/support/ResourcePropertySourceTests.java similarity index 92% rename from org.springframework.core/src/test/java/org/springframework/core/io/ResourcePropertySourceTests.java rename to org.springframework.core/src/test/java/org/springframework/core/io/support/ResourcePropertySourceTests.java index 0a7f126a6a3..1b77a9f542c 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/io/ResourcePropertySourceTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/io/support/ResourcePropertySourceTests.java @@ -14,18 +14,18 @@ * limitations under the License. */ -package org.springframework.core.io; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; +package org.springframework.core.io.support; import java.io.IOException; -import org.easymock.internal.matchers.StartsWith; import org.junit.Test; + import org.springframework.core.env.PropertySource; +import org.springframework.core.io.ByteArrayResource; +import org.springframework.core.io.ClassPathResource; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; /** * Unit tests for {@link ResourcePropertySource}.