Skip to content

Commit 5331499

Browse files
committed
Do not further process imports of Object.class
This commit filters out source classes that didn't pass a predicate filter so that they are no longer considered. Closes gh-27080
1 parent e011d4f commit 5331499

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,10 @@ SourceClass asSourceClass(@Nullable Class<?> classType, Predicate<String> filter
670670
private Collection<SourceClass> asSourceClasses(String[] classNames, Predicate<String> filter) throws IOException {
671671
List<SourceClass> annotatedClasses = new ArrayList<>(classNames.length);
672672
for (String className : classNames) {
673-
annotatedClasses.add(asSourceClass(className, filter));
673+
SourceClass sourceClass = asSourceClass(className, filter);
674+
if (this.objectSourceClass != sourceClass) {
675+
annotatedClasses.add(sourceClass);
676+
}
674677
}
675678
return annotatedClasses;
676679
}

spring-context/src/test/java/org/springframework/context/annotation/ImportSelectorTests.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@ void importSelectors() {
9191
ordered.verify(beanFactory).registerBeanDefinition(eq("c"), any());
9292
}
9393

94+
@Test
95+
void filteredImportSelector() {
96+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
97+
context.register(FilteredConfig.class);
98+
context.refresh();
99+
String[] beanNames = context.getBeanFactory().getBeanDefinitionNames();
100+
assertThat(beanNames).endsWith("importSelectorTests.FilteredConfig",
101+
ImportedSelector2.class.getName(), "b");
102+
assertThat(beanNames).doesNotContain(Object.class.getName());
103+
}
104+
94105
@Test
95106
void invokeAwareMethodsInImportSelector() {
96107
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AwareConfig.class);
@@ -274,6 +285,25 @@ public String[] selectImports(AnnotationMetadata importingClassMetadata) {
274285
}
275286
}
276287

288+
@Configuration
289+
@Import(FilteredImportSelector.class)
290+
public static class FilteredConfig {
291+
}
292+
293+
public static class FilteredImportSelector implements ImportSelector {
294+
295+
@Override
296+
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
297+
return new String[] { ImportedSelector1.class.getName(), ImportedSelector2.class.getName(), ImportedSelector3.class.getName() };
298+
}
299+
300+
@Override
301+
public Predicate<String> getExclusionFilter() {
302+
return (className -> className.equals(ImportedSelector1.class.getName()) ||
303+
className.equals(ImportedSelector3.class.getName()));
304+
}
305+
}
306+
277307

278308
public static class DeferredImportSelector1 implements DeferredImportSelector, Ordered {
279309

@@ -320,6 +350,15 @@ public String b() {
320350
}
321351
}
322352

353+
@Configuration
354+
public static class ImportedSelector3 {
355+
356+
@Bean
357+
public String c() {
358+
return "c";
359+
}
360+
}
361+
323362

324363
@Configuration
325364
public static class DeferredImportedSelector1 {

0 commit comments

Comments
 (0)