Skip to content

Commit 66c1129

Browse files
committed
Add runtime hints for timeout methods with Duration overloads
Closes gh-47675
1 parent bc6a92a commit 66c1129

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/http/client/ClientHttpRequestFactoryRuntimeHints.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.lang.reflect.Field;
2020
import java.lang.reflect.Method;
2121
import java.net.HttpURLConnection;
22+
import java.time.Duration;
2223

2324
import org.springframework.aot.hint.ExecutableMode;
2425
import org.springframework.aot.hint.ReflectionHints;
@@ -87,7 +88,9 @@ private void registerReflectionHints(ReflectionHints hints,
8788
private void registerReflectionHints(ReflectionHints hints,
8889
Class<? extends ClientHttpRequestFactory> requestFactoryType, Class<?> readTimeoutType) {
8990
registerMethod(hints, requestFactoryType, "setConnectTimeout", int.class);
91+
registerMethod(hints, requestFactoryType, "setConnectTimeout", Duration.class);
9092
registerMethod(hints, requestFactoryType, "setReadTimeout", readTimeoutType);
93+
registerMethod(hints, requestFactoryType, "setReadTimeout", Duration.class);
9194
}
9295

9396
private void registerMethod(ReflectionHints hints, Class<? extends ClientHttpRequestFactory> requestFactoryType,

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/http/client/ClientHttpRequestFactoryRuntimeHintsTests.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.lang.reflect.Field;
2020
import java.lang.reflect.Method;
21+
import java.time.Duration;
2122

2223
import org.junit.jupiter.api.Test;
2324

@@ -26,6 +27,7 @@
2627
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
2728
import org.springframework.http.client.AbstractClientHttpRequestFactoryWrapper;
2829
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
30+
import org.springframework.http.client.JdkClientHttpRequestFactory;
2931
import org.springframework.http.client.JettyClientHttpRequestFactory;
3032
import org.springframework.http.client.ReactorClientHttpRequestFactory;
3133
import org.springframework.http.client.SimpleClientHttpRequestFactory;
@@ -60,6 +62,15 @@ void shouldRegisterHttpComponentHints() {
6062
assertThat(reflection
6163
.onMethod(method(HttpComponentsClientHttpRequestFactory.class, "setConnectTimeout", int.class)))
6264
.accepts(hints);
65+
assertThat(reflection
66+
.onMethod(method(HttpComponentsClientHttpRequestFactory.class, "setConnectTimeout", Duration.class)))
67+
.accepts(hints);
68+
assertThat(
69+
reflection.onMethod(method(HttpComponentsClientHttpRequestFactory.class, "setReadTimeout", int.class)))
70+
.accepts(hints);
71+
assertThat(reflection
72+
.onMethod(method(HttpComponentsClientHttpRequestFactory.class, "setReadTimeout", Duration.class)))
73+
.accepts(hints);
6374
}
6475

6576
@Test
@@ -69,8 +80,13 @@ void shouldRegisterJettyClientHints() {
6980
ReflectionHintsPredicates reflection = RuntimeHintsPredicates.reflection();
7081
assertThat(reflection.onMethod(method(JettyClientHttpRequestFactory.class, "setConnectTimeout", int.class)))
7182
.accepts(hints);
83+
assertThat(
84+
reflection.onMethod(method(JettyClientHttpRequestFactory.class, "setConnectTimeout", Duration.class)))
85+
.accepts(hints);
7286
assertThat(reflection.onMethod(method(JettyClientHttpRequestFactory.class, "setReadTimeout", long.class)))
7387
.accepts(hints);
88+
assertThat(reflection.onMethod(method(JettyClientHttpRequestFactory.class, "setReadTimeout", Duration.class)))
89+
.accepts(hints);
7490
}
7591

7692
@Test
@@ -80,8 +96,13 @@ void shouldRegisterReactorHints() {
8096
ReflectionHintsPredicates reflection = RuntimeHintsPredicates.reflection();
8197
assertThat(reflection.onMethod(method(ReactorClientHttpRequestFactory.class, "setConnectTimeout", int.class)))
8298
.accepts(hints);
99+
assertThat(
100+
reflection.onMethod(method(ReactorClientHttpRequestFactory.class, "setConnectTimeout", Duration.class)))
101+
.accepts(hints);
83102
assertThat(reflection.onMethod(method(ReactorClientHttpRequestFactory.class, "setReadTimeout", long.class)))
84103
.accepts(hints);
104+
assertThat(reflection.onMethod(method(ReactorClientHttpRequestFactory.class, "setReadTimeout", Duration.class)))
105+
.accepts(hints);
85106
}
86107

87108
@Test
@@ -91,8 +112,24 @@ void shouldRegisterSimpleHttpHints() {
91112
ReflectionHintsPredicates reflection = RuntimeHintsPredicates.reflection();
92113
assertThat(reflection.onMethod(method(SimpleClientHttpRequestFactory.class, "setConnectTimeout", int.class)))
93114
.accepts(hints);
115+
assertThat(
116+
reflection.onMethod(method(SimpleClientHttpRequestFactory.class, "setConnectTimeout", Duration.class)))
117+
.accepts(hints);
94118
assertThat(reflection.onMethod(method(SimpleClientHttpRequestFactory.class, "setReadTimeout", int.class)))
95119
.accepts(hints);
120+
assertThat(reflection.onMethod(method(SimpleClientHttpRequestFactory.class, "setReadTimeout", Duration.class)))
121+
.accepts(hints);
122+
}
123+
124+
@Test
125+
void shouldRegisterJdkHttpHints() {
126+
RuntimeHints hints = new RuntimeHints();
127+
new ClientHttpRequestFactoryRuntimeHints().registerHints(hints, getClass().getClassLoader());
128+
ReflectionHintsPredicates reflection = RuntimeHintsPredicates.reflection();
129+
assertThat(reflection.onMethod(method(JdkClientHttpRequestFactory.class, "setReadTimeout", int.class)))
130+
.accepts(hints);
131+
assertThat(reflection.onMethod(method(JdkClientHttpRequestFactory.class, "setReadTimeout", Duration.class)))
132+
.accepts(hints);
96133
}
97134

98135
private static Method method(Class<?> target, String name, Class<?>... parameterTypes) {

0 commit comments

Comments
 (0)