diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/aot/ValueInstantiatorCustomizerRuntimeHints.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/aot/ValueInstantiatorCustomizerRuntimeHints.java new file mode 100644 index 000000000..e4f69ce23 --- /dev/null +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/aot/ValueInstantiatorCustomizerRuntimeHints.java @@ -0,0 +1,38 @@ +/* + * Copyright 2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.rest.webmvc.aot; + +import org.springframework.aot.hint.RuntimeHints; +import org.springframework.aot.hint.RuntimeHintsRegistrar; +import org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module.AssociationUriResolvingDeserializerModifier.ValueInstantiatorCustomizer; + +import com.fasterxml.jackson.databind.deser.std.StdValueInstantiator; + +/** + * Registers the {@link StdValueInstantiator}'s {@code _constructorArgs} field for reflection as needed by + * {@link ValueInstantiatorCustomizer}. + * + * @author Oliver Drotbohm + * @since 4.0.2 + * @soundtrack The Intersphere - Wanderer (https://www.youtube.com/watch?v=Sp_VyFBbDPA) + */ +class ValueInstantiatorCustomizerRuntimeHints implements RuntimeHintsRegistrar { + + @Override + public void registerHints(RuntimeHints hints, ClassLoader classLoader) { + hints.reflection().registerField(ValueInstantiatorCustomizer.CONSTRUCTOR_ARGS_FIELD); + } +} diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2Module.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2Module.java index 7920c57d0..1722fee08 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2Module.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2Module.java @@ -487,11 +487,19 @@ public class PersistentEntityJackson2Module extends SimpleModule { * @author Oliver Drotbohm * @see https://github.com/FasterXML/jackson-databind/issues/2367 */ - static class ValueInstantiatorCustomizer { + public static class ValueInstantiatorCustomizer { + + public static final Field CONSTRUCTOR_ARGS_FIELD; private final SettableBeanProperty[] properties; private final StdValueInstantiator instantiator; + static { + + CONSTRUCTOR_ARGS_FIELD = ReflectionUtils.findField(StdValueInstantiator.class, "_constructorArguments"); + ReflectionUtils.makeAccessible(CONSTRUCTOR_ARGS_FIELD); + } + ValueInstantiatorCustomizer(ValueInstantiator instantiator, DeserializationConfig config) { this.instantiator = StdValueInstantiator.class.isInstance(instantiator) // @@ -535,9 +543,7 @@ public class PersistentEntityJackson2Module extends SimpleModule { return builder; } - Field field = ReflectionUtils.findField(StdValueInstantiator.class, "_constructorArguments"); - ReflectionUtils.makeAccessible(field); - ReflectionUtils.setField(field, instantiator, properties); + ReflectionUtils.setField(CONSTRUCTOR_ARGS_FIELD, instantiator, properties); builder.setValueInstantiator(instantiator); diff --git a/spring-data-rest-webmvc/src/main/resources/META-INF/spring/aot.factories b/spring-data-rest-webmvc/src/main/resources/META-INF/spring/aot.factories index d606d29af..bd693b9bb 100644 --- a/spring-data-rest-webmvc/src/main/resources/META-INF/spring/aot.factories +++ b/spring-data-rest-webmvc/src/main/resources/META-INF/spring/aot.factories @@ -1,3 +1,5 @@ org.springframework.beans.factory.aot.BeanRegistrationAotProcessor=\ org.springframework.data.rest.webmvc.aot.BasePathAwareControllerAotProcessor,\ org.springframework.data.rest.webmvc.aot.ProjectionProxyAotProcessor +org.springframework.aot.hint.RuntimeHintsRegistrar=\ + org.springframework.data.rest.webmvc.aot.ValueInstantiatorCustomizerRuntimeHints diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/aot/ValueInstantiatorCustomizerRuntimeHintsUnitTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/aot/ValueInstantiatorCustomizerRuntimeHintsUnitTests.java new file mode 100644 index 000000000..e621c2fec --- /dev/null +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/aot/ValueInstantiatorCustomizerRuntimeHintsUnitTests.java @@ -0,0 +1,46 @@ +/* + * Copyright 2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.rest.webmvc.aot; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.Test; +import org.springframework.aot.hint.RuntimeHints; +import org.springframework.aot.hint.RuntimeHintsRegistrar; +import org.springframework.aot.hint.predicate.RuntimeHintsPredicates; + +import com.fasterxml.jackson.databind.deser.std.StdValueInstantiator; + +/** + * Unit tests for {@link ValueInstantiatorCustomizerRuntimeHints}. + * + * @author Oliver Drotbohm + */ +class ValueInstantiatorCustomizerRuntimeHintsUnitTests { + + RuntimeHintsRegistrar registrar = new ValueInstantiatorCustomizerRuntimeHints(); + + @Test // #2213 + void registersHintsForStdValueInstantiator() { + + var hints = new RuntimeHints(); + + registrar.registerHints(hints, getClass().getClassLoader()); + + assertThat(RuntimeHintsPredicates.reflection().onField(StdValueInstantiator.class, "_constructorArguments")) + .accepts(hints); + } +}