Skip to content

Commit

Permalink
Remove pre-creation/lazy-creation for non-default Collection/Map types
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 16, 2024
1 parent 6e918bf commit 2982e56
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 111 deletions.
4 changes: 4 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -1738,3 +1738,7 @@ Muhammad Khalikov (mukham12@github)
* Contributed fix for #4209: Make `BeanDeserializerModifier`/`BeanSerializerModifier`
implement `java.io.Serializable`
(2.17.0)

Eduard Dudar (edudar@github)
* Contributed #4299: Some `Collection` and `Map` fallbacks don't work in GraalVM native image
(2.17.0)
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Project: jackson-databind
#4262: Improve handling of `null` insertion failure for `TreeSet`
#4263: Change `ObjectArrayDeserializer` to use "generic" type parameter
(`java.lang.Object`) to remove co-variant return type
#4299: Some `Collection` and `Map` fallbacks don't work in GraalVM native image
(contributed by Eduard D)
#4309: `@JsonSetter(nulls=...)` handling of `Collection` `null` values during
deserialization with `READ_UNKNOWN_ENUM_VALUES_AS_NULL` and `FAIL_ON_INVALID_SUBTYPE` wrong
(reported by @ivan-zaitsev)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,28 @@ public static ValueInstantiator findStdValueInstantiator(DeserializationConfig c
Class<?> raw)
{
if (raw == JsonLocation.class) {
return JsonLocationInstantiator.instance();
return new JsonLocationInstantiator();
}
// [databind#1868]: empty List/Set/Map
// [databind#2416]: optimize commonly needed default creators
if (Collection.class.isAssignableFrom(raw)) {
if (raw == ArrayList.class) {
if (raw == ArrayList.class) { // default impl, pre-constructed instance
return ArrayListInstantiator.INSTANCE;
}
if (raw == LinkedList.class) {
return LinkedListInstantiator.instance();
return new LinkedListInstantiator();
}
if (raw == HashSet.class) {
if (raw == HashSet.class) { // default impl, pre-constructed instance
return HashSetInstantiator.INSTANCE;
}
if (raw == TreeSet.class) {
return TreeSetInstantiator.instance();
return new TreeSetInstantiator();
}
if (raw == Collections.emptySet().getClass()) {
return EmptySetInstantiator.instance();
return new ConstantValueInstantiator(Collections.emptySet());
}
if (raw == Collections.emptyList().getClass()) {
return EmptyListInstantiator.instance();
return new ConstantValueInstantiator(Collections.emptyList());
}
} else if (Map.class.isAssignableFrom(raw)) {
if (raw == LinkedHashMap.class) {
Expand All @@ -55,13 +55,13 @@ public static ValueInstantiator findStdValueInstantiator(DeserializationConfig c
return HashMapInstantiator.INSTANCE;
}
if (raw == ConcurrentHashMap.class) {
return ConcurrentHashMapInstantiator.instance();
return new ConcurrentHashMapInstantiator();
}
if (raw == TreeMap.class) {
return TreeMapInstantiator.instance();
return new TreeMapInstantiator();
}
if (raw == Collections.emptyMap().getClass()) {
return EmptyMapInstantiator.instance();
return new ConstantValueInstantiator(Collections.emptyMap());
}
}
return null;
Expand Down Expand Up @@ -93,7 +93,7 @@ private static class ArrayListInstantiator
{
private static final long serialVersionUID = 2L;

private static final ArrayListInstantiator INSTANCE = new ArrayListInstantiator();
static final ArrayListInstantiator INSTANCE = new ArrayListInstantiator();

public ArrayListInstantiator() {
super(ArrayList.class);
Expand All @@ -111,15 +111,6 @@ private static class LinkedListInstantiator
{
private static final long serialVersionUID = 2L;

private static LinkedListInstantiator _instance;

private static LinkedListInstantiator instance() {
if (_instance == null) {
_instance = new LinkedListInstantiator();
}
return _instance;
}

public LinkedListInstantiator() {
super(LinkedList.class);
}
Expand All @@ -136,7 +127,7 @@ private static class HashSetInstantiator
{
private static final long serialVersionUID = 2L;

private static final HashSetInstantiator INSTANCE = new HashSetInstantiator();
static final HashSetInstantiator INSTANCE = new HashSetInstantiator();

public HashSetInstantiator() {
super(HashSet.class);
Expand All @@ -154,15 +145,6 @@ private static class TreeSetInstantiator
{
private static final long serialVersionUID = 2L;

private static TreeSetInstantiator _instance;

private static TreeSetInstantiator instance() {
if (_instance == null) {
_instance = new TreeSetInstantiator();
}
return _instance;
}

public TreeSetInstantiator() {
super(TreeSet.class);
}
Expand All @@ -179,15 +161,6 @@ private static class ConcurrentHashMapInstantiator
{
private static final long serialVersionUID = 2L;

private static ConcurrentHashMapInstantiator _instance;

private static ConcurrentHashMapInstantiator instance() {
if (_instance == null) {
_instance = new ConcurrentHashMapInstantiator();
}
return _instance;
}

public ConcurrentHashMapInstantiator() {
super(ConcurrentHashMap.class);
}
Expand All @@ -203,7 +176,7 @@ private static class HashMapInstantiator
{
private static final long serialVersionUID = 2L;

private static final HashMapInstantiator INSTANCE = new HashMapInstantiator();
static final HashMapInstantiator INSTANCE = new HashMapInstantiator();

public HashMapInstantiator() {
super(HashMap.class);
Expand All @@ -220,7 +193,7 @@ private static class LinkedHashMapInstantiator
{
private static final long serialVersionUID = 2L;

private static final LinkedHashMapInstantiator INSTANCE = new LinkedHashMapInstantiator();
static final LinkedHashMapInstantiator INSTANCE = new LinkedHashMapInstantiator();

public LinkedHashMapInstantiator() {
super(LinkedHashMap.class);
Expand All @@ -238,15 +211,6 @@ private static class TreeMapInstantiator
{
private static final long serialVersionUID = 2L;

private static TreeMapInstantiator _instance;

private static TreeMapInstantiator instance() {
if (_instance == null) {
_instance = new TreeMapInstantiator();
}
return _instance;
}

public TreeMapInstantiator() {
super(TreeMap.class);
}
Expand All @@ -270,68 +234,8 @@ public ConstantValueInstantiator(Object value) {
}

@Override
public Object createUsingDefault(DeserializationContext ctxt) throws IOException {
public final Object createUsingDefault(DeserializationContext ctxt) throws IOException {
return _value;
}
}

// @since 2.17 [databind#4299] Instantiators for additional container classes
private static class EmptySetInstantiator
extends ConstantValueInstantiator
{
private static final long serialVersionUID = 2L;

private static EmptySetInstantiator _instance;

private static EmptySetInstantiator instance() {
if (_instance == null) {
_instance = new EmptySetInstantiator();
}
return _instance;
}

public EmptySetInstantiator() {
super(Collections.emptySet());
}
}

// @since 2.17 [databind#4299] Instantiators for additional container classes
private static class EmptyListInstantiator
extends ConstantValueInstantiator
{
private static final long serialVersionUID = 2L;

private static EmptyListInstantiator _instance;

private static EmptyListInstantiator instance() {
if (_instance == null) {
_instance = new EmptyListInstantiator();
}
return _instance;
}

public EmptyListInstantiator() {
super(Collections.emptyList());
}
}

// @since 2.17 [databind#4299] Instantiators for additional container classes
private static class EmptyMapInstantiator
extends ConstantValueInstantiator
{
private static final long serialVersionUID = 2L;

private static EmptyMapInstantiator _instance;

private static EmptyMapInstantiator instance() {
if (_instance == null) {
_instance = new EmptyMapInstantiator();
}
return _instance;
}

public EmptyMapInstantiator() {
super(Collections.emptyMap());
}
}
}

0 comments on commit 2982e56

Please sign in to comment.