Skip to content

Commit fd36215

Browse files
committed
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
1 parent 7872f61 commit fd36215

File tree

7 files changed

+76
-13
lines changed

7 files changed

+76
-13
lines changed

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/AutoConfigurationSorterTests.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,10 @@ private void addAutoConfigureAfter(Properties properties, String className, Anno
223223
Map<String, Object> autoConfigureAfter = annotationMetadata
224224
.getAnnotationAttributes(AutoConfigureAfter.class.getName(), true);
225225
if (autoConfigureAfter != null) {
226-
properties.put(className + ".AutoConfigureAfter",
227-
merge((String[]) autoConfigureAfter.get("value"), (String[]) autoConfigureAfter.get("name")));
226+
String value = merge((String[]) autoConfigureAfter.get("value"), (String[]) autoConfigureAfter.get("name"));
227+
if (!value.isEmpty()) {
228+
properties.put(className + ".AutoConfigureAfter", value);
229+
}
228230
}
229231
}
230232

@@ -233,8 +235,11 @@ private void addAutoConfigureBefore(Properties properties, String className,
233235
Map<String, Object> autoConfigureBefore = annotationMetadata
234236
.getAnnotationAttributes(AutoConfigureBefore.class.getName(), true);
235237
if (autoConfigureBefore != null) {
236-
properties.put(className + ".AutoConfigureBefore",
237-
merge((String[]) autoConfigureBefore.get("value"), (String[]) autoConfigureBefore.get("name")));
238+
String value = merge((String[]) autoConfigureBefore.get("value"),
239+
(String[]) autoConfigureBefore.get("name"));
240+
if (!value.isEmpty()) {
241+
properties.put(className + ".AutoConfigureBefore", value);
242+
}
238243
}
239244
}
240245

