Register StdValueInstantiator._constructorArguments for reflection.

The field is looked up by reflection in ValueInstantiatorCustomizer.

Fixes #2213.
This commit is contained in:
Oliver Drotbohm
2023-01-20 16:23:37 +01:00
parent 2c9b0c829f
commit 8819cdc741
4 changed files with 96 additions and 4 deletions

View File

@@ -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);
}
}

View File

@@ -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);

View File

@@ -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

View File

@@ -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);
}
}