Add classpath checks to JacksonModulesRuntimeHints

Closes gh-29001
This commit is contained in:
Sébastien Deleuze 2022-08-23 12:55:26 +02:00
parent 5525b17f19
commit 0973348bde
1 changed files with 10 additions and 7 deletions

View File

@ -21,6 +21,7 @@ import org.springframework.aot.hint.ReflectionHints;
import org.springframework.aot.hint.RuntimeHints; import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar; import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeReference; import org.springframework.aot.hint.TypeReference;
import org.springframework.util.ClassUtils;
/** /**
* {@link RuntimeHintsRegistrar} implementation that registers reflection entries * {@link RuntimeHintsRegistrar} implementation that registers reflection entries
@ -34,14 +35,16 @@ class JacksonModulesRuntimeHints implements RuntimeHintsRegistrar {
@Override @Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) { public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
ReflectionHints reflectionHints = hints.reflection(); ReflectionHints reflectionHints = hints.reflection();
registerType(reflectionHints, "com.fasterxml.jackson.datatype.jdk8.Jdk8Module"); registerType(reflectionHints, "com.fasterxml.jackson.datatype.jdk8.Jdk8Module", classLoader);
registerType(reflectionHints, "com.fasterxml.jackson.datatype.jsr310.JavaTimeModule"); registerType(reflectionHints, "com.fasterxml.jackson.datatype.jsr310.JavaTimeModule", classLoader);
registerType(reflectionHints, "com.fasterxml.jackson.module.kotlin.KotlinModule"); registerType(reflectionHints, "com.fasterxml.jackson.module.kotlin.KotlinModule", classLoader);
} }
private void registerType(ReflectionHints reflectionHints, String className) { private void registerType(ReflectionHints reflectionHints, String className, ClassLoader classLoader) {
if (ClassUtils.isPresent(className, classLoader)) {
reflectionHints.registerType(TypeReference.of(className), reflectionHints.registerType(TypeReference.of(className),
builder -> builder.onReachableType(TypeReference.of(Jackson2ObjectMapperBuilder.class)) builder -> builder.onReachableType(TypeReference.of(Jackson2ObjectMapperBuilder.class))
.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)); .withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS));
} }
}
} }