|
16 | 16 |
|
17 | 17 | package org.springframework.context.annotation;
|
18 | 18 |
|
| 19 | +import static org.springframework.context.annotation.MetadataUtils.attributesFor; |
| 20 | + |
19 | 21 | import java.io.IOException;
|
20 | 22 | import java.util.Arrays;
|
21 | 23 | import java.util.Collections;
|
|
24 | 26 | import java.util.HashSet;
|
25 | 27 | import java.util.Iterator;
|
26 | 28 | import java.util.LinkedHashSet;
|
27 |
| -import java.util.LinkedList; |
28 | 29 | import java.util.Map;
|
29 | 30 | import java.util.Set;
|
30 | 31 | import java.util.Stack;
|
|
51 | 52 | import org.springframework.core.type.filter.AssignableTypeFilter;
|
52 | 53 | import org.springframework.util.StringUtils;
|
53 | 54 |
|
54 |
| -import static org.springframework.context.annotation.MetadataUtils.*; |
55 |
| - |
56 | 55 | /**
|
57 | 56 | * Parses a {@link Configuration} class definition, populating a collection of
|
58 | 57 | * {@link ConfigurationClass} objects (parsing a single Configuration class may result in
|
|
73 | 72 | */
|
74 | 73 | class ConfigurationClassParser {
|
75 | 74 |
|
76 |
| - private static final String[] EMPTY_IMPORTS = {}; |
77 |
| - |
78 | 75 | private final MetadataReaderFactory metadataReaderFactory;
|
79 | 76 |
|
80 | 77 | private final ProblemReporter problemReporter;
|
@@ -223,7 +220,10 @@ protected AnnotationMetadata doProcessConfigurationClass(
|
223 | 220 | }
|
224 | 221 |
|
225 | 222 | // process any @Import annotations
|
226 |
| - processImport(configClass, getImports(metadata.getClassName()), true); |
| 223 | + Set<String> imports = getImports(metadata.getClassName(), null, new HashSet<String>()); |
| 224 | + if (imports != null && !imports.isEmpty()) { |
| 225 | + processImport(configClass, imports.toArray(new String[imports.size()]), true); |
| 226 | + } |
227 | 227 |
|
228 | 228 | // process any @ImportResource annotations
|
229 | 229 | if (metadata.isAnnotated(ImportResource.class.getName())) {
|
@@ -285,31 +285,23 @@ else if (superclass.startsWith("java")) {
|
285 | 285 | * @return a set of all {@link Import#value() import values} or {@code null}
|
286 | 286 | * @throws IOException if there is any problem reading metadata from the named class
|
287 | 287 | */
|
288 |
| - private String[] getImports(String className) throws IOException { |
289 |
| - LinkedList<String> imports = new LinkedList<String>(); |
290 |
| - collectImports(className, imports, new HashSet<String>()); |
291 |
| - if(imports == null || imports.isEmpty()) { |
292 |
| - return EMPTY_IMPORTS; |
293 |
| - } |
294 |
| - LinkedHashSet<String> uniqueImports = new LinkedHashSet<String>(imports); |
295 |
| - return uniqueImports.toArray(new String[uniqueImports.size()]); |
296 |
| - } |
297 |
| - |
298 |
| - private void collectImports(String className, LinkedList<String> imports, |
| 288 | + private Set<String> getImports(String className, Set<String> imports, |
299 | 289 | Set<String> visited) throws IOException {
|
300 | 290 | if (visited.add(className)) {
|
301 | 291 | AnnotationMetadata metadata = metadataReaderFactory.getMetadataReader(className).getAnnotationMetadata();
|
302 | 292 | for (String annotationType : metadata.getAnnotationTypes()) {
|
303 |
| - collectImports(annotationType, imports, visited); |
| 293 | + imports = getImports(annotationType, imports, visited); |
304 | 294 | }
|
305 | 295 | Map<String, Object> attributes = metadata.getAnnotationAttributes(Import.class.getName(), true);
|
306 | 296 | if (attributes != null) {
|
307 | 297 | String[] value = (String[]) attributes.get("value");
|
308 | 298 | if (value != null && value.length > 0) {
|
| 299 | + imports = (imports == null ? new LinkedHashSet<String>() : imports); |
309 | 300 | imports.addAll(Arrays.asList(value));
|
310 | 301 | }
|
311 | 302 | }
|
312 | 303 | }
|
| 304 | + return imports; |
313 | 305 | }
|
314 | 306 |
|
315 | 307 | private void processImport(ConfigurationClass configClass, String[] classesToImport, boolean checkForCircularImports) throws IOException {
|
|
0 commit comments