Skip to content

Commit 3090479

Browse files
committed
Merge branch '2.10' into 2.11
2 parents f3da1aa + 3c7729f commit 3090479

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

src/main/java/com/fasterxml/jackson/databind/jsontype/impl/TypeNameIdResolver.java

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.fasterxml.jackson.databind.jsontype.impl;
22

33
import java.util.*;
4+
import java.util.concurrent.ConcurrentHashMap;
45

56
import com.fasterxml.jackson.annotation.JsonTypeInfo;
67
import com.fasterxml.jackson.databind.BeanDescription;
@@ -14,46 +15,55 @@ public class TypeNameIdResolver extends TypeIdResolverBase
1415
protected final MapperConfig<?> _config;
1516

1617
/**
17-
* Mappings from class name to type id, used for serialization
18+
* Mappings from class name to type id, used for serialization.
19+
*<p>
20+
* Since lazily constructed will require synchronization (either internal
21+
* by type, or external)
1822
*/
19-
protected final Map<String, String> _typeToId;
23+
protected final ConcurrentHashMap<String, String> _typeToId;
2024

2125
/**
22-
* Mappings from type id to JavaType, used for deserialization
26+
* Mappings from type id to JavaType, used for deserialization.
27+
*<p>
28+
* Eagerly constructed, not modified, can use regular unsynchronized {@link Map}.
2329
*/
2430
protected final Map<String, JavaType> _idToType;
2531

2632
protected TypeNameIdResolver(MapperConfig<?> config, JavaType baseType,
27-
Map<String, String> typeToId, Map<String, JavaType> idToType)
33+
ConcurrentHashMap<String, String> typeToId,
34+
HashMap<String, JavaType> idToType)
2835
{
2936
super(baseType, config.getTypeFactory());
3037
_config = config;
3138
_typeToId = typeToId;
3239
_idToType = idToType;
3340
}
34-
41+
3542
public static TypeNameIdResolver construct(MapperConfig<?> config, JavaType baseType,
3643
Collection<NamedType> subtypes, boolean forSer, boolean forDeser)
3744
{
3845
// sanity check
3946
if (forSer == forDeser) throw new IllegalArgumentException();
40-
Map<String, String> typeToId = null;
41-
Map<String, JavaType> idToType = null;
47+
48+
final ConcurrentHashMap<String, String> typeToId;
49+
final HashMap<String, JavaType> idToType;
4250

4351
if (forSer) {
44-
typeToId = new HashMap<String, String>();
45-
}
46-
if (forDeser) {
47-
idToType = new HashMap<String, JavaType>();
52+
// Only need Class-to-id for serialization; but synchronized since may be
53+
// lazily built (if adding type-id-mappings dynamically)
54+
typeToId = new ConcurrentHashMap<>();
55+
idToType = null;
56+
} else {
57+
idToType = new HashMap<>();
4858
// 14-Apr-2016, tatu: Apparently needed for special case of `defaultImpl`;
49-
// see [databind#1198] for details.
50-
typeToId = new TreeMap<String, String>();
59+
// see [databind#1198] for details: but essentially we only need room
60+
// for a single value.
61+
typeToId = new ConcurrentHashMap<>(4);
5162
}
5263
if (subtypes != null) {
5364
for (NamedType t : subtypes) {
54-
/* no name? Need to figure out default; for now, let's just
55-
* use non-qualified class name
56-
*/
65+
// no name? Need to figure out default; for now, let's just
66+
// use non-qualified class name
5767
Class<?> cls = t.getType();
5868
String id = t.hasName() ? t.getName() : _defaultTypeId(cls);
5969
if (forSer) {
@@ -91,10 +101,7 @@ protected String idFromClass(Class<?> clazz)
91101
// NOTE: although we may need to let `TypeModifier` change actual type to use
92102
// for id, we can use original type as key for more efficient lookup:
93103
final String key = clazz.getName();
94-
String name;
95-
synchronized (_typeToId) {
96-
name = _typeToId.get(key);
97-
}
104+
String name = _typeToId.get(key);
98105

99106
if (name == null) {
100107
// 29-Nov-2019, tatu: As per test in `TestTypeModifierNameResolution` somehow
@@ -110,9 +117,7 @@ protected String idFromClass(Class<?> clazz)
110117
// And if still not found, let's choose default?
111118
name = _defaultTypeId(cls);
112119
}
113-
synchronized (_typeToId) {
114-
_typeToId.put(key, name);
115-
}
120+
_typeToId.put(key, name);
116121
}
117122
return name;
118123
}

src/test/java/com/fasterxml/jackson/databind/jsontype/ext/ExternalTypeId198Test.java renamed to src/test/java/com/fasterxml/jackson/databind/jsontype/ext/ExternalTypeId1198Test.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
import com.fasterxml.jackson.databind.*;
88

9-
public class ExternalTypeId198Test extends BaseMapTest
9+
// [databind#1198]
10+
public class ExternalTypeId1198Test extends BaseMapTest
1011
{
1112
public enum Attacks { KICK, PUNCH }
1213

0 commit comments

Comments
 (0)