@@ -104,25 +104,35 @@ static List<ArchRule> standard() {
104104 rules .add (noClassesShouldLoadResourcesUsingResourceUtils ());
105105 rules .add (noClassesShouldCallStringToUpperCaseWithoutLocale ());
106106 rules .add (noClassesShouldCallStringToLowerCaseWithoutLocale ());
107- rules .add (conditionalOnMissingBeanShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodReturnType ());
108107 rules .add (enumSourceShouldNotHaveValueThatIsTheSameAsTypeOfMethodsFirstParameter ());
109- rules .add (classLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute ());
110- rules .add (methodLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute ());
111108 rules .add (conditionsShouldNotBePublic ());
112- rules .add (allConfigurationPropertiesBindingBeanMethodsShouldBeStatic ());
113109 rules .add (autoConfigurationClassesShouldBePublicAndFinal ());
114110 rules .add (autoConfigurationClassesShouldHaveNoPublicMembers ());
115111 rules .add (testAutoConfigurationClassesShouldBePackagePrivateAndFinal ());
116112 return List .copyOf (rules );
117113 }
118114
119- static List <ArchRule > beanMethods (String annotationName ) {
115+ static List <ArchRule > beanMethods (String annotationClass ) {
120116 return List .of (allBeanMethodsShouldReturnNonPrivateType (),
121- allBeanMethodsShouldNotHaveConditionalOnClassAnnotation (annotationName ));
117+ allBeanMethodsShouldNotHaveConditionalOnClassAnnotation (annotationClass ));
122118 }
123119
124- static List <ArchRule > configurationProperties (String annotationName ) {
125- return List .of (allDeprecatedConfigurationPropertiesShouldIncludeSince (annotationName ));
120+ static List <ArchRule > conditionalOnMissingBean (String annotationClass ) {
121+ return List
122+ .of (conditionalOnMissingBeanShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodReturnType (annotationClass ));
123+ }
124+
125+ static List <ArchRule > configurationProperties (String annotationClass ) {
126+ return List .of (classLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute (annotationClass ),
127+ methodLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute (annotationClass ));
128+ }
129+
130+ static List <ArchRule > configurationPropertiesBinding (String annotationClass ) {
131+ return List .of (allConfigurationPropertiesBindingBeanMethodsShouldBeStatic (annotationClass ));
132+ }
133+
134+ static List <ArchRule > configurationPropertiesDeprecation (String annotationClass ) {
135+ return List .of (allDeprecatedConfigurationPropertiesShouldIncludeSince (annotationClass ));
126136 }
127137
128138 private static ArchRule allBeanMethodsShouldReturnNonPrivateType () {
@@ -267,9 +277,10 @@ private static ArchRule noClassesShouldCallStringToLowerCaseWithoutLocale() {
267277 .because (shouldUse ("String.toLowerCase(Locale.ROOT)" ));
268278 }
269279
270- private static ArchRule conditionalOnMissingBeanShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodReturnType () {
271- return methodsThatAreAnnotatedWith ("org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean" )
272- .should (notSpecifyOnlyATypeThatIsTheSameAsTheMethodReturnType ())
280+ private static ArchRule conditionalOnMissingBeanShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodReturnType (
281+ String annotation ) {
282+ return methodsThatAreAnnotatedWith (annotation )
283+ .should (notSpecifyOnlyATypeThatIsTheSameAsTheMethodReturnType (annotation ))
273284 .allowEmptyShould (true );
274285 }
275286
@@ -279,10 +290,10 @@ static ArchRule packagesShouldBeAnnotatedWithNullMarked(Set<String> ignoredPacka
279290 .allowEmptyShould (true );
280291 }
281292
282- private static ArchCondition <? super JavaMethod > notSpecifyOnlyATypeThatIsTheSameAsTheMethodReturnType () {
293+ private static ArchCondition <? super JavaMethod > notSpecifyOnlyATypeThatIsTheSameAsTheMethodReturnType (
294+ String annotation ) {
283295 return check ("not specify only a type that is the same as the method's return type" , (item , events ) -> {
284- JavaAnnotation <JavaMethod > conditionalAnnotation = item
285- .getAnnotationOfType ("org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean" );
296+ JavaAnnotation <JavaMethod > conditionalAnnotation = item .getAnnotationOfType (annotation );
286297 Map <String , Object > properties = conditionalAnnotation .getProperties ();
287298 if (!hasProperty ("type" , properties ) && !hasProperty ("name" , properties )) {
288299 conditionalAnnotation .get ("value" ).ifPresent ((value ) -> {
@@ -300,7 +311,7 @@ private static boolean hasProperty(String name, Map<String, Object> properties)
300311 if (property == null ) {
301312 return false ;
302313 }
303- return ! property .getClass ().isArray () || ((Object []) property ).length > 0 ;
314+ return ( property .getClass ().isArray ()) ? ((Object []) property ).length > 0 : ! property . toString (). isEmpty () ;
304315 }
305316
306317 private static ArchRule enumSourceShouldNotHaveValueThatIsTheSameAsTypeOfMethodsFirstParameter () {
@@ -329,33 +340,37 @@ private static void notSpecifyOnlyATypeThatIsTheSameAsTheMethodParameterType(Jav
329340 });
330341 }
331342
332- private static ArchRule classLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute () {
343+ private static ArchRule classLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute (
344+ String annotationClass ) {
333345 return ArchRuleDefinition .classes ()
334346 .that ()
335- .areAnnotatedWith ("org.springframework.boot.context.properties.ConfigurationProperties" )
336- .should (notSpecifyOnlyPrefixAttributeOfConfigurationProperties ())
347+ .areAnnotatedWith (annotationClass )
348+ .should (notSpecifyOnlyPrefixAttributeOfConfigurationProperties (annotationClass ))
337349 .allowEmptyShould (true );
338350 }
339351
340- private static ArchRule methodLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute () {
352+ private static ArchRule methodLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute (
353+ String annotationClass ) {
341354 return ArchRuleDefinition .methods ()
342355 .that ()
343- .areAnnotatedWith ("org.springframework.boot.context.properties.ConfigurationProperties" )
344- .should (notSpecifyOnlyPrefixAttributeOfConfigurationProperties ())
356+ .areAnnotatedWith (annotationClass )
357+ .should (notSpecifyOnlyPrefixAttributeOfConfigurationProperties (annotationClass ))
345358 .allowEmptyShould (true );
346359 }
347360
348- private static ArchCondition <? super HasAnnotations <?>> notSpecifyOnlyPrefixAttributeOfConfigurationProperties () {
349- return check ("not specify only prefix attribute of @ConfigurationProperties" ,
350- ArchitectureRules ::notSpecifyOnlyPrefixAttributeOfConfigurationProperties );
361+ private static ArchCondition <? super HasAnnotations <?>> notSpecifyOnlyPrefixAttributeOfConfigurationProperties (
362+ String annotationClass ) {
363+ return check ("not specify only prefix attribute of @ConfigurationProperties" , (item ,
364+ events ) -> notSpecifyOnlyPrefixAttributeOfConfigurationProperties (annotationClass , item , events ));
351365 }
352366
353- private static void notSpecifyOnlyPrefixAttributeOfConfigurationProperties (HasAnnotations <?> item ,
354- ConditionEvents events ) {
355- JavaAnnotation <?> configurationPropertiesAnnotation = item
356- .getAnnotationOfType ("org.springframework.boot.context.properties.ConfigurationProperties" );
367+ private static void notSpecifyOnlyPrefixAttributeOfConfigurationProperties (String annotationClass ,
368+ HasAnnotations <?> item , ConditionEvents events ) {
369+ JavaAnnotation <?> configurationPropertiesAnnotation = item .getAnnotationOfType (annotationClass );
357370 Map <String , Object > properties = configurationPropertiesAnnotation .getProperties ();
358- if (properties .size () == 1 && properties .containsKey ("prefix" )) {
371+ if (hasProperty ("prefix" , properties ) && !hasProperty ("value" , properties )
372+ && properties .get ("ignoreInvalidFields" ).equals (false )
373+ && properties .get ("ignoreUnknownFields" ).equals (true )) {
359374 addViolation (events , item , configurationPropertiesAnnotation .getDescription ()
360375 + " should specify implicit 'value' attribute other than explicit 'prefix' attribute" );
361376 }
@@ -375,9 +390,9 @@ private static ArchRule conditionsShouldNotBePublic() {
375390 .allowEmptyShould (true );
376391 }
377392
378- private static ArchRule allConfigurationPropertiesBindingBeanMethodsShouldBeStatic () {
393+ private static ArchRule allConfigurationPropertiesBindingBeanMethodsShouldBeStatic (String annotationClass ) {
379394 return methodsThatAreAnnotatedWith ("org.springframework.context.annotation.Bean" ).and ()
380- .areAnnotatedWith ("org.springframework.boot.context.properties.ConfigurationPropertiesBinding" )
395+ .areAnnotatedWith (annotationClass )
381396 .should ()
382397 .beStatic ()
383398 .allowEmptyShould (true );
0 commit comments