|
24 | 24 | import java.util.HashSet;
|
25 | 25 | import java.util.Iterator;
|
26 | 26 | import java.util.LinkedHashSet;
|
| 27 | +import java.util.LinkedList; |
27 | 28 | import java.util.Map;
|
28 | 29 | import java.util.Set;
|
29 | 30 | import java.util.Stack;
|
|
72 | 73 | */
|
73 | 74 | class ConfigurationClassParser {
|
74 | 75 |
|
| 76 | + private static final String[] EMPTY_IMPORTS = {}; |
| 77 | + |
75 | 78 | private final MetadataReaderFactory metadataReaderFactory;
|
76 | 79 |
|
77 | 80 | private final ProblemReporter problemReporter;
|
@@ -220,10 +223,7 @@ protected AnnotationMetadata doProcessConfigurationClass(
|
220 | 223 | }
|
221 | 224 |
|
222 | 225 | // process any @Import annotations
|
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 |
| - } |
| 226 | + processImport(configClass, getImports(metadata.getClassName()), true); |
227 | 227 |
|
228 | 228 | // process any @ImportResource annotations
|
229 | 229 | if (metadata.isAnnotated(ImportResource.class.getName())) {
|
@@ -285,23 +285,31 @@ 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 Set<String> getImports(String className, Set<String> imports, |
| 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, |
289 | 299 | Set<String> visited) throws IOException {
|
290 | 300 | if (visited.add(className)) {
|
291 | 301 | AnnotationMetadata metadata = metadataReaderFactory.getMetadataReader(className).getAnnotationMetadata();
|
| 302 | + for (String annotationType : metadata.getAnnotationTypes()) { |
| 303 | + collectImports(annotationType, imports, visited); |
| 304 | + } |
292 | 305 | Map<String, Object> attributes = metadata.getAnnotationAttributes(Import.class.getName(), true);
|
293 | 306 | if (attributes != null) {
|
294 | 307 | String[] value = (String[]) attributes.get("value");
|
295 | 308 | if (value != null && value.length > 0) {
|
296 |
| - imports = (imports == null ? new LinkedHashSet<String>() : imports); |
297 | 309 | imports.addAll(Arrays.asList(value));
|
298 | 310 | }
|
299 | 311 | }
|
300 |
| - for (String annotationType : metadata.getAnnotationTypes()) { |
301 |
| - getImports(annotationType, imports, visited); |
302 |
| - } |
303 | 312 | }
|
304 |
| - return imports; |
305 | 313 | }
|
306 | 314 |
|
307 | 315 | private void processImport(ConfigurationClass configClass, String[] classesToImport, boolean checkForCircularImports) throws IOException {
|
|
0 commit comments