Skip to content

Commit f4c6aab

Browse files
committed
Merge branch '3.3.x'
Closes gh-42945
2 parents e8b8584 + 5318665 commit f4c6aab

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

buildSrc/src/main/java/org/springframework/boot/build/architecture/ArchitectureCheck.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,20 @@
2424
import java.nio.file.StandardOpenOption;
2525
import java.util.Collections;
2626
import java.util.List;
27+
import java.util.Map;
2728
import java.util.Objects;
2829
import java.util.function.Supplier;
2930
import java.util.stream.Collectors;
3031

3132
import com.tngtech.archunit.base.DescribedPredicate;
33+
import com.tngtech.archunit.core.domain.JavaAnnotation;
3234
import com.tngtech.archunit.core.domain.JavaCall;
3335
import com.tngtech.archunit.core.domain.JavaClass;
3436
import com.tngtech.archunit.core.domain.JavaClass.Predicates;
3537
import com.tngtech.archunit.core.domain.JavaClasses;
3638
import com.tngtech.archunit.core.domain.JavaMethod;
3739
import com.tngtech.archunit.core.domain.JavaParameter;
40+
import com.tngtech.archunit.core.domain.JavaType;
3841
import com.tngtech.archunit.core.domain.properties.CanBeAnnotated;
3942
import com.tngtech.archunit.core.domain.properties.HasName;
4043
import com.tngtech.archunit.core.domain.properties.HasOwner.Predicates.With;
@@ -90,7 +93,8 @@ public ArchitectureCheck() {
9093
noClassesShouldConfigureDefaultStepVerifierTimeout(), noClassesShouldCallCollectorsToList(),
9194
noClassesShouldCallURLEncoderWithStringEncoding(), noClassesShouldCallURLDecoderWithStringEncoding(),
9295
noClassesShouldLoadResourcesUsingResourceUtils(), noClassesShouldCallStringToUpperCaseWithoutLocale(),
93-
noClassesShouldCallStringToLowerCaseWithoutLocale());
96+
noClassesShouldCallStringToLowerCaseWithoutLocale(),
97+
conditionalOnMissingBeanShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodReturnType());
9498
getRules().addAll(getProhibitObjectsRequireNonNull()
9599
.map((prohibit) -> prohibit ? noClassesShouldCallObjectsRequireNonNull() : Collections.emptyList()));
96100
getRuleDescriptions().set(getRules().map((rules) -> rules.stream().map(ArchRule::getDescription).toList()));
@@ -265,6 +269,36 @@ private List<ArchRule> noClassesShouldCallObjectsRequireNonNull() {
265269
.because("org.springframework.utils.Assert.notNull(Object, Supplier) should be used instead"));
266270
}
267271

272+
private ArchRule conditionalOnMissingBeanShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodReturnType() {
273+
return ArchRuleDefinition.methods()
274+
.that()
275+
.areAnnotatedWith("org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean")
276+
.should(notSpecifyOnlyATypeThatIsTheSameAsTheMethodReturnType())
277+
.allowEmptyShould(true);
278+
}
279+
280+
private ArchCondition<? super JavaMethod> notSpecifyOnlyATypeThatIsTheSameAsTheMethodReturnType() {
281+
return new ArchCondition<>("not specify only a type that is the same as the method's return type") {
282+
283+
@Override
284+
public void check(JavaMethod item, ConditionEvents events) {
285+
JavaAnnotation<JavaMethod> conditional = item
286+
.getAnnotationOfType("org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean");
287+
Map<String, Object> properties = conditional.getProperties();
288+
if (!properties.containsKey("type") && !properties.containsKey("name")) {
289+
conditional.get("value").ifPresent((value) -> {
290+
JavaType[] types = (JavaType[]) value;
291+
if (types.length == 1 && item.getReturnType().equals(types[0])) {
292+
events.add(SimpleConditionEvent.violated(item, conditional.getDescription()
293+
+ " should not specify only a value that is the same as the method's return type"));
294+
}
295+
});
296+
}
297+
}
298+
299+
};
300+
}
301+
268302
public void setClasses(FileCollection classes) {
269303
this.classes = classes;
270304
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ ExampleBean createExampleBean() {
562562
static class ConditionalOnIgnoredSubclass {
563563

564564
@Bean
565-
@ConditionalOnMissingBean(value = ExampleBean.class, ignored = CustomExampleBean.class)
565+
@ConditionalOnMissingBean(ignored = CustomExampleBean.class)
566566
ExampleBean exampleBean() {
567567
return new ExampleBean("test");
568568
}
@@ -573,7 +573,7 @@ ExampleBean exampleBean() {
573573
static class ConditionalOnIgnoredSubclassByName {
574574

575575
@Bean
576-
@ConditionalOnMissingBean(value = ExampleBean.class,
576+
@ConditionalOnMissingBean(
577577
ignoredType = "org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBeanTests$CustomExampleBean")
578578
ExampleBean exampleBean() {
579579
return new ExampleBean("test");
@@ -798,8 +798,7 @@ TestParameterizedContainer<CustomExampleBean> customExampleBean() {
798798
static class ParameterizedConditionWithValueConfig {
799799

800800
@Bean
801-
@ConditionalOnMissingBean(value = CustomExampleBean.class,
802-
parameterizedContainer = TestParameterizedContainer.class)
801+
@ConditionalOnMissingBean(parameterizedContainer = TestParameterizedContainer.class)
803802
CustomExampleBean conditionalCustomExampleBean() {
804803
return new CustomExampleBean();
805804
}

0 commit comments

Comments
 (0)