Generate the AutoConfiguration.imports file from annotations
This commit adds the `AutoConfigurationImportsAnnotationProcessor` to the `spring-boot-autoconfigure-processor` annotation processor module. When added to a project build, the annotation processor will generate the `org.springframework.boot.autoconfigure.AutoConfiguration.imports` file automatically from `@AutoConfiguration`-annotated classes. It also applies the annotation processor to the Spring Boot build. Closes gh-31228
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright 2012-2022 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigureprocessor;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.annotation.processing.AbstractProcessor;
|
||||
import javax.annotation.processing.Filer;
|
||||
import javax.annotation.processing.RoundEnvironment;
|
||||
import javax.annotation.processing.SupportedAnnotationTypes;
|
||||
import javax.lang.model.SourceVersion;
|
||||
import javax.lang.model.element.Element;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.tools.FileObject;
|
||||
import javax.tools.StandardLocation;
|
||||
|
||||
/**
|
||||
* Annotation processor to generate a
|
||||
* {@code META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports}
|
||||
* file from {@code @AutoConfiguration} annotations.
|
||||
*
|
||||
* @author Scott Frederick
|
||||
* @since 3.0.0
|
||||
*/
|
||||
@SupportedAnnotationTypes({ "org.springframework.boot.autoconfigure.AutoConfiguration" })
|
||||
public class AutoConfigurationImportsAnnotationProcessor extends AbstractProcessor {
|
||||
|
||||
static final String IMPORTS_FILE_PATH = "META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports";
|
||||
|
||||
private final List<String> qualifiedClassNames = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public SourceVersion getSupportedSourceVersion() {
|
||||
return SourceVersion.latestSupported();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
||||
for (TypeElement annotation : annotations) {
|
||||
Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(annotation);
|
||||
for (Element element : elements) {
|
||||
this.qualifiedClassNames.add(element.asType().toString());
|
||||
}
|
||||
}
|
||||
if (roundEnv.processingOver()) {
|
||||
try {
|
||||
writeImportsFile();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new IllegalStateException("Failed to write auto-configuration imports file", ex);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void writeImportsFile() throws IOException {
|
||||
if (!this.qualifiedClassNames.isEmpty()) {
|
||||
Filer filer = this.processingEnv.getFiler();
|
||||
FileObject file = filer.createResource(StandardLocation.CLASS_OUTPUT, "", IMPORTS_FILE_PATH);
|
||||
try (Writer writer = new OutputStreamWriter(file.openOutputStream(), StandardCharsets.UTF_8)) {
|
||||
for (String className : this.qualifiedClassNames) {
|
||||
writer.append(className);
|
||||
writer.append(System.lineSeparator());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
org.springframework.boot.autoconfigureprocessor.AutoConfigureAnnotationProcessor,aggregating
|
||||
org.springframework.boot.autoconfigureprocessor.AutoConfigureAnnotationProcessor,aggregating
|
||||
org.springframework.boot.autoconfigureprocessor.AutoConfigurationImportsAnnotationProcessor,aggregating
|
||||
@@ -1 +1,2 @@
|
||||
org.springframework.boot.autoconfigureprocessor.AutoConfigureAnnotationProcessor
|
||||
org.springframework.boot.autoconfigureprocessor.AutoConfigurationImportsAnnotationProcessor
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright 2012-2022 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigureprocessor;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import org.springframework.boot.testsupport.compiler.TestCompiler;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link TestAutoConfigurationImportsAnnotationProcessor}.
|
||||
*
|
||||
* @author Scott Frederick
|
||||
*/
|
||||
class AutoConfigurationImportsAnnotationProcessorTests {
|
||||
|
||||
@TempDir
|
||||
File tempDir;
|
||||
|
||||
private TestCompiler compiler;
|
||||
|
||||
@BeforeEach
|
||||
void createCompiler() throws IOException {
|
||||
this.compiler = new TestCompiler(this.tempDir);
|
||||
}
|
||||
|
||||
@Test
|
||||
void annotatedClasses() throws Exception {
|
||||
List<String> classes = compile(TestAutoConfigurationConfiguration.class,
|
||||
TestAutoConfigurationOnlyConfiguration.class);
|
||||
assertThat(classes).hasSize(2);
|
||||
assertThat(classes).containsExactly(
|
||||
"org.springframework.boot.autoconfigureprocessor.TestAutoConfigurationConfiguration",
|
||||
"org.springframework.boot.autoconfigureprocessor.TestAutoConfigurationOnlyConfiguration");
|
||||
}
|
||||
|
||||
@Test
|
||||
void notAnnotatedClasses() throws Exception {
|
||||
List<String> classes = compile(TestAutoConfigurationImportsAnnotationProcessor.class);
|
||||
assertThat(classes).isNull();
|
||||
}
|
||||
|
||||
private List<String> compile(Class<?>... types) throws IOException {
|
||||
TestAutoConfigurationImportsAnnotationProcessor processor = new TestAutoConfigurationImportsAnnotationProcessor();
|
||||
this.compiler.getTask(types).call(processor);
|
||||
return getWrittenImports();
|
||||
}
|
||||
|
||||
private List<String> getWrittenImports() throws IOException {
|
||||
File file = getWrittenFile();
|
||||
if (!file.exists()) {
|
||||
return null;
|
||||
}
|
||||
BufferedReader reader = new BufferedReader(new FileReader(file));
|
||||
return reader.lines().collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private File getWrittenFile() {
|
||||
return new File(this.tempDir, AutoConfigurationImportsAnnotationProcessor.IMPORTS_FILE_PATH);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright 2012-2022 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigureprocessor;
|
||||
|
||||
import javax.annotation.processing.SupportedAnnotationTypes;
|
||||
|
||||
/**
|
||||
* An extension of {@link AutoConfigurationImportsAnnotationProcessor} used for testing.
|
||||
*
|
||||
* @author Scott Frederick
|
||||
*/
|
||||
@SupportedAnnotationTypes({ "org.springframework.boot.autoconfigureprocessor.TestAutoConfiguration" })
|
||||
public class TestAutoConfigurationImportsAnnotationProcessor extends AutoConfigurationImportsAnnotationProcessor {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user