Skip to content

Commit 75e25a5

Browse files
committed
Prevent @bean method being public if it should not be
Fix the violation that classes are exposed outside their defined visibility scope. Signed-off-by: Yanming Zhou <[email protected]>
1 parent 5b759c7 commit 75e25a5

File tree

30 files changed

+82
-58
lines changed

30 files changed

+82
-58
lines changed

buildSrc/src/main/java/org/springframework/boot/build/architecture/ArchitectureRules.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ static List<ArchRule> standard() {
9898
rules.add(methodLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute());
9999
rules.add(conditionsShouldNotBePublic());
100100
rules.add(allConfigurationPropertiesBindingBeanMethodsShouldBeStatic());
101+
rules.add(allConfigurationMethodsShouldNotBePublicIfReturnTypeOrParameterTypeIsNotPublic());
101102
return List.copyOf(rules);
102103
}
103104

@@ -319,6 +320,30 @@ private static ArchRule allConfigurationPropertiesBindingBeanMethodsShouldBeStat
319320
.allowEmptyShould(true);
320321
}
321322

323+
private static ArchRule allConfigurationMethodsShouldNotBePublicIfReturnTypeOrParameterTypeIsNotPublic() {
324+
return methodsThatAreAnnotatedWith("org.springframework.context.annotation.Bean")
325+
.should(check("not be public if return type is not public or any of parameter types is not public",
326+
ArchitectureRules::configurationMethodsShouldNotBePublicIfReturnTypeOrParameterTypeIsNotPublic))
327+
.allowEmptyShould(true);
328+
}
329+
330+
private static void configurationMethodsShouldNotBePublicIfReturnTypeOrParameterTypeIsNotPublic(JavaMethod item,
331+
ConditionEvents events) {
332+
if (!item.getModifiers().contains(JavaModifier.PUBLIC)) {
333+
return;
334+
}
335+
boolean violated = !item.getReturnType().toErasure().getModifiers().contains(JavaModifier.PUBLIC);
336+
if (!violated) {
337+
violated = item.getParameterTypes()
338+
.stream()
339+
.anyMatch((parameterType) -> !parameterType.toErasure().getModifiers().contains(JavaModifier.PUBLIC));
340+
}
341+
if (violated) {
342+
addViolation(events, item, item.getDescription()
343+
+ " should not be public if return type is not public or any of parameter types is not public");
344+
}
345+
}
346+
322347
private static boolean containsOnlySingleType(JavaType[] types, JavaType type) {
323348
return types.length == 1 && type.equals(types[0]);
324349
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/availability/AvailabilityProbesAutoConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -59,7 +59,7 @@ public ReadinessStateHealthIndicator readinessStateHealthIndicator(
5959
}
6060

6161
@Bean
62-
public AvailabilityProbesHealthEndpointGroupsPostProcessor availabilityProbesHealthEndpointGroupsPostProcessor(
62+
AvailabilityProbesHealthEndpointGroupsPostProcessor availabilityProbesHealthEndpointGroupsPostProcessor(
6363
Environment environment) {
6464
return new AvailabilityProbesHealthEndpointGroupsPostProcessor(environment);
6565
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/reactive/ReactiveCloudFoundryActuatorAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public CloudFoundryInfoEndpointWebExtension cloudFoundryInfoEndpointWebExtension
107107

108108
@Bean
109109
@SuppressWarnings("removal")
110-
public CloudFoundryWebFluxEndpointHandlerMapping cloudFoundryWebFluxEndpointHandlerMapping(
110+
CloudFoundryWebFluxEndpointHandlerMapping cloudFoundryWebFluxEndpointHandlerMapping(
111111
ParameterValueMapper parameterMapper, EndpointMediaTypes endpointMediaTypes,
112112
WebClient.Builder webClientBuilder,
113113
org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpointsSupplier controllerEndpointsSupplier,

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundryActuatorAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public CloudFoundryInfoEndpointWebExtension cloudFoundryInfoEndpointWebExtension
111111

112112
@Bean
113113
@SuppressWarnings("removal")
114-
public CloudFoundryWebEndpointServletHandlerMapping cloudFoundryWebEndpointServletHandlerMapping(
114+
CloudFoundryWebEndpointServletHandlerMapping cloudFoundryWebEndpointServletHandlerMapping(
115115
ParameterValueMapper parameterMapper, EndpointMediaTypes endpointMediaTypes,
116116
RestTemplateBuilder restTemplateBuilder,
117117
org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointsSupplier servletEndpointsSupplier,

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/jmx/JmxEndpointAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public JmxEndpointDiscoverer jmxAnnotationEndpointDiscoverer(ParameterValueMappe
9292

9393
@Bean
9494
@ConditionalOnMissingBean(value = EndpointObjectNameFactory.class, search = SearchStrategy.CURRENT)
95-
public DefaultEndpointObjectNameFactory endpointObjectNameFactory(MBeanServer mBeanServer) {
95+
DefaultEndpointObjectNameFactory endpointObjectNameFactory(MBeanServer mBeanServer) {
9696
String contextId = ObjectUtils.getIdentityHexString(this.applicationContext);
9797
return new DefaultEndpointObjectNameFactory(this.properties, this.jmxProperties, mBeanServer, contextId);
9898
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsAutoConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -56,7 +56,7 @@ public Clock micrometerClock() {
5656
}
5757

5858
@Bean
59-
public static MeterRegistryPostProcessor meterRegistryPostProcessor(ApplicationContext applicationContext,
59+
static MeterRegistryPostProcessor meterRegistryPostProcessor(ApplicationContext applicationContext,
6060
ObjectProvider<MetricsProperties> metricsProperties,
6161
ObjectProvider<MeterRegistryCustomizer<?>> meterRegistryCustomizers,
6262
ObjectProvider<MeterFilter> meterFilters, ObjectProvider<MeterBinder> meterBinders) {

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/amqp/RabbitMetricsAutoConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,7 +44,7 @@
4444
public class RabbitMetricsAutoConfiguration {
4545

4646
@Bean
47-
public static RabbitConnectionFactoryMetricsPostProcessor rabbitConnectionFactoryMetricsPostProcessor(
47+
static RabbitConnectionFactoryMetricsPostProcessor rabbitConnectionFactoryMetricsPostProcessor(
4848
ApplicationContext applicationContext) {
4949
return new RabbitConnectionFactoryMetricsPostProcessor(applicationContext);
5050
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/data/RepositoryMetricsAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public MetricsRepositoryMethodInvocationListener metricsRepositoryMethodInvocati
7272
}
7373

7474
@Bean
75-
public static MetricsRepositoryMethodInvocationListenerBeanPostProcessor metricsRepositoryMethodInvocationListenerBeanPostProcessor(
75+
static MetricsRepositoryMethodInvocationListenerBeanPostProcessor metricsRepositoryMethodInvocationListenerBeanPostProcessor(
7676
ObjectProvider<MetricsRepositoryMethodInvocationListener> metricsRepositoryMethodInvocationListener) {
7777
return new MetricsRepositoryMethodInvocationListenerBeanPostProcessor(
7878
SingletonSupplier.of(metricsRepositoryMethodInvocationListener::getObject));

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/annotation/AbstractWebEndpointIntegrationTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -725,7 +725,7 @@ private void load(Consumer<T> contextCustomizer, String endpointPath,
725725
protected static class TestEndpointConfiguration {
726726

727727
@Bean
728-
public TestEndpoint testEndpoint(EndpointDelegate endpointDelegate) {
728+
TestEndpoint testEndpoint(EndpointDelegate endpointDelegate) {
729729
return new TestEndpoint(endpointDelegate);
730730
}
731731

@@ -846,7 +846,7 @@ NullDeleteResponseEndpoint nullDeleteResponseEndpoint() {
846846
protected static class ResourceEndpointConfiguration {
847847

848848
@Bean
849-
public ResourceEndpoint resourceEndpoint() {
849+
ResourceEndpoint resourceEndpoint() {
850850
return new ResourceEndpoint();
851851
}
852852

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public CacheManagerCustomizers cacheManagerCustomizers(ObjectProvider<CacheManag
6969
}
7070

7171
@Bean
72-
public CacheManagerValidator cacheAutoConfigurationValidator(CacheProperties cacheProperties,
72+
CacheManagerValidator cacheAutoConfigurationValidator(CacheProperties cacheProperties,
7373
ObjectProvider<CacheManager> cacheManager) {
7474
return new CacheManagerValidator(cacheProperties, cacheManager);
7575
}

0 commit comments

Comments
 (0)