forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTypeLiteral.java
More file actions
219 lines (197 loc) · 7.97 KB
/
TypeLiteral.java
File metadata and controls
219 lines (197 loc) · 7.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package com.jsoniter.spi;
import com.jsoniter.any.Any;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.WildcardType;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
public class TypeLiteral<T> {
public enum NativeType {
FLOAT,
DOUBLE,
BOOLEAN,
BYTE,
SHORT,
INT,
CHAR,
LONG,
BIG_DECIMAL,
BIG_INTEGER,
STRING,
OBJECT,
ANY,
}
public static Map<Type, NativeType> nativeTypes = new HashMap<Type, NativeType>() {{
put(float.class, NativeType.FLOAT);
put(Float.class, NativeType.FLOAT);
put(double.class, NativeType.DOUBLE);
put(Double.class, NativeType.DOUBLE);
put(boolean.class, NativeType.BOOLEAN);
put(Boolean.class, NativeType.BOOLEAN);
put(byte.class, NativeType.BYTE);
put(Byte.class, NativeType.BYTE);
put(short.class, NativeType.SHORT);
put(Short.class, NativeType.SHORT);
put(int.class, NativeType.INT);
put(Integer.class, NativeType.INT);
put(char.class, NativeType.CHAR);
put(Character.class, NativeType.CHAR);
put(long.class, NativeType.LONG);
put(Long.class, NativeType.LONG);
put(BigDecimal.class, NativeType.BIG_DECIMAL);
put(BigInteger.class, NativeType.BIG_INTEGER);
put(String.class, NativeType.STRING);
put(Object.class, NativeType.OBJECT);
put(Any.class, NativeType.ANY);
}};
private volatile static Map<Type, TypeLiteral> typeLiteralCache = new HashMap<Type, TypeLiteral>();
final Type type;
final String decoderCacheKey;
final String encoderCacheKey;
// TODO: remove native type
final NativeType nativeType;
/**
* Constructs a new type literal. Derives represented class from type parameter.
* Clients create an empty anonymous subclass. Doing so embeds the type parameter in the
* anonymous class's type hierarchy so we can reconstitute it at runtime despite erasure.
*/
@SuppressWarnings("unchecked")
protected TypeLiteral() {
this.type = getSuperclassTypeParameter(getClass());
nativeType = nativeTypes.get(this.type);
decoderCacheKey = generateDecoderCacheKey(type);
encoderCacheKey = generateEncoderCacheKey(type);
}
public TypeLiteral(Type type, String decoderCacheKey, String encoderCacheKey) {
this.type = type;
nativeType = nativeTypes.get(this.type);
this.decoderCacheKey = decoderCacheKey;
this.encoderCacheKey = encoderCacheKey;
}
private static String generateDecoderCacheKey(Type type) {
return generateCacheKey(type, "decoder.");
}
private static String generateEncoderCacheKey(Type type) {
return generateCacheKey(type, "encoder.");
}
private static String generateCacheKey(Type type, String prefix) {
StringBuilder decoderClassName = new StringBuilder(prefix);
if (type instanceof Class) {
Class clazz = (Class) type;
if (clazz.isAnonymousClass()) {
throw new JsonException("anonymous class not supported: " + clazz);
}
if (clazz.isArray()) {
decoderClassName.append(clazz.getCanonicalName().replace("[]", "_array"));
} else {
// for nested class $
decoderClassName.append(clazz.getName().replace("[]", "_array"));
}
} else if (type instanceof ParameterizedType) {
try {
ParameterizedType pType = (ParameterizedType) type;
Class clazz = (Class) pType.getRawType();
decoderClassName.append(clazz.getCanonicalName().replace("[]", "_array"));
for (int i = 0; i < pType.getActualTypeArguments().length; i++) {
String typeName = formatTypeWithoutSpecialCharacter(pType.getActualTypeArguments()[i]);
decoderClassName.append('_');
decoderClassName.append(typeName);
}
} catch (JsonException e) {
throw e;
} catch (Exception e) {
throw new JsonException("failed to generate cache key for: " + type, e);
}
} else if (type instanceof GenericArrayType) {
GenericArrayType gaType = (GenericArrayType) type;
Type compType = gaType.getGenericComponentType();
decoderClassName.append(formatTypeWithoutSpecialCharacter(compType));
decoderClassName.append("_array");
} else if (type instanceof WildcardType) {
decoderClassName.append(Object.class.getName());
} else {
throw new UnsupportedOperationException("do not know how to handle: " + type);
}
return decoderClassName.toString().replace("$", "_");
}
private static String formatTypeWithoutSpecialCharacter(Type type) {
if (type instanceof Class) {
Class clazz = (Class) type;
return clazz.getCanonicalName();
}
if (type instanceof ParameterizedType) {
ParameterizedType pType = (ParameterizedType) type;
String typeName = formatTypeWithoutSpecialCharacter(pType.getRawType());
for (Type typeArg : pType.getActualTypeArguments()) {
typeName += "_";
typeName += formatTypeWithoutSpecialCharacter(typeArg);
}
return typeName;
}
if (type instanceof GenericArrayType) {
GenericArrayType gaType = (GenericArrayType) type;
return formatTypeWithoutSpecialCharacter(gaType.getGenericComponentType()) + "_array";
}
if (type instanceof WildcardType) {
return Object.class.getCanonicalName();
}
throw new JsonException("unsupported type: " + type + ", of class " + type.getClass());
}
static Type getSuperclassTypeParameter(Class<?> subclass) {
Type superclass = subclass.getGenericSuperclass();
if (superclass instanceof Class) {
throw new JsonException("Missing type parameter.");
}
ParameterizedType parameterized = (ParameterizedType) superclass;
return parameterized.getActualTypeArguments()[0];
}
public static TypeLiteral create(Type valueType) {
TypeLiteral typeLiteral = typeLiteralCache.get(valueType);
if (typeLiteral != null) {
return typeLiteral;
}
return createNew(valueType);
}
private synchronized static TypeLiteral createNew(Type valueType) {
TypeLiteral typeLiteral = typeLiteralCache.get(valueType);
if (typeLiteral != null) {
return typeLiteral;
}
HashMap<Type, TypeLiteral> copy = new HashMap<Type, TypeLiteral>(typeLiteralCache);
typeLiteral = new TypeLiteral(valueType,
generateDecoderCacheKey(valueType),
generateEncoderCacheKey(valueType));
copy.put(valueType, typeLiteral);
typeLiteralCache = copy;
return typeLiteral;
}
public Type getType() {
return type;
}
public String getDecoderCacheKey() {
return getDecoderCacheKey(JsoniterSpi.getCurrentConfig().configName());
}
public String getDecoderCacheKey(String configName) {
return configName + decoderCacheKey;
}
public String getEncoderCacheKey() {
return getEncoderCacheKey(JsoniterSpi.getCurrentConfig().configName());
}
public String getEncoderCacheKey(String configName) {
return configName + encoderCacheKey;
}
public NativeType getNativeType() {
return nativeType;
}
@Override
public String toString() {
return "TypeLiteral{" +
"type=" + type +
", decoderCacheKey='" + decoderCacheKey + '\'' +
", encoderCacheKey='" + encoderCacheKey + '\'' +
'}';
}
}