Omit empty values for AutoConfigureAfter and AutoConfigureBefore
As @AutoConfiguration is now meta-annotated with @AutoConfigureAfter and @AutoConfigureBefore, the generated property files have a lot of superfluous lines in the format <class>.AutoConfigureAfter= and <class>.AutoConfigureBefore=. One can now configure in the annotation processor for each property key if empty values should be omitted. This is currently only activated for AutoConfigureAfter and AutoConfigureBefore See gh-29907
This commit is contained in:
@@ -223,8 +223,10 @@ class AutoConfigurationSorterTests {
|
||||
Map<String, Object> autoConfigureAfter = annotationMetadata
|
||||
.getAnnotationAttributes(AutoConfigureAfter.class.getName(), true);
|
||||
if (autoConfigureAfter != null) {
|
||||
properties.put(className + ".AutoConfigureAfter",
|
||||
merge((String[]) autoConfigureAfter.get("value"), (String[]) autoConfigureAfter.get("name")));
|
||||
String value = merge((String[]) autoConfigureAfter.get("value"), (String[]) autoConfigureAfter.get("name"));
|
||||
if (!value.isEmpty()) {
|
||||
properties.put(className + ".AutoConfigureAfter", value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -233,8 +235,11 @@ class AutoConfigurationSorterTests {
|
||||
Map<String, Object> autoConfigureBefore = annotationMetadata
|
||||
.getAnnotationAttributes(AutoConfigureBefore.class.getName(), true);
|
||||
if (autoConfigureBefore != null) {
|
||||
properties.put(className + ".AutoConfigureBefore",
|
||||
merge((String[]) autoConfigureBefore.get("value"), (String[]) autoConfigureBefore.get("name")));
|
||||
String value = merge((String[]) autoConfigureBefore.get("value"),
|
||||
(String[]) autoConfigureBefore.get("name"));
|
||||
if (!value.isEmpty()) {
|
||||
properties.put(className + ".AutoConfigureBefore", value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -88,11 +88,11 @@ public class AutoConfigureAnnotationProcessor extends AbstractProcessor {
|
||||
generators.add(PropertyGenerator.of("ConditionalOnWebApplication",
|
||||
"org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication",
|
||||
ValueExtractor.allFrom("type")));
|
||||
generators.add(PropertyGenerator.of("AutoConfigureBefore",
|
||||
generators.add(PropertyGenerator.of("AutoConfigureBefore", true,
|
||||
"org.springframework.boot.autoconfigure.AutoConfigureBefore", ValueExtractor.allFrom("value", "name"),
|
||||
"org.springframework.boot.autoconfigure.AutoConfiguration",
|
||||
ValueExtractor.allFrom("before", "beforeName")));
|
||||
generators.add(PropertyGenerator.of("AutoConfigureAfter",
|
||||
generators.add(PropertyGenerator.of("AutoConfigureAfter", true,
|
||||
"org.springframework.boot.autoconfigure.AutoConfigureAfter", ValueExtractor.allFrom("value", "name"),
|
||||
"org.springframework.boot.autoconfigure.AutoConfiguration",
|
||||
ValueExtractor.allFrom("after", "afterName")));
|
||||
@@ -284,13 +284,17 @@ public class AutoConfigureAnnotationProcessor extends AbstractProcessor {
|
||||
|
||||
private final String keyName;
|
||||
|
||||
private final boolean omitEmptyValues;
|
||||
|
||||
/**
|
||||
* Maps from annotation class name -> {@link ValueExtractor}.
|
||||
*/
|
||||
private final Map<String, ValueExtractor> valueExtractors;
|
||||
|
||||
private PropertyGenerator(String keyName, Map<String, ValueExtractor> valueExtractors) {
|
||||
private PropertyGenerator(String keyName, boolean omitEmptyValues,
|
||||
Map<String, ValueExtractor> valueExtractors) {
|
||||
this.keyName = keyName;
|
||||
this.omitEmptyValues = omitEmptyValues;
|
||||
this.valueExtractors = valueExtractors;
|
||||
}
|
||||
|
||||
@@ -303,6 +307,9 @@ public class AutoConfigureAnnotationProcessor extends AbstractProcessor {
|
||||
}
|
||||
|
||||
void applyToProperties(Map<String, String> properties, String className, List<Object> annotationValues) {
|
||||
if (this.omitEmptyValues && annotationValues.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
mergeProperties(properties, className + "." + this.keyName, toCommaDelimitedString(annotationValues));
|
||||
}
|
||||
|
||||
@@ -329,15 +336,26 @@ public class AutoConfigureAnnotationProcessor extends AbstractProcessor {
|
||||
}
|
||||
|
||||
static PropertyGenerator of(String keyName, String annotation, ValueExtractor valueExtractor) {
|
||||
return new PropertyGenerator(keyName, Collections.singletonMap(annotation, valueExtractor));
|
||||
return of(keyName, false, annotation, valueExtractor);
|
||||
}
|
||||
|
||||
static PropertyGenerator of(String keyName, boolean omitEmptyValues, String annotation,
|
||||
ValueExtractor valueExtractor) {
|
||||
return new PropertyGenerator(keyName, omitEmptyValues,
|
||||
Collections.singletonMap(annotation, valueExtractor));
|
||||
}
|
||||
|
||||
static PropertyGenerator of(String keyName, String annotation1, ValueExtractor valueExtractor1,
|
||||
String annotation2, ValueExtractor valueExtractor2) {
|
||||
return of(keyName, false, annotation1, valueExtractor1, annotation2, valueExtractor2);
|
||||
}
|
||||
|
||||
static PropertyGenerator of(String keyName, boolean omitEmptyValues, String annotation1,
|
||||
ValueExtractor valueExtractor1, String annotation2, ValueExtractor valueExtractor2) {
|
||||
Map<String, ValueExtractor> valueExtractors = new LinkedHashMap<>();
|
||||
valueExtractors.put(annotation1, valueExtractor1);
|
||||
valueExtractors.put(annotation2, valueExtractor2);
|
||||
return new PropertyGenerator(keyName, valueExtractors);
|
||||
return new PropertyGenerator(keyName, omitEmptyValues, valueExtractors);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -67,6 +67,19 @@ class AutoConfigureAnnotationProcessorTests {
|
||||
+ "TestClassConfiguration.ConditionalOnWebApplication", "SERVLET");
|
||||
}
|
||||
|
||||
@Test
|
||||
void annotatedClassWithOnlyAutoConfiguration() throws Exception {
|
||||
Properties properties = compile(TestAutoConfigurationOnlyConfiguration.class);
|
||||
assertThat(properties).containsEntry(
|
||||
"org.springframework.boot.autoconfigureprocessor.TestAutoConfigurationOnlyConfiguration", "");
|
||||
assertThat(properties).doesNotContainEntry(
|
||||
"org.springframework.boot.autoconfigureprocessor.TestAutoConfigurationOnlyConfiguration.AutoConfigureAfter",
|
||||
"");
|
||||
assertThat(properties).doesNotContainEntry(
|
||||
"org.springframework.boot.autoconfigureprocessor.TestAutoConfigurationOnlyConfiguration.AutoConfigureBefore",
|
||||
"");
|
||||
}
|
||||
|
||||
@Test
|
||||
void annotatedClassWithOnBeanThatHasName() throws Exception {
|
||||
Properties properties = compile(TestOnBeanWithNameClassConfiguration.class);
|
||||
|
||||
@@ -26,6 +26,6 @@ import java.io.OutputStream;
|
||||
*/
|
||||
@TestAutoConfiguration(before = InputStream.class, beforeName = { "test.before1", "test.before2" },
|
||||
after = OutputStream.class, afterName = { "test.after1", "test.after2" })
|
||||
public class TestAutoConfigurationConfiguration {
|
||||
class TestAutoConfigurationConfiguration {
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Tests a plain {@code @AutoConfiguration} annotated class.
|
||||
*
|
||||
* @author Moritz Halbritter
|
||||
*/
|
||||
@TestAutoConfiguration
|
||||
class TestAutoConfigurationOnlyConfiguration {
|
||||
|
||||
}
|
||||
@@ -57,10 +57,10 @@ public class TestAutoConfigureAnnotationProcessor extends AutoConfigureAnnotatio
|
||||
TestConditionalOnSingleCandidate.class.getName(), new OnBeanConditionValueExtractor()));
|
||||
generators.add(PropertyGenerator.of("ConditionalOnWebApplication",
|
||||
TestConditionalOnWebApplication.class.getName(), ValueExtractor.allFrom("type")));
|
||||
generators.add(PropertyGenerator.of("AutoConfigureBefore", TestAutoConfigureBefore.class.getName(),
|
||||
generators.add(PropertyGenerator.of("AutoConfigureBefore", true, TestAutoConfigureBefore.class.getName(),
|
||||
ValueExtractor.allFrom("value", "name"), TestAutoConfiguration.class.getName(),
|
||||
ValueExtractor.allFrom("before", "beforeName")));
|
||||
generators.add(PropertyGenerator.of("AutoConfigureAfter", TestAutoConfigureAfter.class.getName(),
|
||||
generators.add(PropertyGenerator.of("AutoConfigureAfter", true, TestAutoConfigureAfter.class.getName(),
|
||||
ValueExtractor.allFrom("value", "name"), TestAutoConfiguration.class.getName(),
|
||||
ValueExtractor.allFrom("after", "afterName")));
|
||||
generators.add(PropertyGenerator.of("AutoConfigureOrder", TestAutoConfigureOrder.class.getName(),
|
||||
|
||||
@@ -31,6 +31,6 @@ import java.io.OutputStream;
|
||||
@TestAutoConfigureAfter(value = OutputStream.class, name = { "test.after1", "test.after2" })
|
||||
@TestAutoConfiguration(before = ObjectInputStream.class, beforeName = { "test.before3", "test.before4" },
|
||||
after = ObjectOutputStream.class, afterName = { "test.after3", "test.after4" })
|
||||
public class TestMergedAutoConfigurationConfiguration {
|
||||
class TestMergedAutoConfigurationConfiguration {
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user