From 08f636b691031b9e40ecc5b9ea55c8418151d6b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Fri, 26 Aug 2022 08:30:25 +0200 Subject: [PATCH] Introduce TypeHint.Builder#onReachableType(Class) Closes gh-29017 --- .../quartz/SchedulerFactoryBeanRuntimeHints.java | 4 +--- .../java/org/springframework/aot/hint/TypeHint.java | 12 ++++++++++++ .../nativex/FileNativeConfigurationWriterTests.java | 2 +- .../aot/nativex/ReflectionHintsWriterTests.java | 2 +- .../http/codec/CodecConfigurerRuntimeHints.java | 2 +- .../converter/json/JacksonModulesRuntimeHints.java | 3 +-- 6 files changed, 17 insertions(+), 8 deletions(-) diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBeanRuntimeHints.java b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBeanRuntimeHints.java index dc8a8ace11..26ed71a8cd 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBeanRuntimeHints.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBeanRuntimeHints.java @@ -38,8 +38,6 @@ class SchedulerFactoryBeanRuntimeHints implements RuntimeHintsRegistrar { private static final String SCHEDULER_FACTORY_CLASS_NAME = "org.quartz.impl.StdSchedulerFactory"; - private static final TypeReference FACTORY_BEAN_TYPE_REFERENCE = TypeReference.of(SchedulerFactoryBean.class); - private final ReflectiveRuntimeHintsRegistrar reflectiveRegistrar = new ReflectiveRuntimeHintsRegistrar(); @Override @@ -49,7 +47,7 @@ class SchedulerFactoryBeanRuntimeHints implements RuntimeHintsRegistrar { } Consumer typeHint = type -> type .withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS) - .onReachableType(FACTORY_BEAN_TYPE_REFERENCE); + .onReachableType(SchedulerFactoryBean.class); hints.reflection() .registerType(TypeReference.of(SCHEDULER_FACTORY_CLASS_NAME), typeHint) .registerTypes(TypeReference.listOf(ResourceLoaderClassLoadHelper.class, diff --git a/spring-core/src/main/java/org/springframework/aot/hint/TypeHint.java b/spring-core/src/main/java/org/springframework/aot/hint/TypeHint.java index e7781caabd..e80f9fa8ad 100644 --- a/spring-core/src/main/java/org/springframework/aot/hint/TypeHint.java +++ b/spring-core/src/main/java/org/springframework/aot/hint/TypeHint.java @@ -161,6 +161,18 @@ public final class TypeHint implements ConditionalHint { return this; } + /** + * Make this hint conditional on the fact that the specified type + * can be resolved. + * @param reachableType the type that should be reachable for this + * hint to apply + * @return {@code this}, to facilitate method chaining + */ + public Builder onReachableType(Class reachableType) { + this.reachableType = TypeReference.of(reachableType); + return this; + } + /** * Register the need for reflection on the field with the specified name. * @param name the name of the field diff --git a/spring-core/src/test/java/org/springframework/aot/nativex/FileNativeConfigurationWriterTests.java b/spring-core/src/test/java/org/springframework/aot/nativex/FileNativeConfigurationWriterTests.java index 833ad61d6f..d72f531f2a 100644 --- a/spring-core/src/test/java/org/springframework/aot/nativex/FileNativeConfigurationWriterTests.java +++ b/spring-core/src/test/java/org/springframework/aot/nativex/FileNativeConfigurationWriterTests.java @@ -99,7 +99,7 @@ public class FileNativeConfigurationWriterTests { ReflectionHints reflectionHints = hints.reflection(); reflectionHints.registerType(StringDecoder.class, builder -> { builder - .onReachableType(TypeReference.of(String.class)) + .onReachableType(String.class) .withMembers(MemberCategory.PUBLIC_FIELDS, MemberCategory.DECLARED_FIELDS, MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS, MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, diff --git a/spring-core/src/test/java/org/springframework/aot/nativex/ReflectionHintsWriterTests.java b/spring-core/src/test/java/org/springframework/aot/nativex/ReflectionHintsWriterTests.java index 62abe02908..b47f9a440d 100644 --- a/spring-core/src/test/java/org/springframework/aot/nativex/ReflectionHintsWriterTests.java +++ b/spring-core/src/test/java/org/springframework/aot/nativex/ReflectionHintsWriterTests.java @@ -50,7 +50,7 @@ public class ReflectionHintsWriterTests { void one() throws JSONException { ReflectionHints hints = new ReflectionHints(); hints.registerType(StringDecoder.class, builder -> builder - .onReachableType(TypeReference.of(String.class)) + .onReachableType(String.class) .withMembers(MemberCategory.PUBLIC_FIELDS, MemberCategory.DECLARED_FIELDS, MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS, MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, diff --git a/spring-web/src/main/java/org/springframework/http/codec/CodecConfigurerRuntimeHints.java b/spring-web/src/main/java/org/springframework/http/codec/CodecConfigurerRuntimeHints.java index 3040f552fe..91273acf5e 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/CodecConfigurerRuntimeHints.java +++ b/spring-web/src/main/java/org/springframework/http/codec/CodecConfigurerRuntimeHints.java @@ -38,7 +38,7 @@ import org.springframework.lang.Nullable; class CodecConfigurerRuntimeHints implements RuntimeHintsRegistrar { private static final Consumer CODEC_HINT = type -> type - .onReachableType(TypeReference.of(CodecConfigurerFactory.class)) + .onReachableType(CodecConfigurerFactory.class) .withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS); @Override diff --git a/spring-web/src/main/java/org/springframework/http/converter/json/JacksonModulesRuntimeHints.java b/spring-web/src/main/java/org/springframework/http/converter/json/JacksonModulesRuntimeHints.java index 914fb9ea16..b6619f1f20 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/json/JacksonModulesRuntimeHints.java +++ b/spring-web/src/main/java/org/springframework/http/converter/json/JacksonModulesRuntimeHints.java @@ -22,7 +22,6 @@ import org.springframework.aot.hint.MemberCategory; import org.springframework.aot.hint.RuntimeHints; import org.springframework.aot.hint.RuntimeHintsRegistrar; import org.springframework.aot.hint.TypeHint.Builder; -import org.springframework.aot.hint.TypeReference; /** * {@link RuntimeHintsRegistrar} implementation that registers reflection entries @@ -35,7 +34,7 @@ import org.springframework.aot.hint.TypeReference; class JacksonModulesRuntimeHints implements RuntimeHintsRegistrar { private static final Consumer asJacksonModule = builder -> - builder.onReachableType(TypeReference.of(Jackson2ObjectMapperBuilder.class)) + builder.onReachableType(Jackson2ObjectMapperBuilder.class) .withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS); @Override