-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathPropertyValueBuffer.java
312 lines (282 loc) · 11.2 KB
/
PropertyValueBuffer.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package com.fasterxml.jackson.databind.deser.impl;
import java.io.IOException;
import java.util.BitSet;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.SettableAnyProperty;
import com.fasterxml.jackson.databind.deser.SettableBeanProperty;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
/**
* Simple container used for temporarily buffering a set of
* <code>PropertyValue</code>s.
* Using during construction of beans (and Maps) that use Creators,
* and hence need buffering before instance (that will have properties
* to assign values to) is constructed.
*/
public class PropertyValueBuffer
{
/*
/**********************************************************
/* Configuration
/**********************************************************
*/
protected final JsonParser _parser;
protected final DeserializationContext _context;
protected final ObjectIdReader _objectIdReader;
/*
/**********************************************************
/* Accumulated properties, other stuff
/**********************************************************
*/
/**
* Buffer used for storing creator parameters for constructing
* instance.
*/
protected final Object[] _creatorParameters;
/**
* Number of creator parameters for which we have not yet received
* values.
*/
protected int _paramsNeeded;
/**
* Bitflag used to track parameters found from incoming data
* when number of parameters is
* less than 32 (fits in int).
*/
protected int _paramsSeen;
/**
* Bitflag used to track parameters found from incoming data
* when number of parameters is
* 32 or higher.
*/
protected final BitSet _paramsSeenBig;
/**
* If we get non-creator parameters before or between
* creator parameters, those need to be buffered. Buffer
* is just a simple linked list
*/
protected PropertyValue _buffered;
/**
* In case there is an Object Id property to handle, this is the value
* we have for it.
*/
protected Object _idValue;
/*
/**********************************************************
/* Life-cycle
/**********************************************************
*/
public PropertyValueBuffer(JsonParser p, DeserializationContext ctxt, int paramCount,
ObjectIdReader oir)
{
_parser = p;
_context = ctxt;
_paramsNeeded = paramCount;
_objectIdReader = oir;
_creatorParameters = new Object[paramCount];
if (paramCount < 32) {
_paramsSeenBig = null;
} else {
_paramsSeenBig = new BitSet();
}
}
/**
* Returns {@code true} if the given property was seen in the JSON source by
* this buffer.
*
* @since 2.8
*/
public final boolean hasParameter(SettableBeanProperty prop)
{
if (_paramsSeenBig == null) {
return ((_paramsSeen >> prop.getCreatorIndex()) & 1) == 1;
}
return _paramsSeenBig.get(prop.getCreatorIndex());
}
/**
* A variation of {@link #getParameters(SettableBeanProperty[])} that
* accepts a single property. Whereas the plural form eagerly fetches and
* validates all properties, this method may be used (along with
* {@link #hasParameter(SettableBeanProperty)}) to let applications only
* fetch the properties defined in the JSON source itself, and to have some
* other customized behavior for missing properties.
*
* @since 2.8
*/
public Object getParameter(SettableBeanProperty prop)
throws JsonMappingException
{
Object value;
if (hasParameter(prop)) {
value = _creatorParameters[prop.getCreatorIndex()];
} else {
value = _creatorParameters[prop.getCreatorIndex()] = _findMissing(prop);
}
if (value == null && _context.isEnabled(DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES)) {
return _context.reportInputMismatch(prop,
"Null value for creator property '%s' (index %d); `DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES` enabled",
prop.getName(), prop.getCreatorIndex());
}
return value;
}
/**
* Method called to do necessary post-processing such as injection of values
* and verification of values for required properties,
* after either {@link #assignParameter(SettableBeanProperty, Object)}
* returns <code>true</code> (to indicate all creator properties are found), or when
* then whole JSON Object has been processed,
*/
public Object[] getParameters(SettableBeanProperty[] props)
throws JsonMappingException
{
// quick check to see if anything else is needed
if (_paramsNeeded > 0) {
if (_paramsSeenBig == null) {
int mask = _paramsSeen;
// not optimal, could use `Integer.trailingZeroes()`, but for now should not
// really matter for common cases
for (int ix = 0, len = _creatorParameters.length; ix < len; ++ix, mask >>= 1) {
if ((mask & 1) == 0) {
_creatorParameters[ix] = _findMissing(props[ix]);
}
}
} else {
final int len = _creatorParameters.length;
for (int ix = 0; (ix = _paramsSeenBig.nextClearBit(ix)) < len; ++ix) {
_creatorParameters[ix] = _findMissing(props[ix]);
}
}
}
if (_context.isEnabled(DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES)) {
for (int ix = 0; ix < props.length; ++ix) {
if (_creatorParameters[ix] == null) {
SettableBeanProperty prop = props[ix];
_context.reportInputMismatch(prop,
"Null value for creator property '%s' (index %d); `DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES` enabled",
prop.getName(), props[ix].getCreatorIndex());
}
}
}
return _creatorParameters;
}
protected Object _findMissing(SettableBeanProperty prop) throws JsonMappingException
{
// First: do we have injectable value?
Object injectableValueId = prop.getInjectableValueId();
if (injectableValueId != null) {
return _context.findInjectableValue(prop.getInjectableValueId(),
prop, null);
}
// Second: required?
if (prop.isRequired()) {
_context.reportInputMismatch(prop, "Missing required creator property '%s' (index %d)",
prop.getName(), prop.getCreatorIndex());
}
if (_context.isEnabled(DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES)) {
_context.reportInputMismatch(prop,
"Missing creator property '%s' (index %d); `DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES` enabled",
prop.getName(), prop.getCreatorIndex());
}
try {
// Third: NullValueProvider? (22-Sep-2019, [databind#2458])
// 08-Aug-2021, tatu: consider [databind#3214]; not null but "absent" value...
Object absentValue = prop.getNullValueProvider().getAbsentValue(_context);
if (absentValue != null) {
return absentValue;
}
// Fourth: default value
JsonDeserializer<Object> deser = prop.getValueDeserializer();
return deser.getAbsentValue(_context);
} catch (DatabindException e) {
// [databind#2101]: Include property name, if we have it
AnnotatedMember member = prop.getMember();
if (member != null) {
e.prependPath(member.getDeclaringClass(), prop.getName());
}
throw e;
}
}
/*
/**********************************************************
/* Other methods
/**********************************************************
*/
/**
* Helper method called to see if given non-creator property is the "id property";
* and if so, handle appropriately.
*
* @since 2.1
*/
public boolean readIdProperty(String propName) throws IOException
{
if ((_objectIdReader != null) && propName.equals(_objectIdReader.propertyName.getSimpleName())) {
_idValue = _objectIdReader.readObjectReference(_parser, _context);
return true;
}
return false;
}
/**
* Helper method called to handle Object Id value collected earlier, if any
*/
public Object handleIdValue(final DeserializationContext ctxt, Object bean) throws IOException
{
if (_objectIdReader != null) {
if (_idValue != null) {
ReadableObjectId roid = ctxt.findObjectId(_idValue, _objectIdReader.generator, _objectIdReader.resolver);
roid.bindItem(bean);
// also: may need to set a property value as well
SettableBeanProperty idProp = _objectIdReader.idProperty;
if (idProp != null) {
return idProp.setAndReturn(bean, _idValue);
}
} else {
// 07-Jun-2016, tatu: Trying to improve error messaging here...
ctxt.reportUnresolvedObjectId(_objectIdReader, bean);
}
}
return bean;
}
protected PropertyValue buffered() { return _buffered; }
public boolean isComplete() { return _paramsNeeded <= 0; }
/**
* Method called to buffer value for given property, as well as check whether
* we now have values for all (creator) properties that we expect to get values for.
*
* @return True if we have received all creator parameters
*
* @since 2.6
*/
public boolean assignParameter(SettableBeanProperty prop, Object value)
{
final int ix = prop.getCreatorIndex();
_creatorParameters[ix] = value;
if (_paramsSeenBig == null) {
int old = _paramsSeen;
int newValue = (old | (1 << ix));
if (old != newValue) {
_paramsSeen = newValue;
if (--_paramsNeeded <= 0) {
// 29-Nov-2016, tatu: But! May still require Object Id value
return (_objectIdReader == null) || (_idValue != null);
}
}
} else {
if (!_paramsSeenBig.get(ix)) {
_paramsSeenBig.set(ix);
if (--_paramsNeeded <= 0) {
// 29-Nov-2016, tatu: But! May still require Object Id value
}
}
}
return false;
}
public void bufferProperty(SettableBeanProperty prop, Object value) {
_buffered = new PropertyValue.Regular(_buffered, value, prop);
}
public void bufferAnyProperty(SettableAnyProperty prop, String propName, Object value) {
_buffered = new PropertyValue.Any(_buffered, value, prop, propName);
}
public void bufferMapProperty(Object key, Object value) {
_buffered = new PropertyValue.Map(_buffered, value, key);
}
}