Add reflection hints for JsonTesters

Closes gh-32858
This commit is contained in:
Andy Wilkinson
2022-11-09 14:15:07 +00:00
parent 41e2423a0c
commit fcbc7dacf1
3 changed files with 175 additions and 0 deletions

View File

@@ -23,6 +23,11 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import jakarta.json.bind.Jsonb;
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.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
@@ -42,6 +47,7 @@ import org.springframework.boot.test.json.JacksonTester;
import org.springframework.boot.test.json.JsonbTester;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportRuntimeHints;
import org.springframework.context.annotation.Scope;
import org.springframework.core.ResolvableType;
import org.springframework.test.util.ReflectionTestUtils;
@@ -68,6 +74,7 @@ public class JsonTestersAutoConfiguration {
@Bean
@Scope("prototype")
@ImportRuntimeHints(BasicJsonTesterRuntimeHints.class)
public FactoryBean<BasicJsonTester> basicJsonTesterFactoryBean() {
return new JsonTesterFactoryBean<BasicJsonTester, Void>(BasicJsonTester.class, null);
}
@@ -79,10 +86,19 @@ public class JsonTestersAutoConfiguration {
@Bean
@Scope("prototype")
@ConditionalOnBean(ObjectMapper.class)
@ImportRuntimeHints(JacksonTesterRuntimeHints.class)
FactoryBean<JacksonTester<?>> jacksonTesterFactoryBean(ObjectMapper mapper) {
return new JsonTesterFactoryBean<>(JacksonTester.class, mapper);
}
static class JacksonTesterRuntimeHints extends AbstractJsonMarshalTesterRuntimeHints {
JacksonTesterRuntimeHints() {
super(JacksonTester.class);
}
}
}
@Configuration(proxyBeanMethods = false)
@@ -92,10 +108,19 @@ public class JsonTestersAutoConfiguration {
@Bean
@Scope("prototype")
@ConditionalOnBean(Gson.class)
@ImportRuntimeHints(GsonTesterRuntimeHints.class)
FactoryBean<GsonTester<?>> gsonTesterFactoryBean(Gson gson) {
return new JsonTesterFactoryBean<>(GsonTester.class, gson);
}
static class GsonTesterRuntimeHints extends AbstractJsonMarshalTesterRuntimeHints {
GsonTesterRuntimeHints() {
super(GsonTester.class);
}
}
}
@Configuration(proxyBeanMethods = false)
@@ -105,10 +130,19 @@ public class JsonTestersAutoConfiguration {
@Bean
@Scope("prototype")
@ConditionalOnBean(Jsonb.class)
@ImportRuntimeHints(JsonbJsonTesterRuntimeHints.class)
FactoryBean<JsonbTester<?>> jsonbTesterFactoryBean(Jsonb jsonb) {
return new JsonTesterFactoryBean<>(JsonbTester.class, jsonb);
}
static class JsonbJsonTesterRuntimeHints extends AbstractJsonMarshalTesterRuntimeHints {
JsonbJsonTesterRuntimeHints() {
super(JsonbTester.class);
}
}
}
/**
@@ -189,4 +223,36 @@ public class JsonTestersAutoConfiguration {
}
@SuppressWarnings("rawtypes")
static class AbstractJsonMarshalTesterRuntimeHints implements RuntimeHintsRegistrar {
private final Class<? extends AbstractJsonMarshalTester> tester;
AbstractJsonMarshalTesterRuntimeHints(Class<? extends AbstractJsonMarshalTester> tester) {
this.tester = tester;
}
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
ReflectionHints reflection = hints.reflection();
reflection.registerType(this.tester, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
reflection.registerMethod(
ReflectionUtils.findMethod(this.tester, "initialize", Class.class, ResolvableType.class),
ExecutableMode.INVOKE);
}
}
static class BasicJsonTesterRuntimeHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
ReflectionHints reflection = hints.reflection();
reflection.registerType(BasicJsonTester.class, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
reflection.registerMethod(ReflectionUtils.findMethod(BasicJsonTester.class, "initialize", Class.class),
ExecutableMode.INVOKE);
}
}
}