Skip to content

Commit 4812328

Browse files
committed
Polish OnPropertyCondition
See gh-43754
1 parent 9c146c0 commit 4812328

File tree

4 files changed

+31
-16
lines changed

4 files changed

+31
-16
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionEvaluationReport.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import java.util.Set;
2828
import java.util.SortedMap;
2929
import java.util.TreeMap;
30+
import java.util.stream.Stream;
31+
import java.util.stream.StreamSupport;
3032

3133
import org.springframework.beans.factory.BeanFactory;
3234
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
@@ -237,6 +239,15 @@ public boolean isFullMatch() {
237239
return true;
238240
}
239241

242+
/**
243+
* Return a {@link Stream} of the {@link ConditionAndOutcome} items.
244+
* @return a stream of the {@link ConditionAndOutcome} items.
245+
* @since 3.5.0
246+
*/
247+
public Stream<ConditionAndOutcome> stream() {
248+
return StreamSupport.stream(spliterator(), false);
249+
}
250+
240251
@Override
241252
public Iterator<ConditionAndOutcome> iterator() {
242253
return Collections.unmodifiableSet(this.outcomes).iterator();

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnPropertyCondition.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.springframework.core.annotation.AnnotationAttributes;
2929
import org.springframework.core.annotation.MergedAnnotation;
3030
import org.springframework.core.annotation.MergedAnnotationPredicates;
31-
import org.springframework.core.annotation.MergedAnnotations;
3231
import org.springframework.core.annotation.Order;
3332
import org.springframework.core.env.PropertyResolver;
3433
import org.springframework.core.type.AnnotatedTypeMetadata;
@@ -51,15 +50,14 @@ class OnPropertyCondition extends SpringBootCondition {
5150

5251
@Override
5352
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
54-
MergedAnnotations annotations = metadata.getAnnotations();
55-
List<MergedAnnotation<Annotation>> allAnnotations = Stream
56-
.concat(annotations.stream(ConditionalOnProperty.class.getName()),
57-
annotations.stream(ConditionalOnBooleanProperty.class.getName()))
53+
List<MergedAnnotation<Annotation>> annotations = Stream
54+
.concat(metadata.getAnnotations().stream(ConditionalOnProperty.class.getName()),
55+
metadata.getAnnotations().stream(ConditionalOnBooleanProperty.class.getName()))
5856
.filter(MergedAnnotationPredicates.unique(MergedAnnotation::getMetaTypes))
5957
.toList();
6058
List<ConditionMessage> noMatch = new ArrayList<>();
6159
List<ConditionMessage> match = new ArrayList<>();
62-
for (MergedAnnotation<Annotation> annotation : allAnnotations) {
60+
for (MergedAnnotation<Annotation> annotation : annotations) {
6361
ConditionOutcome outcome = determineOutcome(annotation, context.getEnvironment());
6462
(outcome.isMatch() ? match : noMatch).add(outcome.getConditionMessage());
6563
}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
package org.springframework.boot.autoconfigure.condition;
1818

1919
import java.util.function.Consumer;
20+
import java.util.stream.Collectors;
2021

2122
import org.junit.jupiter.api.AfterEach;
2223
import org.junit.jupiter.api.Test;
2324

2425
import org.springframework.boot.WebApplicationType;
26+
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport.ConditionAndOutcomes;
2527
import org.springframework.boot.builder.SpringApplicationBuilder;
2628
import org.springframework.boot.test.util.TestPropertyValues;
2729
import org.springframework.context.ConfigurableApplicationContext;
@@ -181,12 +183,13 @@ private <T extends Exception> Consumer<T> causeMessageContaining(String message)
181183
}
182184

183185
private String getConditionEvaluationReport() {
184-
ConditionEvaluationReport report = ConditionEvaluationReport.get(this.context.getBeanFactory());
185-
StringBuilder builder = new StringBuilder();
186-
report.getConditionAndOutcomesBySource()
186+
return ConditionEvaluationReport.get(this.context.getBeanFactory())
187+
.getConditionAndOutcomesBySource()
187188
.values()
188-
.forEach((outcomes) -> outcomes.forEach((outcome) -> builder.append(outcome.toString()).append('\n')));
189-
return builder.toString();
189+
.stream()
190+
.flatMap(ConditionAndOutcomes::stream)
191+
.map(Object::toString)
192+
.collect(Collectors.joining("\n"));
190193
}
191194

192195
private void load(Class<?> config, String... environment) {

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
import java.lang.annotation.RetentionPolicy;
2222
import java.lang.annotation.Target;
2323
import java.util.function.Consumer;
24+
import java.util.stream.Collectors;
2425

2526
import org.junit.jupiter.api.AfterEach;
2627
import org.junit.jupiter.api.Test;
2728

2829
import org.springframework.boot.WebApplicationType;
30+
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport.ConditionAndOutcomes;
2931
import org.springframework.boot.builder.SpringApplicationBuilder;
3032
import org.springframework.boot.test.util.TestPropertyValues;
3133
import org.springframework.context.ConfigurableApplicationContext;
@@ -293,12 +295,13 @@ private void load(Class<?> config, String... environment) {
293295
}
294296

295297
private String getConditionEvaluationReport() {
296-
ConditionEvaluationReport report = ConditionEvaluationReport.get(this.context.getBeanFactory());
297-
StringBuilder builder = new StringBuilder();
298-
report.getConditionAndOutcomesBySource()
298+
return ConditionEvaluationReport.get(this.context.getBeanFactory())
299+
.getConditionAndOutcomesBySource()
299300
.values()
300-
.forEach((outcomes) -> outcomes.forEach((outcome) -> builder.append(outcome.toString()).append('\n')));
301-
return builder.toString();
301+
.stream()
302+
.flatMap(ConditionAndOutcomes::stream)
303+
.map(Object::toString)
304+
.collect(Collectors.joining("\n"));
302305
}
303306

304307
@Configuration(proxyBeanMethods = false)

0 commit comments

Comments
 (0)