Skip to content

Commit 1f52f09

Browse files
philwebbcbeams
authored andcommitted
Backport "Ensure @imports are processed in correct order"
Issue: SPR-9925 Backport-Commit: 3416e05 Conflicts: spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java
1 parent e9616b7 commit 1f52f09

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

org.springframework.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;
@@ -220,10 +223,7 @@ protected AnnotationMetadata doProcessConfigurationClass(
220223
}
221224

222225
// 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);
227227

228228
// process any @ImportResource annotations
229229
if (metadata.isAnnotated(ImportResource.class.getName())) {
@@ -285,23 +285,31 @@ else if (superclass.startsWith("java")) {
285285
* @return a set of all {@link Import#value() import values} or {@code null}
286286
* @throws IOException if there is any problem reading metadata from the named class
287287
*/
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,
289299
Set<String> visited) throws IOException {
290300
if (visited.add(className)) {
291301
AnnotationMetadata metadata = metadataReaderFactory.getMetadataReader(className).getAnnotationMetadata();
302+
for (String annotationType : metadata.getAnnotationTypes()) {
303+
collectImports(annotationType, imports, visited);
304+
}
292305
Map<String, Object> attributes = metadata.getAnnotationAttributes(Import.class.getName(), true);
293306
if (attributes != null) {
294307
String[] value = (String[]) attributes.get("value");
295308
if (value != null && value.length > 0) {
296-
imports = (imports == null ? new LinkedHashSet<String>() : imports);
297309
imports.addAll(Arrays.asList(value));
298310
}
299311
}
300-
for (String annotationType : metadata.getAnnotationTypes()) {
301-
getImports(annotationType, imports, visited);
302-
}
303312
}
304-
return imports;
305313
}
306314

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

0 commit comments

Comments
 (0)