Polish OnPropertyCondition
See gh-43754
This commit is contained in:
@@ -27,6 +27,8 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
@@ -237,6 +239,15 @@ public final class ConditionEvaluationReport {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a {@link Stream} of the {@link ConditionAndOutcome} items.
|
||||
* @return a stream of the {@link ConditionAndOutcome} items.
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public Stream<ConditionAndOutcome> stream() {
|
||||
return StreamSupport.stream(spliterator(), false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<ConditionAndOutcome> iterator() {
|
||||
return Collections.unmodifiableSet(this.outcomes).iterator();
|
||||
|
||||
@@ -28,7 +28,6 @@ import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.AnnotationAttributes;
|
||||
import org.springframework.core.annotation.MergedAnnotation;
|
||||
import org.springframework.core.annotation.MergedAnnotationPredicates;
|
||||
import org.springframework.core.annotation.MergedAnnotations;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.core.env.PropertyResolver;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
@@ -51,15 +50,14 @@ class OnPropertyCondition extends SpringBootCondition {
|
||||
|
||||
@Override
|
||||
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
MergedAnnotations annotations = metadata.getAnnotations();
|
||||
List<MergedAnnotation<Annotation>> allAnnotations = Stream
|
||||
.concat(annotations.stream(ConditionalOnProperty.class.getName()),
|
||||
annotations.stream(ConditionalOnBooleanProperty.class.getName()))
|
||||
List<MergedAnnotation<Annotation>> annotations = Stream
|
||||
.concat(metadata.getAnnotations().stream(ConditionalOnProperty.class.getName()),
|
||||
metadata.getAnnotations().stream(ConditionalOnBooleanProperty.class.getName()))
|
||||
.filter(MergedAnnotationPredicates.unique(MergedAnnotation::getMetaTypes))
|
||||
.toList();
|
||||
List<ConditionMessage> noMatch = new ArrayList<>();
|
||||
List<ConditionMessage> match = new ArrayList<>();
|
||||
for (MergedAnnotation<Annotation> annotation : allAnnotations) {
|
||||
for (MergedAnnotation<Annotation> annotation : annotations) {
|
||||
ConditionOutcome outcome = determineOutcome(annotation, context.getEnvironment());
|
||||
(outcome.isMatch() ? match : noMatch).add(outcome.getConditionMessage());
|
||||
}
|
||||
|
||||
@@ -17,11 +17,13 @@
|
||||
package org.springframework.boot.autoconfigure.condition;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport.ConditionAndOutcomes;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
@@ -181,12 +183,13 @@ class ConditionalOnBooleanPropertyTests {
|
||||
}
|
||||
|
||||
private String getConditionEvaluationReport() {
|
||||
ConditionEvaluationReport report = ConditionEvaluationReport.get(this.context.getBeanFactory());
|
||||
StringBuilder builder = new StringBuilder();
|
||||
report.getConditionAndOutcomesBySource()
|
||||
return ConditionEvaluationReport.get(this.context.getBeanFactory())
|
||||
.getConditionAndOutcomesBySource()
|
||||
.values()
|
||||
.forEach((outcomes) -> outcomes.forEach((outcome) -> builder.append(outcome.toString()).append('\n')));
|
||||
return builder.toString();
|
||||
.stream()
|
||||
.flatMap(ConditionAndOutcomes::stream)
|
||||
.map(Object::toString)
|
||||
.collect(Collectors.joining("\n"));
|
||||
}
|
||||
|
||||
private void load(Class<?> config, String... environment) {
|
||||
|
||||
@@ -21,11 +21,13 @@ import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport.ConditionAndOutcomes;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
@@ -293,12 +295,13 @@ class ConditionalOnPropertyTests {
|
||||
}
|
||||
|
||||
private String getConditionEvaluationReport() {
|
||||
ConditionEvaluationReport report = ConditionEvaluationReport.get(this.context.getBeanFactory());
|
||||
StringBuilder builder = new StringBuilder();
|
||||
report.getConditionAndOutcomesBySource()
|
||||
return ConditionEvaluationReport.get(this.context.getBeanFactory())
|
||||
.getConditionAndOutcomesBySource()
|
||||
.values()
|
||||
.forEach((outcomes) -> outcomes.forEach((outcome) -> builder.append(outcome.toString()).append('\n')));
|
||||
return builder.toString();
|
||||
.stream()
|
||||
.flatMap(ConditionAndOutcomes::stream)
|
||||
.map(Object::toString)
|
||||
.collect(Collectors.joining("\n"));
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
|
||||
Reference in New Issue
Block a user