|
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;
|
@@ -97,7 +98,9 @@ public ArchitectureCheck() {
|
97 | 98 | noClassesShouldLoadResourcesUsingResourceUtils(), noClassesShouldCallStringToUpperCaseWithoutLocale(),
|
98 | 99 | noClassesShouldCallStringToLowerCaseWithoutLocale(),
|
99 | 100 | conditionalOnMissingBeanShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodReturnType(),
|
100 |
| - enumSourceShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodParameterType()); |
| 101 | + enumSourceShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodParameterType(), |
| 102 | + classLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute(), |
| 103 | + methodLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute()); |
101 | 104 | getRules().addAll(getProhibitObjectsRequireNonNull()
|
102 | 105 | .map((prohibit) -> prohibit ? noClassesShouldCallObjectsRequireNonNull() : Collections.emptyList()));
|
103 | 106 | getRuleDescriptions().set(getRules().map((rules) -> rules.stream().map(ArchRule::getDescription).toList()));
|
@@ -344,6 +347,39 @@ public void check(JavaMethod item, ConditionEvents events) {
|
344 | 347 | };
|
345 | 348 | }
|
346 | 349 |
|
| 350 | + private ArchRule classLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute() { |
| 351 | + return ArchRuleDefinition.classes() |
| 352 | + .that() |
| 353 | + .areAnnotatedWith("org.springframework.boot.context.properties.ConfigurationProperties") |
| 354 | + .should(notSpecifyOnlyPrefixAttributeOfConfigurationProperties()) |
| 355 | + .allowEmptyShould(true); |
| 356 | + } |
| 357 | + |
| 358 | + private ArchRule methodLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute() { |
| 359 | + return ArchRuleDefinition.methods() |
| 360 | + .that() |
| 361 | + .areAnnotatedWith("org.springframework.boot.context.properties.ConfigurationProperties") |
| 362 | + .should(notSpecifyOnlyPrefixAttributeOfConfigurationProperties()) |
| 363 | + .allowEmptyShould(true); |
| 364 | + } |
| 365 | + |
| 366 | + private ArchCondition<? super HasAnnotations<?>> notSpecifyOnlyPrefixAttributeOfConfigurationProperties() { |
| 367 | + return new ArchCondition<>("not specify only prefix attribute of @ConfigurationProperties") { |
| 368 | + |
| 369 | + @Override |
| 370 | + public void check(HasAnnotations<?> item, ConditionEvents events) { |
| 371 | + JavaAnnotation<?> configurationProperties = item |
| 372 | + .getAnnotationOfType("org.springframework.boot.context.properties.ConfigurationProperties"); |
| 373 | + Map<String, Object> properties = configurationProperties.getProperties(); |
| 374 | + if (properties.size() == 1 && properties.containsKey("prefix")) { |
| 375 | + events.add(SimpleConditionEvent.violated(item, configurationProperties.getDescription() |
| 376 | + + " should specify implicit 'value' attribute other than explicit 'prefix' attribute")); |
| 377 | + } |
| 378 | + } |
| 379 | + |
| 380 | + }; |
| 381 | + } |
| 382 | + |
347 | 383 | public void setClasses(FileCollection classes) {
|
348 | 384 | this.classes = classes;
|
349 | 385 | }
|
|
0 commit comments