|
| 1 | +package com.fasterxml.jackson.databind.deser.impl; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.Collection; |
| 6 | +import java.util.Collections; |
| 7 | +import java.util.LinkedHashMap; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +import com.fasterxml.jackson.core.JsonLocation; |
| 11 | +import com.fasterxml.jackson.databind.DeserializationConfig; |
| 12 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 13 | +import com.fasterxml.jackson.databind.deser.ValueInstantiator; |
| 14 | +import com.fasterxml.jackson.databind.deser.std.JsonLocationInstantiator; |
| 15 | + |
| 16 | +/** |
| 17 | + * Container for a set of {@link ValueInstantiator}s used for certain critical |
| 18 | + * JDK value types, either as performance optimization for initialization time observed |
| 19 | + * by profiling, or due to difficulty in otherwise finding constructors. |
| 20 | + * |
| 21 | + * @since 2.10 |
| 22 | + */ |
| 23 | +public abstract class JDKValueInstantiators |
| 24 | +{ |
| 25 | + public static ValueInstantiator findStdValueInstantiator(DeserializationConfig config, |
| 26 | + Class<?> raw) |
| 27 | + { |
| 28 | + if (raw == JsonLocation.class) { |
| 29 | + return new JsonLocationInstantiator(); |
| 30 | + } |
| 31 | + // [databind#1868]: empty List/Set/Map |
| 32 | + if (Collection.class.isAssignableFrom(raw)) { |
| 33 | + if (raw == ArrayList.class) { |
| 34 | + return ArrayListInstantiator.INSTANCE; |
| 35 | + } |
| 36 | + // [databind#XXX]: optimize commonly needed default creators |
| 37 | + if (Collections.EMPTY_SET.getClass() == raw) { |
| 38 | + return new ConstantValueInstantiator(Collections.EMPTY_SET); |
| 39 | + } |
| 40 | + if (Collections.EMPTY_LIST.getClass() == raw) { |
| 41 | + return new ConstantValueInstantiator(Collections.EMPTY_LIST); |
| 42 | + } |
| 43 | + } else if (Map.class.isAssignableFrom(raw)) { |
| 44 | + if (raw == LinkedHashMap.class) { |
| 45 | + return LinkedHashMapInstantiator.INSTANCE; |
| 46 | + } |
| 47 | + if (Collections.EMPTY_MAP.getClass() == raw) { |
| 48 | + return new ConstantValueInstantiator(Collections.EMPTY_MAP); |
| 49 | + } |
| 50 | + } |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + private static class ArrayListInstantiator |
| 55 | + extends ValueInstantiator.Base |
| 56 | + implements java.io.Serializable |
| 57 | + { |
| 58 | + private static final long serialVersionUID = 2L; |
| 59 | + |
| 60 | + public final static ArrayListInstantiator INSTANCE = new ArrayListInstantiator(); |
| 61 | + public ArrayListInstantiator() { |
| 62 | + super(ArrayList.class); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public boolean canInstantiate() { return true; } |
| 67 | + |
| 68 | + @Override |
| 69 | + public boolean canCreateUsingDefault() { return true; } |
| 70 | + |
| 71 | + @Override |
| 72 | + public Object createUsingDefault(DeserializationContext ctxt) throws IOException { |
| 73 | + return new ArrayList<>(); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private static class LinkedHashMapInstantiator |
| 78 | + extends ValueInstantiator.Base |
| 79 | + implements java.io.Serializable |
| 80 | + { |
| 81 | + private static final long serialVersionUID = 2L; |
| 82 | + |
| 83 | + public final static LinkedHashMapInstantiator INSTANCE = new LinkedHashMapInstantiator(); |
| 84 | + |
| 85 | + public LinkedHashMapInstantiator() { |
| 86 | + super(LinkedHashMap.class); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public boolean canInstantiate() { return true; } |
| 91 | + |
| 92 | + @Override |
| 93 | + public boolean canCreateUsingDefault() { return true; } |
| 94 | + |
| 95 | + @Override |
| 96 | + public Object createUsingDefault(DeserializationContext ctxt) throws IOException { |
| 97 | + return new LinkedHashMap<>(); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private static class ConstantValueInstantiator |
| 102 | + extends ValueInstantiator.Base |
| 103 | + implements java.io.Serializable |
| 104 | + { |
| 105 | + private static final long serialVersionUID = 2L; |
| 106 | + |
| 107 | + protected final Object _value; |
| 108 | + |
| 109 | + public ConstantValueInstantiator(Object value) { |
| 110 | + super(value.getClass()); |
| 111 | + _value = value; |
| 112 | + } |
| 113 | + |
| 114 | + @Override // yes, since default ctor works |
| 115 | + public boolean canInstantiate() { return true; } |
| 116 | + |
| 117 | + @Override |
| 118 | + public boolean canCreateUsingDefault() { return true; } |
| 119 | + |
| 120 | + @Override |
| 121 | + public Object createUsingDefault(DeserializationContext ctxt) throws IOException { |
| 122 | + return _value; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments