Skip to content

Commit 3ba61c3

Browse files
committed
Improve handling of annotation properties in architecture checks
Closes gh-47437
1 parent 61acc52 commit 3ba61c3

File tree

9 files changed

+155
-23
lines changed

9 files changed

+155
-23
lines changed

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

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static List<ArchRule> standard() {
8989
rules.add(noClassesShouldCallStringToUpperCaseWithoutLocale());
9090
rules.add(noClassesShouldCallStringToLowerCaseWithoutLocale());
9191
rules.add(conditionalOnMissingBeanShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodReturnType());
92-
rules.add(enumSourceShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodParameterType());
92+
rules.add(enumSourceShouldNotHaveValueThatIsTheSameAsTypeOfMethodsFirstParameter());
9393
rules.add(allConfigurationPropertiesBindingBeanMethodsShouldBeStatic());
9494
return List.copyOf(rules);
9595
}
@@ -222,7 +222,7 @@ private static ArchCondition<? super JavaMethod> notSpecifyOnlyATypeThatIsTheSam
222222
JavaAnnotation<JavaMethod> conditionalAnnotation = item
223223
.getAnnotationOfType("org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean");
224224
Map<String, Object> properties = conditionalAnnotation.getProperties();
225-
if (!properties.containsKey("type") && !properties.containsKey("name")) {
225+
if (!hasProperty("type", properties) && !hasProperty("name", properties)) {
226226
conditionalAnnotation.get("value").ifPresent((value) -> {
227227
if (containsOnlySingleType((JavaType[]) value, item.getReturnType())) {
228228
addViolation(events, item, conditionalAnnotation.getDescription()
@@ -233,32 +233,38 @@ private static ArchCondition<? super JavaMethod> notSpecifyOnlyATypeThatIsTheSam
233233
});
234234
}
235235

236-
private static ArchRule enumSourceShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodParameterType() {
236+
private static boolean hasProperty(String name, Map<String, Object> properties) {
237+
Object property = properties.get(name);
238+
if (property == null) {
239+
return false;
240+
}
241+
return !property.getClass().isArray() || ((Object[]) property).length > 0;
242+
}
243+
244+
private static ArchRule enumSourceShouldNotHaveValueThatIsTheSameAsTypeOfMethodsFirstParameter() {
237245
return ArchRuleDefinition.methods()
238246
.that()
239247
.areAnnotatedWith("org.junit.jupiter.params.provider.EnumSource")
240-
.should(notSpecifyOnlyATypeThatIsTheSameAsTheMethodParameterType())
248+
.should(notHaveValueThatIsTheSameAsTheTypeOfTheMethodsFirstParameter())
241249
.allowEmptyShould(true);
242250
}
243251

244-
private static ArchCondition<? super JavaMethod> notSpecifyOnlyATypeThatIsTheSameAsTheMethodParameterType() {
245-
return check("not specify only a type that is the same as the method's parameter type",
252+
private static ArchCondition<? super JavaMethod> notHaveValueThatIsTheSameAsTheTypeOfTheMethodsFirstParameter() {
253+
return check("not have a value that is the same as the type of the method's first parameter",
246254
ArchitectureRules::notSpecifyOnlyATypeThatIsTheSameAsTheMethodParameterType);
247255
}
248256

249257
private static void notSpecifyOnlyATypeThatIsTheSameAsTheMethodParameterType(JavaMethod item,
250258
ConditionEvents events) {
251259
JavaAnnotation<JavaMethod> enumSourceAnnotation = item
252260
.getAnnotationOfType("org.junit.jupiter.params.provider.EnumSource");
253-
Map<String, Object> properties = enumSourceAnnotation.getProperties();
254-
if (properties.size() == 1 && item.getParameterTypes().size() == 1) {
255-
enumSourceAnnotation.get("value").ifPresent((value) -> {
256-
if (value.equals(item.getParameterTypes().get(0))) {
257-
addViolation(events, item, enumSourceAnnotation.getDescription()
258-
+ " should not specify only a value that is the same as the method's parameter type");
259-
}
260-
});
261-
}
261+
enumSourceAnnotation.get("value").ifPresent((value) -> {
262+
JavaType parameterType = item.getParameterTypes().get(0);
263+
if (value.equals(parameterType)) {
264+
addViolation(events, item, enumSourceAnnotation.getDescription()
265+
+ " should not specify a value that is the same as the type of the method's first parameter");
266+
}
267+
});
262268
}
263269

264270
private static ArchRule allConfigurationPropertiesBindingBeanMethodsShouldBeStatic() {

buildSrc/src/test/java/org/springframework/boot/build/architecture/ArchitectureCheckTests.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class ArchitectureCheckTests {
5757

5858
private static final String SPRING_CONTEXT = "org.springframework:spring-context:6.2.9";
5959

60+
private static final String JUNIT_JUPITER = "org.junit.jupiter:junit-jupiter:5.12.0";
61+
6062
private static final String SPRING_INTEGRATION_JMX = "org.springframework.integration:spring-integration-jmx:6.5.1";
6163

6264
private GradleBuild gradleBuild;
@@ -270,6 +272,29 @@ void whenBeanMethodExposesNonPrivateTypeShouldSucceedAndWriteEmptyReport(Task ta
270272
build(this.gradleBuild.withDependencies(SPRING_CONTEXT), task);
271273
}
272274

275+
@Test
276+
void whenEnumSourceValueIsInferredShouldSucceedAndWriteEmptyReport() throws IOException {
277+
prepareTask(Task.CHECK_ARCHITECTURE_TEST, "junit/enumsource/inferredfromparametertype");
278+
build(this.gradleBuild.withDependencies(JUNIT_JUPITER), Task.CHECK_ARCHITECTURE_TEST);
279+
}
280+
281+
@Test
282+
void whenEnumSourceValueIsNotTheSameAsTypeOfMethodsFirstParameterShouldSucceedAndWriteEmptyReport()
283+
throws IOException {
284+
prepareTask(Task.CHECK_ARCHITECTURE_TEST, "junit/enumsource/valuenecessary");
285+
build(this.gradleBuild.withDependencies(JUNIT_JUPITER), Task.CHECK_ARCHITECTURE_TEST);
286+
}
287+
288+
@Test
289+
void whenEnumSourceValueIsSameAsTypeOfMethodsFirstParameterShouldFailAndWriteReport() throws IOException {
290+
prepareTask(Task.CHECK_ARCHITECTURE_TEST, "junit/enumsource/sameasparametertype");
291+
buildAndFail(this.gradleBuild.withDependencies(JUNIT_JUPITER), Task.CHECK_ARCHITECTURE_TEST,
292+
"method <org.springframework.boot.build.architecture.junit.enumsource.sameasparametertype"
293+
+ ".EnumSourceSameAsParameterType.exampleMethod(org.springframework.boot.build."
294+
+ "architecture.junit.enumsource.sameasparametertype.EnumSourceSameAsParameterType$Example)>",
295+
"should not have a value that is the same as the type of the method's first parameter");
296+
}
297+
273298
private void prepareTask(Task task, String... sourceDirectories) throws IOException {
274299
for (String sourceDirectory : sourceDirectories) {
275300
FileSystemUtils.copyRecursively(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2012-present 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.build.architecture.junit.enumsource.inferredfromparametertype;
18+
19+
import org.junit.jupiter.params.provider.EnumSource;
20+
21+
class EnumSourceInferredFromParameterType {
22+
23+
@EnumSource
24+
void exampleMethod(Example example) {
25+
26+
}
27+
28+
enum Example {
29+
30+
ONE, TWO, THREE
31+
32+
}
33+
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2012-present 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.build.architecture.junit.enumsource.sameasparametertype;
18+
19+
import org.junit.jupiter.params.provider.EnumSource;
20+
21+
class EnumSourceSameAsParameterType {
22+
23+
@EnumSource(Example.class)
24+
void exampleMethod(Example example) {
25+
26+
}
27+
28+
enum Example {
29+
30+
ONE, TWO, THREE
31+
32+
}
33+
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2012-present 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.build.architecture.junit.enumsource.valuenecessary;
18+
19+
import org.junit.jupiter.params.provider.EnumSource;
20+
21+
class EnumSourceValueNecessary {
22+
23+
@EnumSource(Example.class)
24+
void exampleMethod(String thing, Example example) {
25+
26+
}
27+
28+
enum Example {
29+
30+
ONE, TWO, THREE
31+
32+
}
33+
34+
}

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/sbom/SbomEndpointWebExtensionTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ void shouldUseContentTypeForAdditionalSbomsIfSet() {
112112
}
113113

114114
@ParameterizedTest
115-
@EnumSource(value = SbomType.class, names = "UNKNOWN", mode = Mode.EXCLUDE)
115+
@EnumSource(names = "UNKNOWN", mode = Mode.EXCLUDE)
116116
void shouldAutodetectFormats(SbomType type) throws IOException {
117117
String content = getSbomContent(type);
118118
assertThat(type.matches(content)).isTrue();

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/http/JacksonHttpMessageConvertersConfiguration.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,9 @@ class JacksonHttpMessageConvertersConfiguration {
4545
static class MappingJackson2HttpMessageConverterConfiguration {
4646

4747
@Bean
48-
@ConditionalOnMissingBean(value = MappingJackson2HttpMessageConverter.class,
49-
ignoredType = {
50-
"org.springframework.hateoas.server.mvc.TypeConstrainedMappingJackson2HttpMessageConverter",
51-
"org.springframework.data.rest.webmvc.alps.AlpsJsonHttpMessageConverter" })
48+
@ConditionalOnMissingBean(ignoredType = {
49+
"org.springframework.hateoas.server.mvc.TypeConstrainedMappingJackson2HttpMessageConverter",
50+
"org.springframework.data.rest.webmvc.alps.AlpsJsonHttpMessageConverter" })
5251
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
5352
return new MappingJackson2HttpMessageConverter(objectMapper);
5453
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/error/ErrorWebFluxAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public ErrorWebFluxAutoConfiguration(ServerProperties serverProperties) {
5959
}
6060

6161
@Bean
62-
@ConditionalOnMissingBean(value = ErrorWebExceptionHandler.class, search = SearchStrategy.CURRENT)
62+
@ConditionalOnMissingBean(search = SearchStrategy.CURRENT)
6363
@Order(-1)
6464
public ErrorWebExceptionHandler errorWebExceptionHandler(ErrorAttributes errorAttributes,
6565
WebProperties webProperties, ObjectProvider<ViewResolver> viewResolvers,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ void getSettingsWithPlatformDoesNotTouchDataSource() {
6262
}
6363

6464
@ParameterizedTest
65-
@EnumSource(value = DatabaseDriver.class, mode = Mode.EXCLUDE, names = { "CLICKHOUSE", "FIREBIRD", "INFORMIX",
66-
"JTDS", "PHOENIX", "REDSHIFT", "TERADATA", "TESTCONTAINERS", "UNKNOWN" })
65+
@EnumSource(mode = Mode.EXCLUDE, names = { "CLICKHOUSE", "FIREBIRD", "INFORMIX", "JTDS", "PHOENIX", "REDSHIFT",
66+
"TERADATA", "TESTCONTAINERS", "UNKNOWN" })
6767
void batchSchemaCanBeLocated(DatabaseDriver driver) throws SQLException {
6868
DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
6969
BatchProperties properties = new BatchProperties();

0 commit comments

Comments
 (0)