Skip to content

Commit

Permalink
Allow imported auto-configuration to be missing
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed Aug 1, 2023
1 parent 523d86d commit 626a114
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,13 @@
*/
Class<?>[] exclude() default {};

/**
* Whether to ignore classes listed in the {@code .imports} file that are not on the
* classpath.
* @since 3.1.3
* @return whether to ignore missing classes
* @see #classes
*/
boolean ignoreMissingClasses() default false;

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,6 +32,7 @@
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.ClassUtils;
import org.springframework.util.LinkedMultiValueMap;
Expand Down Expand Up @@ -92,13 +93,24 @@ private Collection<String> getConfigurationsForAnnotation(Class<?> source, Annot
if (classes.length > 0) {
return Arrays.asList(classes);
}
return loadFactoryNames(source);
Collection<String> factoryNames = loadFactoryNames(source);
boolean ignoreMissingClasses = (boolean) AnnotationUtils.getAnnotationAttributes(annotation, true)
.get("ignoreMissingClasses");
if (!ignoreMissingClasses) {
return factoryNames;
}
return factoryNames.stream().filter(this::present).toList();
}

protected Collection<String> loadFactoryNames(Class<?> source) {
return ImportCandidates.load(source, getBeanClassLoader()).getCandidates();
}

private boolean present(String className) {
String resourcePath = ClassUtils.convertClassNameToResourcePath(className) + ".class";
return new ClassPathResource(resourcePath).exists();
}

@Override
protected Set<String> getExclusions(AnnotationMetadata metadata, AnnotationAttributes attributes) {
Set<String> exclusions = new LinkedHashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,23 @@ void importsAreSelectedUsingClassesAttribute() throws Exception {
assertThat(imports).containsExactly(FreeMarkerAutoConfiguration.class.getName());
}

@Test
void importsAreSelectedFromImportsFile() throws Exception {
AnnotationMetadata annotationMetadata = getAnnotationMetadata(FromImportsFile.class);
String[] imports = this.importSelector.selectImports(annotationMetadata);
assertThat(imports).containsExactly(
"org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration",
"org.springframework.boot.autoconfigure.missing.MissingAutoConfiguration");
}

@Test
void importsSelectedFromImportsFileCanIgnoreMissingClasses() throws Exception {
AnnotationMetadata annotationMetadata = getAnnotationMetadata(FromImportsFileIgnoringMissingClasses.class);
String[] imports = this.importSelector.selectImports(annotationMetadata);
assertThat(imports)
.containsExactly("org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration");
}

@Test
void propertyExclusionsAreApplied() throws IOException {
this.environment.setProperty("spring.autoconfigure.exclude", FreeMarkerAutoConfiguration.class.getName());
Expand Down Expand Up @@ -312,6 +329,18 @@ Class<?>[] excludeAutoConfiguration() default {

}

@Retention(RetentionPolicy.RUNTIME)
@ImportAutoConfiguration
@interface FromImportsFile {

}

@Retention(RetentionPolicy.RUNTIME)
@ImportAutoConfiguration(ignoreMissingClasses = true)
@interface FromImportsFileIgnoringMissingClasses {

}

static class TestImportAutoConfigurationImportSelector extends ImportAutoConfigurationImportSelector {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration
org.springframework.boot.autoconfigure.missing.MissingAutoConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
com.example.ImportedByFile
org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration

0 comments on commit 626a114

Please sign in to comment.