Skip to content

Commit 51cba6e

Browse files
committed
Polish "Add AOT support for actuator"
See gh-31671
1 parent 584b7d1 commit 51cba6e

File tree

56 files changed

+386
-938
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+386
-938
lines changed

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,15 @@
1919
import java.util.Collection;
2020
import java.util.Collections;
2121
import java.util.LinkedHashMap;
22-
import java.util.List;
2322
import java.util.Map;
23+
import java.util.Objects;
2424
import java.util.stream.Collectors;
2525

2626
import org.reactivestreams.Publisher;
2727
import reactor.core.publisher.Mono;
2828

29-
import org.springframework.aot.hint.ExecutableMode;
3029
import org.springframework.aot.hint.RuntimeHints;
3130
import org.springframework.aot.hint.RuntimeHintsRegistrar;
32-
import org.springframework.aot.hint.TypeReference;
3331
import org.springframework.boot.actuate.autoconfigure.cloudfoundry.AccessLevel;
3432
import org.springframework.boot.actuate.autoconfigure.cloudfoundry.SecurityResponse;
3533
import org.springframework.boot.actuate.autoconfigure.cloudfoundry.reactive.CloudFoundryWebFluxEndpointHandlerMapping.CloudFoundryWebFluxEndpointHandlerMappingRuntimeHints;
@@ -46,6 +44,7 @@
4644
import org.springframework.http.HttpStatus;
4745
import org.springframework.http.ResponseEntity;
4846
import org.springframework.http.server.reactive.ServerHttpRequest;
47+
import org.springframework.util.ReflectionUtils;
4948
import org.springframework.web.cors.CorsConfiguration;
5049
import org.springframework.web.reactive.result.method.RequestMappingInfoHandlerMapping;
5150
import org.springframework.web.server.ServerWebExchange;
@@ -160,10 +159,8 @@ static class CloudFoundryWebFluxEndpointHandlerMappingRuntimeHints implements Ru
160159

161160
@Override
162161
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
163-
hints.reflection().registerType(CloudFoundryLinksHandler.class,
164-
(hint) -> hint.onReachableType(TypeReference.of(CloudFoundryLinksHandler.class)).withMethod("links",
165-
List.of(TypeReference.of(ServerWebExchange.class)),
166-
(method) -> method.setModes(ExecutableMode.INVOKE)));
162+
hints.reflection().registerMethod(Objects.requireNonNull(
163+
ReflectionUtils.findMethod(CloudFoundryLinksHandler.class, "links", ServerWebExchange.class)));
167164
this.bindingRegistrar.registerReflectionHints(hints.reflection(), Link.class);
168165
}
169166

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,17 @@
1919
import java.util.Collection;
2020
import java.util.Collections;
2121
import java.util.LinkedHashMap;
22-
import java.util.List;
2322
import java.util.Map;
23+
import java.util.Objects;
2424
import java.util.stream.Collectors;
2525

2626
import jakarta.servlet.http.HttpServletRequest;
2727
import jakarta.servlet.http.HttpServletResponse;
2828
import org.apache.commons.logging.Log;
2929
import org.apache.commons.logging.LogFactory;
3030

31-
import org.springframework.aot.hint.ExecutableMode;
3231
import org.springframework.aot.hint.RuntimeHints;
3332
import org.springframework.aot.hint.RuntimeHintsRegistrar;
34-
import org.springframework.aot.hint.TypeReference;
3533
import org.springframework.boot.actuate.autoconfigure.cloudfoundry.AccessLevel;
3634
import org.springframework.boot.actuate.autoconfigure.cloudfoundry.SecurityResponse;
3735
import org.springframework.boot.actuate.autoconfigure.cloudfoundry.servlet.CloudFoundryWebEndpointServletHandlerMapping.CloudFoundryWebEndpointServletHandlerMappingRuntimeHints;
@@ -47,6 +45,7 @@
4745
import org.springframework.context.aot.BindingReflectionHintsRegistrar;
4846
import org.springframework.http.HttpStatus;
4947
import org.springframework.http.ResponseEntity;
48+
import org.springframework.util.ReflectionUtils;
5049
import org.springframework.web.bind.annotation.ResponseBody;
5150
import org.springframework.web.cors.CorsConfiguration;
5251
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
@@ -162,11 +161,9 @@ static class CloudFoundryWebEndpointServletHandlerMappingRuntimeHints implements
162161

