|
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;
|
@@ -224,7 +221,10 @@ protected AnnotationMetadata doProcessConfigurationClass(
|
224 | 221 | }
|
225 | 222 |
|
226 | 223 | // process any @Import annotations
|
227 |
| - processImport(configClass, getImports(metadata.getClassName()), true); |
| 224 | + Set<String> imports = getImports(metadata.getClassName(), null, new HashSet<String>()); |
| 225 | + if (imports != null && !imports.isEmpty()) { |
| 226 | + processImport(configClass, imports.toArray(new String[imports.size()]), true); |
| 227 | + } |
228 | 228 |
|
229 | 229 | // process any @ImportResource annotations
|
230 | 230 | if (metadata.isAnnotated(ImportResource.class.getName())) {
|
@@ -276,31 +276,23 @@ protected AnnotationMetadata doProcessConfigurationClass(
|
276 | 276 | * @return a set of all {@link Import#value() import values} or {@code null}
|
277 | 277 | * @throws IOException if there is any problem reading metadata from the named class
|
278 | 278 | */
|
279 |
| - private String[] getImports(String className) throws IOException { |
280 |
| - LinkedList<String> imports = new LinkedList<String>(); |
281 |
| - collectImports(className, imports, new HashSet<String>()); |
282 |
| - if(imports == null || imports.isEmpty()) { |
283 |
| - return EMPTY_IMPORTS; |
284 |
| - } |
285 |
| - LinkedHashSet<String> uniqueImports = new LinkedHashSet<String>(imports); |
286 |
| - return uniqueImports.toArray(new String[uniqueImports.size()]); |
287 |
| - } |
288 |
| - |
289 |
| - private void collectImports(String className, LinkedList<String> imports, |
| 279 | + private Set<String> getImports(String className, Set<String> imports, |
290 | 280 | Set<String> visited) throws IOException {
|
291 | 281 | if (visited.add(className)) {
|
292 | 282 | AnnotationMetadata metadata = metadataReaderFactory.getMetadataReader(className).getAnnotationMetadata();
|
293 | 283 | for (String annotationType : metadata.getAnnotationTypes()) {
|
294 |
| - collectImports(annotationType, imports, visited); |
| 284 | + imports = getImports(annotationType, imports, visited); |
295 | 285 | }
|
296 | 286 | Map<String, Object> attributes = metadata.getAnnotationAttributes(Import.class.getName(), true);
|
297 | 287 | if (attributes != null) {
|
298 | 288 | String[] value = (String[]) attributes.get("value");
|
299 | 289 | if (value != null && value.length > 0) {
|
| 290 | + imports = (imports == null ? new LinkedHashSet<String>() : imports); |
300 | 291 | imports.addAll(Arrays.asList(value));
|
301 | 292 | }
|
302 | 293 | }
|
303 | 294 | }
|
| 295 | + return imports; |
304 | 296 | }
|
305 | 297 |
|
306 | 298 | private void processImport(ConfigurationClass configClass, String[] classesToImport, boolean checkForCircularImports) throws IOException {
|
|
0 commit comments