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