163162
@Override
164163
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
165-
hints.reflection().registerType(CloudFoundryLinksHandler.class,
166-
(hint) -> hint.onReachableType(TypeReference.of(CloudFoundryLinksHandler.class)).withMethod("links",
167-
List.of(TypeReference.of(HttpServletRequest.class),
168-
TypeReference.of(HttpServletResponse.class)),
169-
(method) -> method.setModes(ExecutableMode.INVOKE)));
164+
hints.reflection()
165+
.registerMethod(Objects.requireNonNull(ReflectionUtils.findMethod(CloudFoundryLinksHandler.class,
166+
"links", HttpServletRequest.class, HttpServletResponse.class)));
170167
this.bindingRegistrar.registerReflectionHints(hints.reflection(), Link.class);
171168
}
172169

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.actuate.aot;
17+
package org.springframework.boot.actuate.autoconfigure.endpoint;
1818

1919
import java.util.stream.Stream;
2020

@@ -29,12 +29,11 @@
2929
import org.springframework.core.annotation.SynthesizedAnnotation;
3030

3131
/**
32-
* Registrar which registers the annotations needed for actuator support.
32+
* {@link RuntimeHintsRegistrar} for actuator support.
3333
*
3434
* @author Moritz Halbritter
35-
* @since 3.0.0
3635
*/
37-
public class ActuatorAnnotationsRuntimeHintsRegistrar implements RuntimeHintsRegistrar {
36+
class ActuatorAnnotationsRuntimeHints implements RuntimeHintsRegistrar {
3837

3938
@Override
4039
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.stream.Collectors;
2121

2222
import org.springframework.beans.factory.ObjectProvider;
23-
import org.springframework.boot.actuate.aot.ActuatorAnnotationsRuntimeHintsRegistrar;
2423
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
2524
import org.springframework.boot.actuate.endpoint.annotation.EndpointConverter;
2625
import org.springframework.boot.actuate.endpoint.invoke.ParameterValueMapper;
@@ -47,7 +46,7 @@
4746
* @since 2.0.0
4847
*/
4948
@AutoConfiguration
50-
@ImportRuntimeHints(ActuatorAnnotationsRuntimeHintsRegistrar.class)
49+
@ImportRuntimeHints(ActuatorAnnotationsRuntimeHints.class)
5150
public class EndpointAutoConfiguration {
5251

5352
@Bean

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/CloudFoundryWebEndpointDiscovererRuntimeHintsTests.java

Lines changed: 0 additions & 45 deletions
This file was deleted.

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/CloudFoundryWebEndpointDiscovererTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323

2424
import org.junit.jupiter.api.Test;
2525

26+
import org.springframework.aot.hint.MemberCategory;
27+
import org.springframework.aot.hint.RuntimeHints;
28+
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
29+
import org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryWebEndpointDiscoverer.CloudFoundryWebEndpointDiscovererRuntimeHints;
2630
import org.springframework.boot.actuate.endpoint.EndpointId;
2731
import org.springframework.boot.actuate.endpoint.InvocationContext;
2832
import org.springframework.boot.actuate.endpoint.SecurityContext;
@@ -50,6 +54,7 @@
5054
* Tests for {@link CloudFoundryWebEndpointDiscoverer}.
5155
*
5256
* @author Madhura Bhave
57+
* @author Moritz Halbritter
5358
*/
5459
class CloudFoundryWebEndpointDiscovererTests {
5560

@@ -69,6 +74,14 @@ void getEndpointsShouldAddCloudFoundryHealthExtension() {
6974
});
7075
}
7176

77+
@Test
78+
void shouldRegisterHints() {
79+
RuntimeHints runtimeHints = new RuntimeHints();
80+
new CloudFoundryWebEndpointDiscovererRuntimeHints().registerHints(runtimeHints, getClass().getClassLoader());
81+
assertThat(RuntimeHintsPredicates.reflection().onType(CloudFoundryEndpointFilter.class)
82+
.withMemberCategories(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).accepts(runtimeHints);
83+
}
84+
7285
private WebOperation findMainReadOperation(ExposableWebEndpoint endpoint) {
7386
for (WebOperation operation : endpoint.getOperations()) {
7487
if (operation.getRequestPredicate().getPath().equals("health")) {
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,17 @@
2626
import org.springframework.boot.actuate.endpoint.web.Link;
2727

2828
/**
29-
* Tests for {@link CloudFoundryWebFluxEndpointHandlerMappingRuntimeHints}.
29+
* Tests for {@link CloudFoundryWebFluxEndpointHandlerMapping}.
3030
*
3131
* @author Moritz Halbritter
3232
*/
33-
class CloudFoundryWebFluxEndpointHandlerMappingRuntimeHintsTests {
34-
35-
private final CloudFoundryWebFluxEndpointHandlerMappingRuntimeHints sut = new CloudFoundryWebFluxEndpointHandlerMappingRuntimeHints();
33+
class CloudFoundryWebFluxEndpointHandlerMappingTests {
3634

3735
@Test
3836
void shouldRegisterHints() {
3937
RuntimeHints runtimeHints = new RuntimeHints();
40-
this.sut.registerHints(runtimeHints, getClass().getClassLoader());
38+
new CloudFoundryWebFluxEndpointHandlerMappingRuntimeHints().registerHints(runtimeHints,
39+
getClass().getClassLoader());
4140
Assertions.assertThat(RuntimeHintsPredicates.reflection().onMethod(CloudFoundryLinksHandler.class, "links"))
4241
.accepts(runtimeHints);
4342
Assertions.assertThat(RuntimeHintsPredicates.reflection().onType(Link.class)).accepts(runtimeHints);
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,17 @@
2727
import static org.assertj.core.api.Assertions.assertThat;
2828

2929
/**
30-
* Tests for {@link CloudFoundryWebEndpointServletHandlerMappingRuntimeHints}.
30+
* Tests for {@link CloudFoundryWebEndpointServletHandlerMapping}.
3131
*
3232
* @author Moritz Halbritter
3333
*/
34-
class CloudFoundryWebEndpointServletHandlerMappingRuntimeHintsTests {
35-
36-
private final CloudFoundryWebEndpointServletHandlerMappingRuntimeHints sut = new CloudFoundryWebEndpointServletHandlerMappingRuntimeHints();
34+
class CloudFoundryWebEndpointServletHandlerMappingTests {
3735

3836
@Test
3937
void shouldRegisterHints() {
4038
RuntimeHints runtimeHints = new RuntimeHints();
41-
this.sut.registerHints(runtimeHints, getClass().getClassLoader());
39+
new CloudFoundryWebEndpointServletHandlerMappingRuntimeHints().registerHints(runtimeHints,
40+
getClass().getClassLoader());
4241
assertThat(RuntimeHintsPredicates.reflection().onMethod(CloudFoundryLinksHandler.class, "links"))
4342
.accepts(runtimeHints);
4443
assertThat(RuntimeHintsPredicates.reflection().onType(Link.class)).accepts(runtimeHints);
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.actuate.aot;
17+
package org.springframework.boot.actuate.autoconfigure.endpoint;
1818

1919
import java.util.Set;
2020

@@ -34,20 +34,20 @@
3434
import static org.assertj.core.api.Assertions.assertThat;
3535

3636
/**
37-
* Tests for {@link ActuatorAnnotationsRuntimeHintsRegistrar}.
37+
* Tests for {@link ActuatorAnnotationsRuntimeHints}.
3838
*
3939
* @author Moritz Halbritter
4040
*/
41-
class ActuatorAnnotationsRuntimeHintsRegistrarTests {
41+
class ActuatorAnnotationsRuntimeHintsTests {
4242

43-
private final ActuatorAnnotationsRuntimeHintsRegistrar sut = new ActuatorAnnotationsRuntimeHintsRegistrar();
43+
private final ActuatorAnnotationsRuntimeHints registrar = new ActuatorAnnotationsRuntimeHints();
4444

4545
private RuntimeHints runtimeHints;
4646

4747
@BeforeEach
4848
void setUp() {
4949
this.runtimeHints = new RuntimeHints();
50-
this.sut.registerHints(this.runtimeHints, getClass().getClassLoader());
50+
this.registrar.registerHints(this.runtimeHints, getClass().getClassLoader());
5151
}
5252

5353
@Test

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/aot/package-info.java

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)