Revert "Generate ManagementContextConfiguration.imports file from annotations"

This reverts commit 6b3b0dd3a6.
This commit is contained in:
Scott Frederick
2022-09-21 11:48:51 -05:00
parent 1f53eb7df9
commit d62d7ca75d
12 changed files with 74 additions and 335 deletions

View File

@@ -1,85 +0,0 @@
/*
* 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.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.tools.FileObject;
import javax.tools.StandardLocation;
/**
* An {@link AbstractProcessor} to scan for annotated classes and write the class names to
* a file using the Spring Boot {@code .imports} format.
*
* @author Scott Frederick
*/
abstract class AbstractImportsAnnotationProcessor extends AbstractProcessor {
private final List<String> qualifiedClassNames = new ArrayList<>();
abstract String getImportsFilePath();
@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 imports file '" + getImportsFilePath() + "'", ex);
}
}
return false;
}
private void writeImportsFile() throws IOException {
if (!this.qualifiedClassNames.isEmpty()) {
Filer filer = this.processingEnv.getFiler();
FileObject file = filer.createResource(StandardLocation.CLASS_OUTPUT, "", getImportsFilePath());
try (Writer writer = new OutputStreamWriter(file.openOutputStream(), StandardCharsets.UTF_8)) {
for (String className : this.qualifiedClassNames) {
writer.append(className);
writer.append(System.lineSeparator());
}
}
}
}
}

View File

@@ -16,7 +16,23 @@
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
@@ -27,11 +43,47 @@ import javax.annotation.processing.SupportedAnnotationTypes;
* @since 3.0.0
*/
@SupportedAnnotationTypes({ "org.springframework.boot.autoconfigure.AutoConfiguration" })
public class AutoConfigurationImportsAnnotationProcessor extends AbstractImportsAnnotationProcessor {
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
protected String getImportsFilePath() {
return "META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports";
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());
}
}
}
}
}

View File

@@ -1,37 +0,0 @@
/*
* 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;
/**
* Annotation processor to generate a
* {@code META-INF/spring/org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration.imports}
* file from {@code @ManagementContextConfiguration} annotations.
*
* @author Scott Frederick
* @since 3.0.0
*/
@SupportedAnnotationTypes({ "org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration" })
public class ManagementContextConfigurationImportsAnnotationProcessor extends AbstractImportsAnnotationProcessor {
@Override
protected String getImportsFilePath() {
return "META-INF/spring/org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration.imports";
}
}

View File

@@ -1,3 +1,2 @@
org.springframework.boot.autoconfigureprocessor.AutoConfigureAnnotationProcessor,aggregating
org.springframework.boot.autoconfigureprocessor.AutoConfigurationImportsAnnotationProcessor,aggregating
org.springframework.boot.autoconfigureprocessor.ManagementContextConfigurationImportsAnnotationProcessor,aggregating
org.springframework.boot.autoconfigureprocessor.AutoConfigurationImportsAnnotationProcessor,aggregating

View File

@@ -1,3 +1,2 @@
org.springframework.boot.autoconfigureprocessor.AutoConfigureAnnotationProcessor
org.springframework.boot.autoconfigureprocessor.AutoConfigurationImportsAnnotationProcessor
org.springframework.boot.autoconfigureprocessor.ManagementContextConfigurationImportsAnnotationProcessor