spring-boot-project/spring-boot-tools/spring-boot-autoconfigure-processor/src/main/java/org/springframework/boot/autoconfigureprocessor/AutoConfigureAnnotationProcessor.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ protected List<PropertyGenerator> getPropertyGenerators() {
8888
generators.add(PropertyGenerator.of("ConditionalOnWebApplication",
8989
"org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication",
9090
ValueExtractor.allFrom("type")));
91-
generators.add(PropertyGenerator.of("AutoConfigureBefore",
91+
generators.add(PropertyGenerator.of("AutoConfigureBefore", true,
9292
"org.springframework.boot.autoconfigure.AutoConfigureBefore", ValueExtractor.allFrom("value", "name"),
9393
"org.springframework.boot.autoconfigure.AutoConfiguration",
9494
ValueExtractor.allFrom("before", "beforeName")));
95-
generators.add(PropertyGenerator.of("AutoConfigureAfter",
95+
generators.add(PropertyGenerator.of("AutoConfigureAfter", true,
9696
"org.springframework.boot.autoconfigure.AutoConfigureAfter", ValueExtractor.allFrom("value", "name"),
9797
"org.springframework.boot.autoconfigure.AutoConfiguration",
9898
ValueExtractor.allFrom("after", "afterName")));
@@ -284,13 +284,17 @@ static final class PropertyGenerator {
284284

285285
private final String keyName;
286286

287+
private final boolean omitEmptyValues;
288+
287289
/**
288290
* Maps from annotation class name -> {@link ValueExtractor}.
289291
*/
290292
private final Map<String, ValueExtractor> valueExtractors;
291293

292-
private PropertyGenerator(String keyName, Map<String, ValueExtractor> valueExtractors) {
294+
private PropertyGenerator(String keyName, boolean omitEmptyValues,
295+
Map<String, ValueExtractor> valueExtractors) {
293296
this.keyName = keyName;
297+
this.omitEmptyValues = omitEmptyValues;
294298
this.valueExtractors = valueExtractors;
295299
}
296300

@@ -303,6 +307,9 @@ ValueExtractor getValueExtractor(String annotation) {
303307
}
304308

305309
void applyToProperties(Map<String, String> properties, String className, List<Object> annotationValues) {
310+
if (this.omitEmptyValues && annotationValues.isEmpty()) {
311+
return;
312+
}
306313
mergeProperties(properties, className + "." + this.keyName, toCommaDelimitedString(annotationValues));
307314
}
308315

@@ -329,15 +336,26 @@ private String toCommaDelimitedString(List<Object> list) {
329336
}
330337

331338
static PropertyGenerator of(String keyName, String annotation, ValueExtractor valueExtractor) {
332-
return new PropertyGenerator(keyName, Collections.singletonMap(annotation, valueExtractor));
339+
return of(keyName, false, annotation, valueExtractor);
340+
}
341+
342+
static PropertyGenerator of(String keyName, boolean omitEmptyValues, String annotation,
343+
ValueExtractor valueExtractor) {
344+
return new PropertyGenerator(keyName, omitEmptyValues,
345+
Collections.singletonMap(annotation, valueExtractor));
333346
}
334347

335348
static PropertyGenerator of(String keyName, String annotation1, ValueExtractor valueExtractor1,
336349
String annotation2, ValueExtractor valueExtractor2) {
350+
return of(keyName, false, annotation1, valueExtractor1, annotation2, valueExtractor2);
351+
}
352+
353+
static PropertyGenerator of(String keyName, boolean omitEmptyValues, String annotation1,
354+
ValueExtractor valueExtractor1, String annotation2, ValueExtractor valueExtractor2) {
337355
Map<String, ValueExtractor> valueExtractors = new LinkedHashMap<>();
338356
valueExtractors.put(annotation1, valueExtractor1);
339357
valueExtractors.put(annotation2, valueExtractor2);
340-
return new PropertyGenerator(keyName, valueExtractors);
358+
return new PropertyGenerator(keyName, omitEmptyValues, valueExtractors);
341359
}
342360

343361
}

spring-boot-project/spring-boot-tools/spring-boot-autoconfigure-processor/src/test/java/org/springframework/boot/autoconfigureprocessor/AutoConfigureAnnotationProcessorTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ void annotatedClass() throws Exception {
6767
+ "TestClassConfiguration.ConditionalOnWebApplication", "SERVLET");
6868
}
6969

70+
@Test
71+
void annotatedClassWithOnlyAutoConfiguration() throws Exception {
72+
Properties properties = compile(TestAutoConfigurationOnlyConfiguration.class);
73+
assertThat(properties).containsEntry(
74+
"org.springframework.boot.autoconfigureprocessor.TestAutoConfigurationOnlyConfiguration", "");
75+
assertThat(properties).doesNotContainEntry(
76+
"org.springframework.boot.autoconfigureprocessor.TestAutoConfigurationOnlyConfiguration.AutoConfigureAfter",
77+
"");
78+
assertThat(properties).doesNotContainEntry(
79+
"org.springframework.boot.autoconfigureprocessor.TestAutoConfigurationOnlyConfiguration.AutoConfigureBefore",
80+
"");
81+
}
82+
7083
@Test
7184
void annotatedClassWithOnBeanThatHasName() throws Exception {
7285
Properties properties = compile(TestOnBeanWithNameClassConfiguration.class);

spring-boot-project/spring-boot-tools/spring-boot-autoconfigure-processor/src/test/java/org/springframework/boot/autoconfigureprocessor/TestAutoConfigurationConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@
2626
*/
2727
@TestAutoConfiguration(before = InputStream.class, beforeName = { "test.before1", "test.before2" },
2828
after = OutputStream.class, afterName = { "test.after1", "test.after2" })
29-
public class TestAutoConfigurationConfiguration {
29+
class TestAutoConfigurationConfiguration {
3030

3131
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigureprocessor;
18+
19+
/**
20+
* Tests a plain {@code @AutoConfiguration} annotated class.
21+
*
22+
* @author Moritz Halbritter
23+
*/
24+
@TestAutoConfiguration
25+
class TestAutoConfigurationOnlyConfiguration {
26+
27+
}

spring-boot-project/spring-boot-tools/spring-boot-autoconfigure-processor/src/test/java/org/springframework/boot/autoconfigureprocessor/TestAutoConfigureAnnotationProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ protected List<PropertyGenerator> getPropertyGenerators() {
5757
TestConditionalOnSingleCandidate.class.getName(), new OnBeanConditionValueExtractor()));
5858
generators.add(PropertyGenerator.of("ConditionalOnWebApplication",
5959
TestConditionalOnWebApplication.class.getName(), ValueExtractor.allFrom("type")));
60-
generators.add(PropertyGenerator.of("AutoConfigureBefore", TestAutoConfigureBefore.class.getName(),
60+
generators.add(PropertyGenerator.of("AutoConfigureBefore", true, TestAutoConfigureBefore.class.getName(),
6161
ValueExtractor.allFrom("value", "name"), TestAutoConfiguration.class.getName(),
6262
ValueExtractor.allFrom("before", "beforeName")));
63-
generators.add(PropertyGenerator.of("AutoConfigureAfter", TestAutoConfigureAfter.class.getName(),
63+
generators.add(PropertyGenerator.of("AutoConfigureAfter", true, TestAutoConfigureAfter.class.getName(),
6464
ValueExtractor.allFrom("value", "name"), TestAutoConfiguration.class.getName(),
6565
ValueExtractor.allFrom("after", "afterName")));
6666
generators.add(PropertyGenerator.of("AutoConfigureOrder", TestAutoConfigureOrder.class.getName(),

spring-boot-project/spring-boot-tools/spring-boot-autoconfigure-processor/src/test/java/org/springframework/boot/autoconfigureprocessor/TestMergedAutoConfigurationConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@
3131
@TestAutoConfigureAfter(value = OutputStream.class, name = { "test.after1", "test.after2" })
3232
@TestAutoConfiguration(before = ObjectInputStream.class, beforeName = { "test.before3", "test.before4" },
3333
after = ObjectOutputStream.class, afterName = { "test.after3", "test.after4" })
34-
public class TestMergedAutoConfigurationConfiguration {
34+
class TestMergedAutoConfigurationConfiguration {
3535

3636
}

0 commit comments

Comments
 (0)