Skip to content

Commit 3416e05

Browse files
philwebbcbeams
authored andcommitted
Ensure @imports are processed in correct order
Issue: SPR-9925
1 parent af56b3b commit 3416e05

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.HashSet;
2525
import java.util.Iterator;
2626
import java.util.LinkedHashSet;
27+
import java.util.LinkedList;
2728
import java.util.Map;
2829
import java.util.Set;
2930
import java.util.Stack;
@@ -72,6 +73,8 @@
7273
*/
7374
class ConfigurationClassParser {
7475

76+
private static final String[] EMPTY_IMPORTS = {};
77+
7578
private final MetadataReaderFactory metadataReaderFactory;
7679

7780
private final ProblemReporter problemReporter;
@@ -221,10 +224,7 @@ protected AnnotationMetadata doProcessConfigurationClass(
221224
}
222225

223226
// 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);
228228

229229
// process any @ImportResource annotations
230230
if (metadata.isAnnotated(ImportResource.class.getName())) {
@@ -276,23 +276,31 @@ protected AnnotationMetadata doProcessConfigurationClass(
276276
* @return a set of all {@link Import#value() import values} or {@code null}
277277
* @throws IOException if there is any problem reading metadata from the named class
278278
*/
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,
280290
Set<String> visited) throws IOException {
281291
if (visited.add(className)) {
282292
AnnotationMetadata metadata = metadataReaderFactory.getMetadataReader(className).getAnnotationMetadata();
293+
for (String annotationType : metadata.getAnnotationTypes()) {
294+
collectImports(annotationType, imports, visited);
295+
}
283296
Map<String, Object> attributes = metadata.getAnnotationAttributes(Import.class.getName(), true);
284297
if (attributes != null) {
285298
String[] value = (String[]) attributes.get("value");
286299
if (value != null && value.length > 0) {
287-
imports = (imports == null ? new LinkedHashSet<String>() : imports);
288300
imports.addAll(Arrays.asList(value));
289301
}
290302
}
291-
for (String annotationType : metadata.getAnnotationTypes()) {
292-
getImports(annotationType, imports, visited);
293-
}
294303
}
295-
return imports;
296304
}
297305

298306
private void processImport(ConfigurationClass configClass, String[] classesToImport, boolean checkForCircularImports) throws IOException {

0 commit comments

Comments
 (0)