Skip to content

Rework synchronized block from BeanDeserializerBase #4458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

import com.fasterxml.jackson.annotation.*;

Expand Down Expand Up @@ -177,8 +178,9 @@ public abstract class BeanDeserializerBase
* Note that this is <b>only needed</b> for polymorphic types,
* that is, when the actual type is not statically known.
* For other types this remains null.
* The map type changed in 2.18 (from HashMap to ConcurrentHashMap)
*/
protected transient HashMap<ClassKey, JsonDeserializer<Object>> _subDeserializers;
protected transient ConcurrentHashMap<ClassKey, JsonDeserializer<Object>> _subDeserializers;

/**
* If one of properties has "unwrapped" value, we need separate
Expand Down Expand Up @@ -1882,17 +1884,14 @@ protected JsonDeserializer<Object> _findSubclassDeserializer(DeserializationCont
Object bean, TokenBuffer unknownTokens)
throws IOException
{
JsonDeserializer<Object> subDeser;

// First: maybe we have already created sub-type deserializer?
synchronized (this) {
subDeser = (_subDeserializers == null) ? null : _subDeserializers.get(new ClassKey(bean.getClass()));
}
final ClassKey classKey = new ClassKey(bean.getClass());
JsonDeserializer<Object> subDeser = (_subDeserializers == null) ? null : _subDeserializers.get(classKey);
if (subDeser != null) {
return subDeser;
}
// If not, maybe we can locate one. First, need provider
JavaType type = ctxt.constructType(bean.getClass());
final JavaType type = ctxt.constructType(bean.getClass());
/* 30-Jan-2012, tatu: Ideally we would be passing referring
* property; which in theory we could keep track of via
* ResolvableDeserializer (if we absolutely must...).
Expand All @@ -1902,12 +1901,14 @@ protected JsonDeserializer<Object> _findSubclassDeserializer(DeserializationCont
subDeser = ctxt.findRootValueDeserializer(type);
// Also, need to cache it
if (subDeser != null) {
synchronized (this) {
if (_subDeserializers == null) {
_subDeserializers = new HashMap<ClassKey,JsonDeserializer<Object>>();;
if (_subDeserializers == null) {
synchronized (this) {
if (_subDeserializers == null) {
_subDeserializers = new ConcurrentHashMap<>();
}
}
_subDeserializers.put(new ClassKey(bean.getClass()), subDeser);
}
_subDeserializers.put(classKey, subDeser);
}
return subDeser;
}
Expand Down