Add missing hint for converting String to URI

Closes gh-30627
This commit is contained in:
Sébastien Deleuze 2023-06-09 14:11:46 +02:00
parent 071d6a2a5a
commit 9b4e0e9837
2 changed files with 12 additions and 2 deletions

View File

@ -21,6 +21,8 @@ import java.util.Collections;
import java.util.List;
import org.springframework.aot.hint.ExecutableMode;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.ReflectionHints;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeReference;
@ -37,15 +39,17 @@ class ObjectToObjectConverterRuntimeHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
ReflectionHints reflectionHints = hints.reflection();
TypeReference sqlDateTypeReference = TypeReference.of("java.sql.Date");
hints.reflection().registerTypeIfPresent(classLoader, sqlDateTypeReference.getName(), hint -> hint
reflectionHints.registerTypeIfPresent(classLoader, sqlDateTypeReference.getName(), hint -> hint
.withMethod("toLocalDate", Collections.emptyList(), ExecutableMode.INVOKE)
.onReachableType(sqlDateTypeReference)
.withMethod("valueOf", List.of(TypeReference.of(LocalDate.class)), ExecutableMode.INVOKE)
.onReachableType(sqlDateTypeReference));
hints.reflection().registerTypeIfPresent(classLoader, "org.springframework.http.HttpMethod",
reflectionHints.registerTypeIfPresent(classLoader, "org.springframework.http.HttpMethod",
builder -> builder.withMethod("valueOf", List.of(TypeReference.of(String.class)), ExecutableMode.INVOKE));
reflectionHints.registerTypeIfPresent(classLoader, "java.net.URI", MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
}
}

View File

@ -16,6 +16,7 @@
package org.springframework.aot.hint.support;
import java.net.URI;
import java.time.LocalDate;
import org.junit.jupiter.api.BeforeEach;
@ -52,4 +53,9 @@ class ObjectToObjectConverterRuntimeHintsTests {
assertThat(RuntimeHintsPredicates.reflection().onMethod(java.sql.Date.class.getMethod("valueOf", LocalDate.class))).accepts(this.hints);
}
@Test
void uriHasHints() throws NoSuchMethodException {
assertThat(RuntimeHintsPredicates.reflection().onConstructor(URI.class.getConstructor(String.class))).accepts(this.hints);
}
}