|
1 | 1 | /* |
2 | | - * Copyright 2012-2024 the original author or authors. |
| 2 | + * Copyright 2012-2025 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
39 | 39 | import com.tngtech.archunit.core.domain.JavaParameter; |
40 | 40 | import com.tngtech.archunit.core.domain.JavaType; |
41 | 41 | import com.tngtech.archunit.core.domain.properties.CanBeAnnotated; |
| 42 | +import com.tngtech.archunit.core.domain.properties.HasAnnotations; |
42 | 43 | import com.tngtech.archunit.core.domain.properties.HasName; |
43 | 44 | import com.tngtech.archunit.core.domain.properties.HasOwner.Predicates.With; |
44 | 45 | import com.tngtech.archunit.core.domain.properties.HasParameterTypes; |
@@ -95,7 +96,9 @@ public ArchitectureCheck() { |
95 | 96 | noClassesShouldLoadResourcesUsingResourceUtils(), noClassesShouldCallStringToUpperCaseWithoutLocale(), |
96 | 97 | noClassesShouldCallStringToLowerCaseWithoutLocale(), |
97 | 98 | conditionalOnMissingBeanShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodReturnType(), |
98 | | - enumSourceShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodParameterType()); |
| 99 | + enumSourceShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodParameterType(), |
| 100 | + classLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute(), |
| 101 | + methodLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute()); |
99 | 102 | getRules().addAll(getProhibitObjectsRequireNonNull() |
100 | 103 | .map((prohibit) -> prohibit ? noClassesShouldCallObjectsRequireNonNull() : Collections.emptyList())); |
101 | 104 | getRuleDescriptions().set(getRules().map((rules) -> rules.stream().map(ArchRule::getDescription).toList())); |
@@ -329,6 +332,39 @@ public void check(JavaMethod item, ConditionEvents events) { |
329 | 332 | }; |
330 | 333 | } |
331 | 334 |
|
| 335 | + private ArchRule classLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute() { |
| 336 | + return ArchRuleDefinition.classes() |
| 337 | + .that() |
| 338 | + .areAnnotatedWith("org.springframework.boot.context.properties.ConfigurationProperties") |
| 339 | + .should(notSpecifyOnlyPrefixAttributeOfConfigurationProperties()) |
| 340 | + .allowEmptyShould(true); |
| 341 | + } |
| 342 | + |
| 343 | + private ArchRule methodLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute() { |
| 344 | + return ArchRuleDefinition.methods() |
| 345 | + .that() |
| 346 | + .areAnnotatedWith("org.springframework.boot.context.properties.ConfigurationProperties") |
| 347 | + .should(notSpecifyOnlyPrefixAttributeOfConfigurationProperties()) |
| 348 | + .allowEmptyShould(true); |
| 349 | + } |
| 350 | + |
| 351 | + private ArchCondition<? super HasAnnotations<?>> notSpecifyOnlyPrefixAttributeOfConfigurationProperties() { |
| 352 | + return new ArchCondition<>("not specify only prefix attribute of @ConfigurationProperties") { |
| 353 | + |
| 354 | + @Override |
| 355 | + public void check(HasAnnotations<?> item, ConditionEvents events) { |
| 356 | + JavaAnnotation<?> configurationProperties = item |
| 357 | + .getAnnotationOfType("org.springframework.boot.context.properties.ConfigurationProperties"); |
| 358 | + Map<String, Object> properties = configurationProperties.getProperties(); |
| 359 | + if (properties.size() == 1 && properties.containsKey("prefix")) { |
| 360 | + events.add(SimpleConditionEvent.violated(item, configurationProperties.getDescription() |
| 361 | + + " should specify implicit 'value' attribute other than explicit 'prefix' attribute")); |
| 362 | + } |
| 363 | + } |
| 364 | + |
| 365 | + }; |
| 366 | + } |
| 367 | + |
332 | 368 | public void setClasses(FileCollection classes) { |
333 | 369 | this.classes = classes; |
334 | 370 | } |
|
0 commit comments