Skip to content

Commit 0db1ada

Browse files
committed
Register StdValueInstantiator._constructorArguments for reflection.
The field is looked up by reflection in ValueInstantiatorCustomizer. Fixes #2213.
1 parent a7765c9 commit 0db1ada

File tree

4 files changed

+96
-4
lines changed

4 files changed

+96
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.rest.webmvc.aot;
17+
18+
import org.springframework.aot.hint.RuntimeHints;
19+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
20+
import org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module.AssociationUriResolvingDeserializerModifier.ValueInstantiatorCustomizer;
21+
22+
import com.fasterxml.jackson.databind.deser.std.StdValueInstantiator;
23+
24+
/**
25+
* Registers the {@link StdValueInstantiator}'s {@code _constructorArgs} field for reflection as needed by
26+
* {@link ValueInstantiatorCustomizer}.
27+
*
28+
* @author Oliver Drotbohm
29+
* @since 4.0.2
30+
* @soundtrack The Intersphere - Wanderer (https://www.youtube.com/watch?v=Sp_VyFBbDPA)
31+
*/
32+
class ValueInstantiatorCustomizerRuntimeHints implements RuntimeHintsRegistrar {
33+
34+
@Override
35+
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
36+
hints.reflection().registerField(ValueInstantiatorCustomizer.CONSTRUCTOR_ARGS_FIELD);
37+
}
38+
}

spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2Module.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -487,11 +487,19 @@ public BeanDeserializerBuilder updateBuilder(DeserializationConfig config, BeanD
487487
* @author Oliver Drotbohm
488488
* @see https://github.com/FasterXML/jackson-databind/issues/2367
489489
*/
490-
static class ValueInstantiatorCustomizer {
490+
public static class ValueInstantiatorCustomizer {
491+
492+
public static final Field CONSTRUCTOR_ARGS_FIELD;
491493

492494
private final SettableBeanProperty[] properties;
493495
private final StdValueInstantiator instantiator;
494496

497+
static {
498+
499+
CONSTRUCTOR_ARGS_FIELD = ReflectionUtils.findField(StdValueInstantiator.class, "_constructorArguments");
500+
ReflectionUtils.makeAccessible(CONSTRUCTOR_ARGS_FIELD);
501+
}
502+
495503
ValueInstantiatorCustomizer(ValueInstantiator instantiator, DeserializationConfig config) {
496504

497505
this.instantiator = StdValueInstantiator.class.isInstance(instantiator) //
@@ -535,9 +543,7 @@ BeanDeserializerBuilder conclude(BeanDeserializerBuilder builder) {
535543
return builder;
536544
}
537545

538-
Field field = ReflectionUtils.findField(StdValueInstantiator.class, "_constructorArguments");
539-
ReflectionUtils.makeAccessible(field);
540-
ReflectionUtils.setField(field, instantiator, properties);
546+
ReflectionUtils.setField(CONSTRUCTOR_ARGS_FIELD, instantiator, properties);
541547

542548
builder.setValueInstantiator(instantiator);
543549

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
org.springframework.beans.factory.aot.BeanRegistrationAotProcessor=\
22
org.springframework.data.rest.webmvc.aot.BasePathAwareControllerAotProcessor,\
33
org.springframework.data.rest.webmvc.aot.ProjectionProxyAotProcessor
4+
org.springframework.aot.hint.RuntimeHintsRegistrar=\
5+
org.springframework.data.rest.webmvc.aot.ValueInstantiatorCustomizerRuntimeHints
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.rest.webmvc.aot;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import org.junit.jupiter.api.Test;
21+
import org.springframework.aot.hint.RuntimeHints;
22+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
23+
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
24+
25+
import com.fasterxml.jackson.databind.deser.std.StdValueInstantiator;
26+
27+
/**
28+
* Unit tests for {@link ValueInstantiatorCustomizerRuntimeHints}.
29+
*
30+
* @author Oliver Drotbohm
31+
*/
32+
class ValueInstantiatorCustomizerRuntimeHintsUnitTests {
33+
34+
RuntimeHintsRegistrar registrar = new ValueInstantiatorCustomizerRuntimeHints();
35+
36+
@Test // #2213
37+
void registersHintsForStdValueInstantiator() {
38+
39+
var hints = new RuntimeHints();
40+
41+
registrar.registerHints(hints, getClass().getClassLoader());
42+
43+
assertThat(RuntimeHintsPredicates.reflection().onField(StdValueInstantiator.class, "_constructorArguments"))
44+
.accepts(hints);
45+
}
46+
}

0 commit comments

Comments
 (0)