forked from FasterXML/jackson-modules-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BBDeserializerModifier.java
276 lines (249 loc) · 11.3 KB
/
BBDeserializerModifier.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package com.fasterxml.jackson.module.blackbird.deser;
import java.lang.invoke.LambdaConversionException;
import java.lang.invoke.LambdaMetafactory;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.invoke.MethodHandles.Lookup;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.ObjIntConsumer;
import java.util.function.ObjLongConsumer;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.*;
import com.fasterxml.jackson.databind.deser.impl.MethodProperty;
import com.fasterxml.jackson.databind.deser.std.StdValueInstantiator;
import com.fasterxml.jackson.databind.introspect.*;
import com.fasterxml.jackson.databind.util.ClassUtil;
import com.fasterxml.jackson.module.blackbird.util.ReflectionHack;
public class BBDeserializerModifier extends BeanDeserializerModifier
{
private static final MethodHandle TRAMPOLINE, BOOLEAN_TRAMPOLINE, LONG_TRAMPOLINE, INT_TRAMPOLINE;
static {
try {
TRAMPOLINE = MethodHandles.lookup().findStatic(BBDeserializerModifier.class, "trampoline",
MethodType.methodType(void.class, BiFunction.class, Object.class, Object.class));
BOOLEAN_TRAMPOLINE = MethodHandles.lookup().findStatic(BBDeserializerModifier.class, "booleanTrampoline",
MethodType.methodType(void.class, ObjBooleanBiFunction.class, Object.class, boolean.class));
LONG_TRAMPOLINE = MethodHandles.lookup().findStatic(BBDeserializerModifier.class, "longTrampoline",
MethodType.methodType(void.class, ObjLongBiFunction.class, Object.class, long.class));
INT_TRAMPOLINE = MethodHandles.lookup().findStatic(BBDeserializerModifier.class, "intTrampoline",
MethodType.methodType(void.class, ObjIntBiFunction.class, Object.class, int.class));
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
private Function<Class<?>, Lookup> _lookups;
public BBDeserializerModifier(Function<Class<?>, MethodHandles.Lookup> lookups)
{
_lookups = lookups;
}
/*
/**********************************************************************
/* BeanDeserializerModifier methods
/**********************************************************************
*/
@Override
public BeanDeserializerBuilder updateBuilder(DeserializationConfig config,
BeanDescription beanDesc, BeanDeserializerBuilder builder)
{
final Class<?> beanClass = beanDesc.getBeanClass();
MethodHandles.Lookup lookup = _lookups.apply(beanClass);
if (lookup == null) {
return builder;
}
try {
lookup = ReflectionHack.privateLookupIn(beanClass, lookup);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
/* Hmmh. Can we access stuff from private classes?
* Possibly, if we can use parent class loader.
* (should probably skip all non-public?)
*/
if (Modifier.isPrivate(beanClass.getModifiers())) { // TODO??
return builder;
}
List<OptimizedSettableBeanProperty<?>> newProps = findOptimizableProperties(
lookup, config, builder.getProperties());
// and if we found any, create mutator proxy, replace property objects
if (!newProps.isEmpty()) {
for (OptimizedSettableBeanProperty<?> prop : newProps) {
builder.addOrReplaceProperty(prop, true);
}
}
// Second thing: see if we could (re)generate Creator(s):
ValueInstantiator inst = builder.getValueInstantiator();
/* Hmmh. Probably better to require exact default implementation
* and not sub-class; chances are sub-class uses its own
* construction anyway.
*/
if (inst.getClass() == StdValueInstantiator.class) {
// also, only override if using default creator (no-arg ctor, no-arg static factory)
if (inst.canCreateUsingDefault() || inst.canCreateFromObjectWith()) {
inst = new CreatorOptimizer(beanClass, lookup, (StdValueInstantiator) inst).createOptimized();
if (inst != null) {
builder.setValueInstantiator(inst);
}
}
}
// also: may want to replace actual BeanDeserializer as well? For this, need to replace builder
// (but only if builder is the original standard one; don't want to break other impls)
if (builder.getClass() == BeanDeserializerBuilder.class) {
return new SuperSonicDeserializerBuilder(builder);
}
return builder;
}
/*
/**********************************************************************
/* Internal methods
/**********************************************************************
*/
protected List<OptimizedSettableBeanProperty<?>> findOptimizableProperties(
Lookup lookup, DeserializationConfig config,
Iterator<SettableBeanProperty> propIterator)
{
ArrayList<OptimizedSettableBeanProperty<?>> newProps = new ArrayList<OptimizedSettableBeanProperty<?>>();
// Ok, then, find any properties for which we could generate accessors
while (propIterator.hasNext()) {
try {
nextProperty(propIterator.next(), lookup, newProps);
} catch (Throwable e) {
if (e instanceof Error) {
throw (Error) e;
}
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
throw new RuntimeException(e);
}
}
return newProps;
}
@SuppressWarnings("unchecked")
private void nextProperty(SettableBeanProperty prop,
Lookup lookup,
ArrayList<OptimizedSettableBeanProperty<?>> newProps) throws Throwable
{
AnnotatedMember member = prop.getMember();
Member jdkMember = member.getMember();
// if we ever support virtual properties, this would be null, so check, skip
if (jdkMember == null) {
return;
}
// First: we can't access private fields or methods....
if (Modifier.isPrivate(jdkMember.getModifiers())) {
return;
}
// (although, interestingly enough, can seem to access private classes...)
// 30-Jul-2012, tatu: [module-afterburner#6]: Needs to skip custom deserializers, if any.
if (prop.hasValueDeserializer()) {
if (!isDefaultDeserializer(prop.getValueDeserializer())) {
return;
}
}
MethodHandle setter;
Class<?> type;
if (jdkMember instanceof Method && prop instanceof MethodProperty) {
final Method method = (Method) jdkMember;
setter = lookup.unreflect(method);
type = ((AnnotatedMethod) member).getRawParameterType(0);
} else {
return;
//setter = lookup.unreflectGetter((Field) jdkMember);
}
if (type.isPrimitive()) {
if (type == Integer.TYPE) {
newProps.add(new SettableIntProperty(prop,
createSetter(lookup, ObjIntConsumer.class, ObjIntBiFunction.class,
INT_TRAMPOLINE, int.class, setter)));
} else if (type == Long.TYPE) {
newProps.add(new SettableLongProperty(prop,
createSetter(lookup, ObjLongConsumer.class, ObjLongBiFunction.class,
LONG_TRAMPOLINE, long.class, setter)));
} else if (type == Boolean.TYPE) {
newProps.add(new SettableBooleanProperty(prop,
createSetter(lookup, ObjBooleanConsumer.class, ObjBooleanBiFunction.class,
BOOLEAN_TRAMPOLINE, boolean.class, setter)));
}
} else {
if (type == String.class) {
newProps.add(new SettableStringProperty(prop,
createSetter(lookup, BiConsumer.class, BiFunction.class, TRAMPOLINE, Object.class, setter)));
} else {
newProps.add(new SettableObjectProperty(prop,
createSetter(lookup, BiConsumer.class, BiFunction.class, TRAMPOLINE, Object.class, setter)));
}
}
}
private <T> T createSetter(Lookup lookup, Class<T> iface, Class<?> thunkType, MethodHandle trampoline, Class<?> valueType, MethodHandle setter)
throws Throwable, LambdaConversionException {
if (setter.type().returnType() == void.class) {
return iface.cast(LambdaMetafactory.metafactory(
lookup,
"accept",
MethodType.methodType(iface),
MethodType.methodType(void.class, Object.class, valueType),
setter,
setter.type())
.getTarget().invoke());
}
Object builtThunk = LambdaMetafactory.metafactory(
lookup,
"apply",
MethodType.methodType(thunkType),
MethodType.methodType(Object.class, Object.class, valueType),
setter,
setter.type())
.getTarget().invoke();
return iface.cast(LambdaMetafactory.metafactory(
MethodHandles.lookup(),
"accept",
MethodType.methodType(iface, thunkType),
MethodType.methodType(void.class, Object.class, valueType),
trampoline,
MethodType.methodType(void.class, Object.class, valueType))
.getTarget().invoke(builtThunk));
}
/**
* Helper method used to check whether given deserializer is the default
* deserializer implementation: this is necessary to avoid overriding other
* kinds of deserializers.
*/
protected boolean isDefaultDeserializer(JsonDeserializer<?> deser) {
return ClassUtil.isJacksonStdImpl(deser)
// 07-May-2018, tatu: Probably can't happen but just in case
|| (deser instanceof SuperSonicBeanDeserializer);
}
// These trampolines adapt BiFunction-like setters (i.e. returns a value)
// to BiConsumer style through a generated thunk lambda.
@FunctionalInterface
public interface ObjIntBiFunction {
Object apply(Object bean, int value);
}
@FunctionalInterface
public interface ObjLongBiFunction {
Object apply(Object bean, long value);
}
@FunctionalInterface
public interface ObjBooleanBiFunction {
Object apply(Object bean, boolean value);
}
static void intTrampoline(ObjIntBiFunction thunk, Object bean, int value) {
thunk.apply(bean, value);
}
static void longTrampoline(ObjLongBiFunction thunk, Object bean, long value) {
thunk.apply(bean, value);
}
static void booleanTrampoline(ObjBooleanBiFunction thunk, Object bean, boolean value) {
thunk.apply(bean, value);
}
static void trampoline(BiFunction<Object, Object, Object> thunk, Object bean, Object value) {
thunk.apply(bean, value);
}
}