Skip to content

Enforce that '@ConfigurationPropertiesBinding' bean methods are static #45640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
* @author Scott Frederick
* @author Ivan Malutin
* @author Phillip Webb
* @author Ngoc Nhan
*/
final class ArchitectureRules {

Expand Down Expand Up @@ -96,6 +97,7 @@ static List<ArchRule> standard() {
rules.add(classLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute());
rules.add(methodLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute());
rules.add(conditionsShouldNotBePublic());
rules.add(allConfigurationPropertiesBindingBeanMethodsShouldBeStatic());
return List.copyOf(rules);
}

Expand Down Expand Up @@ -309,6 +311,14 @@ private static ArchRule conditionsShouldNotBePublic() {
.allowEmptyShould(true);
}

private static ArchRule allConfigurationPropertiesBindingBeanMethodsShouldBeStatic() {
return methodsThatAreAnnotatedWith("org.springframework.context.annotation.Bean").and()
.areAnnotatedWith("org.springframework.boot.context.properties.ConfigurationPropertiesBinding")
.should()
.beStatic()
.allowEmptyShould(true);
}

private static boolean containsOnlySingleType(JavaType[] types, JavaType type) {
return types.length == 1 && type.equals(types[0]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public class FlywayAutoConfiguration {

@Bean
@ConfigurationPropertiesBinding
public StringOrNumberToMigrationVersionConverter stringOrNumberMigrationVersionConverter() {
public static StringOrNumberToMigrationVersionConverter stringOrNumberMigrationVersionConverter() {
return new StringOrNumberToMigrationVersionConverter();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ static class ConverterConfiguration {

@Bean
@ConfigurationPropertiesBinding
Converter<String, TargetType> targetTypeConverter() {
static Converter<String, TargetType> targetTypeConverter() {
return new Converter<>() {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1593,7 +1593,7 @@ static class PersonConverterConfiguration {

@Bean
@ConfigurationPropertiesBinding
Converter<String, Person> personConverter() {
static Converter<String, Person> personConverter() {
return new PersonConverter();
}

Expand All @@ -1604,7 +1604,7 @@ static class AlienConverterConfiguration {

@Bean
@ConfigurationPropertiesBinding
Converter<String, Alien> alienConverter() {
static Converter<String, Alien> alienConverter() {
return new AlienConverter();
}

Expand All @@ -1625,7 +1625,7 @@ static class GenericConverterConfiguration {

@Bean
@ConfigurationPropertiesBinding
GenericConverter genericPersonConverter() {
static GenericConverter genericPersonConverter() {
return new GenericPersonConverter();
}

Expand All @@ -1636,7 +1636,7 @@ static class FormatterConfiguration {

@Bean
@ConfigurationPropertiesBinding
Formatter<Person> personFormatter() {
static Formatter<Person> personFormatter() {
return new PersonFormatter();
}

Expand Down Expand Up @@ -3064,7 +3064,7 @@ static class WithCustomConverterAndObjectToObjectMethodConfiguration {

@Bean
@ConfigurationPropertiesBinding
WithObjectToObjectMethodConverter withObjectToObjectMethodConverter() {
static WithObjectToObjectMethodConverter withObjectToObjectMethodConverter() {
return new WithObjectToObjectMethodConverter();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,13 @@ static class CustomConverterConfiguration {

@Bean
@ConfigurationPropertiesBinding
TestConverter testConverter() {
static TestConverter testConverter() {
return new TestConverter();
}

@Bean
@ConfigurationPropertiesBinding
StringConverter stringConverter() {
static StringConverter stringConverter() {
return new StringConverter();
}

Expand All @@ -146,13 +146,13 @@ static class CustomLambdaConverterConfiguration {

@Bean
@ConfigurationPropertiesBinding
Converter<InputStream, OutputStream> testConverter() {
static Converter<InputStream, OutputStream> testConverter() {
return (source) -> new TestConverter().convert(source);
}

@Bean
@ConfigurationPropertiesBinding
Converter<String, InputStream> stringConverter() {
static Converter<String, InputStream> stringConverter() {
return (source) -> new StringConverter().convert(source);
}

Expand All @@ -163,7 +163,7 @@ static class PrinterConfiguration {

@Bean
@ConfigurationPropertiesBinding
Printer<InputStream> inputStreamPrinter() {
static Printer<InputStream> inputStreamPrinter() {
return (source, locale) -> ThrowingSupplier
.of(() -> StreamUtils.copyToString(source, StandardCharsets.UTF_8))
.get();
Expand Down