1
1
package com .fasterxml .jackson .databind .deser ;
2
2
3
3
import java .util .HashMap ;
4
+ import java .util .concurrent .locks .ReentrantLock ;
4
5
5
6
import com .fasterxml .jackson .annotation .JsonFormat ;
6
7
import com .fasterxml .jackson .databind .*;
@@ -52,6 +53,11 @@ public final class DeserializerCache
52
53
protected final HashMap <JavaType , JsonDeserializer <Object >> _incompleteDeserializers
53
54
= new HashMap <JavaType , JsonDeserializer <Object >>(8 );
54
55
56
+ /**
57
+ * We hold an explicit lock while creating deserializers to avoid creating duplicates.
58
+ */
59
+ private final ReentrantLock _incompleteDeserializersLock = new ReentrantLock ();
60
+
55
61
/*
56
62
/**********************************************************
57
63
/* Life-cycle
@@ -246,12 +252,26 @@ protected JsonDeserializer<Object> _createAndCacheValueDeserializer(Deserializat
246
252
* limitations necessary to ensure that only completely initialized ones
247
253
* are visible and used.
248
254
*/
249
- synchronized (_incompleteDeserializers ) {
250
- // Ok, then: could it be that due to a race condition, deserializer can now be found?
251
- JsonDeserializer <Object > deser = _findCachedDeserializer (type );
252
- if (deser != null ) {
253
- return deser ;
255
+ if (type == null ) {
256
+ throw new IllegalArgumentException ("Null JavaType passed" );
257
+ }
258
+ if (_hasCustomHandlers (type )) {
259
+ return null ;
260
+ }
261
+ final boolean isCustom = _hasCustomHandlers (type );
262
+ JsonDeserializer <Object > deser = isCustom ? null : _cachedDeserializers .get (type );
263
+ if (deser != null ) {
264
+ return deser ;
265
+ }
266
+ _incompleteDeserializersLock .lock ();
267
+ try {
268
+ if (!isCustom ) {
269
+ deser = _cachedDeserializers .get (type );
270
+ if (deser != null ) {
271
+ return deser ;
272
+ }
254
273
}
274
+ // Ok, then: could it be that due to a race condition, deserializer can now be found?
255
275
int count = _incompleteDeserializers .size ();
256
276
// Or perhaps being resolved right now?
257
277
if (count > 0 ) {
@@ -269,6 +289,8 @@ protected JsonDeserializer<Object> _createAndCacheValueDeserializer(Deserializat
269
289
_incompleteDeserializers .clear ();
270
290
}
271
291
}
292
+ } finally {
293
+ _incompleteDeserializersLock .unlock ();
272
294
}
273
295
}
274
296
0 commit comments