Skip to content

Commit 3e5d6c9

Browse files
committed
Register ServiceLevelObjectiveBoundary reflection hinds
Register hints for `ServiceLevelObjectiveBoundary` so that the `valueOf` method can be used when binding in a native image. Fixes gh-40480
1 parent 362c0c1 commit 3e5d6c9

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2024 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.
@@ -20,6 +20,10 @@
2020

2121
import io.micrometer.core.instrument.Meter;
2222

23+
import org.springframework.aot.hint.MemberCategory;
24+
import org.springframework.aot.hint.RuntimeHints;
25+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
26+
2327
/**
2428
* A boundary for a service-level objective (SLO) for use when configuring Micrometer. Can
2529
* be specified as either a {@link Double} (applicable to timers and distribution
@@ -67,4 +71,13 @@ public static ServiceLevelObjectiveBoundary valueOf(String value) {
6771
return new ServiceLevelObjectiveBoundary(MeterValue.valueOf(value));
6872
}
6973

74+
static class ServiceLevelObjectiveBoundaryHints implements RuntimeHintsRegistrar {
75+
76+
@Override
77+
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
78+
hints.reflection().registerType(ServiceLevelObjectiveBoundary.class, MemberCategory.INVOKE_PUBLIC_METHODS);
79+
}
80+
81+
}
82+
7083
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
org.springframework.aot.hint.RuntimeHintsRegistrar=\
2+
org.springframework.boot.actuate.autoconfigure.metrics.ServiceLevelObjectiveBoundary.ServiceLevelObjectiveBoundaryHints

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/ServiceLevelObjectiveBoundaryTests.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2024 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.
@@ -21,6 +21,10 @@
2121
import io.micrometer.core.instrument.Meter.Type;
2222
import org.junit.jupiter.api.Test;
2323

24+
import org.springframework.aot.hint.RuntimeHints;
25+
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
26+
import org.springframework.util.ReflectionUtils;
27+
2428
import static org.assertj.core.api.Assertions.assertThat;
2529

2630
/**
@@ -73,4 +77,16 @@ void getValueForDistributionSummaryWhenFromDurationShouldReturnNull() {
7377
assertThat(slo.getValue(Type.DISTRIBUTION_SUMMARY)).isNull();
7478
}
7579

80+
@Test
81+
void shouldRegisterRuntimeHints() {
82+
RuntimeHints runtimeHints = new RuntimeHints();
83+
new ServiceLevelObjectiveBoundary.ServiceLevelObjectiveBoundaryHints().registerHints(runtimeHints,
84+
getClass().getClassLoader());
85+
ReflectionUtils.doWithLocalMethods(ServiceLevelObjectiveBoundary.class, (method) -> {
86+
if ("valueOf".equals(method.getName())) {
87+
assertThat(RuntimeHintsPredicates.reflection().onMethod(method)).accepts(runtimeHints);
88+
}
89+
});
90+
}
91+
7692
}

0 commit comments

Comments
 (0)