@@ -92,22 +92,32 @@ static List<ArchRule> standard() {
9292 rules .add (noClassesShouldLoadResourcesUsingResourceUtils ());
9393 rules .add (noClassesShouldCallStringToUpperCaseWithoutLocale ());
9494 rules .add (noClassesShouldCallStringToLowerCaseWithoutLocale ());
95- rules .add (conditionalOnMissingBeanShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodReturnType ());
9695 rules .add (enumSourceShouldNotHaveValueThatIsTheSameAsTypeOfMethodsFirstParameter ());
97- rules .add (classLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute ());
98- rules .add (methodLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute ());
9996 rules .add (conditionsShouldNotBePublic ());
100- rules .add (allConfigurationPropertiesBindingBeanMethodsShouldBeStatic ());
10197 return List .copyOf (rules );
10298 }
10399
104- static List <ArchRule > beanMethods (String annotationName ) {
100+ static List <ArchRule > beanMethods (String annotationClass ) {
105101 return List .of (allBeanMethodsShouldReturnNonPrivateType (),
106- allBeanMethodsShouldNotHaveConditionalOnClassAnnotation (annotationName ));
102+ allBeanMethodsShouldNotHaveConditionalOnClassAnnotation (annotationClass ));
107103 }
108104
109- static List <ArchRule > configurationProperties (String annotationName ) {
110- return List .of (allDeprecatedConfigurationPropertiesShouldIncludeSince (annotationName ));
105+ static List <ArchRule > conditionalOnMissingBean (String annotationClass ) {
106+ return List
107+ .of (conditionalOnMissingBeanShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodReturnType (annotationClass ));
108+ }
109+
110+ static List <ArchRule > configurationProperties (String annotationClass ) {
111+ return List .of (classLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute (annotationClass ),
112+ methodLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute (annotationClass ));
113+ }
114+
115+ static List <ArchRule > configurationPropertiesBinding (String annotationClass ) {
116+ return List .of (allConfigurationPropertiesBindingBeanMethodsShouldBeStatic (annotationClass ));
117+ }
118+
119+ static List <ArchRule > configurationPropertiesDeprecation (String annotationClass ) {
120+ return List .of (allDeprecatedConfigurationPropertiesShouldIncludeSince (annotationClass ));
111121 }
112122
113123 private static ArchRule allBeanMethodsShouldReturnNonPrivateType () {
@@ -247,16 +257,17 @@ private static ArchRule noClassesShouldCallStringToLowerCaseWithoutLocale() {
247257 .because (shouldUse ("String.toLowerCase(Locale.ROOT)" ));
248258 }
249259
250- private static ArchRule conditionalOnMissingBeanShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodReturnType () {
251- return methodsThatAreAnnotatedWith ("org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean" )
252- .should (notSpecifyOnlyATypeThatIsTheSameAsTheMethodReturnType ())
260+ private static ArchRule conditionalOnMissingBeanShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodReturnType (
261+ String annotation ) {
262+ return methodsThatAreAnnotatedWith (annotation )
263+ .should (notSpecifyOnlyATypeThatIsTheSameAsTheMethodReturnType (annotation ))
253264 .allowEmptyShould (true );
254265 }
255266
256- private static ArchCondition <? super JavaMethod > notSpecifyOnlyATypeThatIsTheSameAsTheMethodReturnType () {
267+ private static ArchCondition <? super JavaMethod > notSpecifyOnlyATypeThatIsTheSameAsTheMethodReturnType (
268+ String annotation ) {
257269 return check ("not specify only a type that is the same as the method's return type" , (item , events ) -> {
258- JavaAnnotation <JavaMethod > conditionalAnnotation = item
259- .getAnnotationOfType ("org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean" );
270+ JavaAnnotation <JavaMethod > conditionalAnnotation = item .getAnnotationOfType (annotation );
260271 Map <String , Object > properties = conditionalAnnotation .getProperties ();
261272 if (!hasProperty ("type" , properties ) && !hasProperty ("name" , properties )) {
262273 conditionalAnnotation .get ("value" ).ifPresent ((value ) -> {
@@ -274,7 +285,7 @@ private static boolean hasProperty(String name, Map<String, Object> properties)
274285 if (property == null ) {
275286 return false ;
276287 }
277- return ! property .getClass ().isArray () || ((Object []) property ).length > 0 ;
288+ return ( property .getClass ().isArray ()) ? ((Object []) property ).length > 0 : ! property . toString (). isEmpty () ;
278289 }
279290
280291 private static ArchRule enumSourceShouldNotHaveValueThatIsTheSameAsTypeOfMethodsFirstParameter () {
@@ -303,33 +314,37 @@ private static void notSpecifyOnlyATypeThatIsTheSameAsTheMethodParameterType(Jav
303314 });
304315 }
305316
306- private static ArchRule classLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute () {
317+ private static ArchRule classLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute (
318+ String annotationClass ) {
307319 return ArchRuleDefinition .classes ()
308320 .that ()
309- .areAnnotatedWith ("org.springframework.boot.context.properties.ConfigurationProperties" )
310- .should (notSpecifyOnlyPrefixAttributeOfConfigurationProperties ())
321+ .areAnnotatedWith (annotationClass )
322+ .should (notSpecifyOnlyPrefixAttributeOfConfigurationProperties (annotationClass ))
311323 .allowEmptyShould (true );
312324 }
313325
314- private static ArchRule methodLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute () {
326+ private static ArchRule methodLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute (
327+ String annotationClass ) {
315328 return ArchRuleDefinition .methods ()
316329 .that ()
317- .areAnnotatedWith ("org.springframework.boot.context.properties.ConfigurationProperties" )
318- .should (notSpecifyOnlyPrefixAttributeOfConfigurationProperties ())
330+ .areAnnotatedWith (annotationClass )
331+ .should (notSpecifyOnlyPrefixAttributeOfConfigurationProperties (annotationClass ))
319332 .allowEmptyShould (true );
320333 }
321334
322- private static ArchCondition <? super HasAnnotations <?>> notSpecifyOnlyPrefixAttributeOfConfigurationProperties () {
323- return check ("not specify only prefix attribute of @ConfigurationProperties" ,
324- ArchitectureRules ::notSpecifyOnlyPrefixAttributeOfConfigurationProperties );
335+ private static ArchCondition <? super HasAnnotations <?>> notSpecifyOnlyPrefixAttributeOfConfigurationProperties (
336+ String annotationClass ) {
337+ return check ("not specify only prefix attribute of @ConfigurationProperties" , (item ,
338+ events ) -> notSpecifyOnlyPrefixAttributeOfConfigurationProperties (annotationClass , item , events ));
325339 }
326340
327- private static void notSpecifyOnlyPrefixAttributeOfConfigurationProperties (HasAnnotations <?> item ,
328- ConditionEvents events ) {
329- JavaAnnotation <?> configurationPropertiesAnnotation = item
330- .getAnnotationOfType ("org.springframework.boot.context.properties.ConfigurationProperties" );
341+ private static void notSpecifyOnlyPrefixAttributeOfConfigurationProperties (String annotationClass ,
342+ HasAnnotations <?> item , ConditionEvents events ) {
343+ JavaAnnotation <?> configurationPropertiesAnnotation = item .getAnnotationOfType (annotationClass );
331344 Map <String , Object > properties = configurationPropertiesAnnotation .getProperties ();
332- if (properties .size () == 1 && properties .containsKey ("prefix" )) {
345+ if (hasProperty ("prefix" , properties ) && !hasProperty ("value" , properties )
346+ && properties .get ("ignoreInvalidFields" ).equals (false )
347+ && properties .get ("ignoreUnknownFields" ).equals (true )) {
333348 addViolation (events , item , configurationPropertiesAnnotation .getDescription ()
334349 + " should specify implicit 'value' attribute other than explicit 'prefix' attribute" );
335350 }
@@ -349,9 +364,9 @@ private static ArchRule conditionsShouldNotBePublic() {
349364 .allowEmptyShould (true );
350365 }
351366
352- private static ArchRule allConfigurationPropertiesBindingBeanMethodsShouldBeStatic () {
367+ private static ArchRule allConfigurationPropertiesBindingBeanMethodsShouldBeStatic (String annotationClass ) {
353368 return methodsThatAreAnnotatedWith ("org.springframework.context.annotation.Bean" ).and ()
354- .areAnnotatedWith ("org.springframework.boot.context.properties.ConfigurationPropertiesBinding" )
369+ .areAnnotatedWith (annotationClass )
355370 .should ()
356371 .beStatic ()
357372 .allowEmptyShould (true );
0 commit comments