Skip to content

Commit

Permalink
Merge branch '2.11'
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Dec 3, 2019
2 parents af2ae8e + 3090479 commit 959b6a1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fasterxml.jackson.databind.jsontype.impl;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

import com.fasterxml.jackson.annotation.JsonTypeInfo;

Expand All @@ -12,45 +13,54 @@
public class TypeNameIdResolver extends TypeIdResolverBase
{
/**
* Mappings from class name to type id, used for serialization
* Mappings from class name to type id, used for serialization.
*<p>
* Since lazily constructed will require synchronization (either internal
* by type, or external)
*/
protected final Map<String, String> _typeToId;
protected final ConcurrentHashMap<String, String> _typeToId;

/**
* Mappings from type id to JavaType, used for deserialization
* Mappings from type id to JavaType, used for deserialization.
*<p>
* Eagerly constructed, not modified, can use regular unsynchronized {@link Map}.
*/
protected final Map<String, JavaType> _idToType;

protected TypeNameIdResolver(JavaType baseType,
Map<String, String> typeToId, Map<String, JavaType> idToType)
ConcurrentHashMap<String, String> typeToId,
HashMap<String, JavaType> idToType)
{
super(baseType);
_typeToId = typeToId;
_idToType = idToType;
}

public static TypeNameIdResolver construct(MapperConfig<?> config, JavaType baseType,
Collection<NamedType> subtypes, boolean forSer, boolean forDeser)
{
// sanity check
if (forSer == forDeser) throw new IllegalArgumentException();
Map<String, String> typeToId = null;
Map<String, JavaType> idToType = null;

final ConcurrentHashMap<String, String> typeToId;
final HashMap<String, JavaType> idToType;

if (forSer) {
typeToId = new HashMap<String, String>();
}
if (forDeser) {
idToType = new HashMap<String, JavaType>();
// Only need Class-to-id for serialization; but synchronized since may be
// lazily built (if adding type-id-mappings dynamically)
typeToId = new ConcurrentHashMap<>();
idToType = null;
} else {
idToType = new HashMap<>();
// 14-Apr-2016, tatu: Apparently needed for special case of `defaultImpl`;
// see [databind#1198] for details.
typeToId = new TreeMap<String, String>();
// see [databind#1198] for details: but essentially we only need room
// for a single value.
typeToId = new ConcurrentHashMap<>(4);
}
if (subtypes != null) {
for (NamedType t : subtypes) {
/* no name? Need to figure out default; for now, let's just
* use non-qualified class name
*/
// no name? Need to figure out default; for now, let's just
// use non-qualified class name
Class<?> cls = t.getType();
String id = t.hasName() ? t.getName() : _defaultTypeId(cls);
if (forSer) {
Expand Down Expand Up @@ -98,11 +108,7 @@ protected String idFromClass(DatabindContext ctxt, Class<?> cls)
// cls = _typeFactory.constructType(cls).getRawClass();

final String key = cls.getName();
String name;

synchronized (_typeToId) {
name = _typeToId.get(key);
}
String name = _typeToId.get(key);

if (name == null) {
// 24-Feb-2011, tatu: As per [JACKSON-498], may need to dynamically look up name
Expand All @@ -115,9 +121,7 @@ protected String idFromClass(DatabindContext ctxt, Class<?> cls)
// And if still not found, let's choose default?
name = _defaultTypeId(cls);
}
synchronized (_typeToId) {
_typeToId.put(key, name);
}
_typeToId.put(key, name);
}
return name;
}
Expand Down Expand Up @@ -156,9 +160,9 @@ public String toString() {
}

/*
/*********************************************************
/**********************************************************************
/* Helper methods
/*********************************************************
/**********************************************************************
*/

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

import com.fasterxml.jackson.databind.*;

public class ExternalTypeId198Test extends BaseMapTest
// [databind#1198]
public class ExternalTypeId1198Test extends BaseMapTest
{
public enum Attacks { KICK, PUNCH }

Expand Down

0 comments on commit 959b6a1

Please sign in to comment.