@@ -28,6 +28,10 @@ class Codegen {
28
28
put (Long .class .getName (), "Long.valueOf(iter.readLong())" );
29
29
put (String .class .getName (), "iter.readString()" );
30
30
}};
31
+ final static Set <Class > WITH_CAPACITY_COLLECTION_CLASSES = new HashSet <Class >(){{
32
+ add (ArrayList .class );
33
+ add (HashSet .class );
34
+ }};
31
35
static volatile Map <String , Decoder > cache = new HashMap <>();
32
36
static ClassPool pool = ClassPool .getDefault ();
33
37
@@ -52,18 +56,7 @@ private synchronized static Decoder gen(String cacheKey, Type type, Type[] typeA
52
56
} else {
53
57
clazz = (Class ) type ;
54
58
}
55
- String source ;
56
- if (clazz .isArray ()) {
57
- source = genArray (clazz );
58
- } else if (List .class .isAssignableFrom (clazz )) {
59
- Class compType = (Class ) typeArgs [0 ];
60
- if (clazz == List .class ) {
61
- clazz = ArrayList .class ;
62
- }
63
- source = genList (clazz , compType );
64
- } else {
65
- source = genObject (clazz , cacheKey );
66
- }
59
+ String source = genSource (cacheKey , typeArgs , clazz );
67
60
try {
68
61
CtClass ctClass = pool .makeClass (cacheKey );
69
62
ctClass .setInterfaces (new CtClass []{pool .get (Decoder .class .getName ())});
@@ -79,6 +72,26 @@ private synchronized static Decoder gen(String cacheKey, Type type, Type[] typeA
79
72
}
80
73
}
81
74
75
+ private static String genSource (String cacheKey , Type [] typeArgs , Class clazz ) {
76
+ if (clazz .isArray ()) {
77
+ return genArray (clazz );
78
+ } else if (Collection .class .isAssignableFrom (clazz )) {
79
+ Class compType = (Class ) typeArgs [0 ];
80
+ if (clazz == List .class ) {
81
+ clazz = ArrayList .class ;
82
+ } else if (clazz == Set .class ) {
83
+ clazz = HashSet .class ;
84
+ }
85
+ if (WITH_CAPACITY_COLLECTION_CLASSES .contains (clazz )) {
86
+ return genCollectionWithCapacity (clazz , compType );
87
+ } else {
88
+ return genCollection (clazz , compType );
89
+ }
90
+ } else {
91
+ return genObject (clazz , cacheKey );
92
+ }
93
+ }
94
+
82
95
public static void addNewDecoder (String cacheKey , Decoder decoder ) {
83
96
HashMap <String , Decoder > newCache = new HashMap <>(cache );
84
97
newCache .put (cacheKey , decoder );
@@ -224,7 +237,57 @@ private static String genArray(Class clazz) {
224
237
"{{op}}" , op );
225
238
}
226
239
227
- private static String genList (Class clazz , Class compType ) {
240
+ private static String genCollectionWithCapacity (Class clazz , Class compType ) {
241
+ String nativeRead = NATIVE_READS .get (compType .getName ());
242
+ StringBuilder lines = new StringBuilder ();
243
+ append (lines , "public Object decode(java.lang.reflect.Type type, com.github.jsoniter.Jsoniter iter) {" );
244
+ append (lines , "if (!iter.readArray()) {" );
245
+ append (lines , "return new {{clazz}}(0);" );
246
+ append (lines , "}" );
247
+ append (lines , "{{comp}} a1 = ({{comp}}) {{op}};" );
248
+ append (lines , "if (!iter.readArray()) {" );
249
+ append (lines , "{{clazz}} obj = new {{clazz}}(1);" );
250
+ append (lines , "obj.add(a1);" );
251
+ append (lines , "return obj;" );
252
+ append (lines , "}" );
253
+ append (lines , "{{comp}} a2 = ({{comp}}) {{op}};" );
254
+ append (lines , "if (!iter.readArray()) {" );
255
+ append (lines , "{{clazz}} obj = new {{clazz}}(2);" );
256
+ append (lines , "obj.add(a1);" );
257
+ append (lines , "obj.add(a2);" );
258
+ append (lines , "return obj;" );
259
+ append (lines , "}" );
260
+ append (lines , "{{comp}} a3 = ({{comp}}) {{op}};" );
261
+ append (lines , "if (!iter.readArray()) {" );
262
+ append (lines , "{{clazz}} obj = new {{clazz}}(3);" );
263
+ append (lines , "obj.add(a1);" );
264
+ append (lines , "obj.add(a2);" );
265
+ append (lines , "obj.add(a3);" );
266
+ append (lines , "return obj;" );
267
+ append (lines , "}" );
268
+ append (lines , "{{comp}} a4 = ({{comp}}) {{op}};" );
269
+ append (lines , "{{clazz}} obj = new {{clazz}}(8);" );
270
+ append (lines , "obj.add(a1);" );
271
+ append (lines , "obj.add(a2);" );
272
+ append (lines , "obj.add(a3);" );
273
+ append (lines , "obj.add(a4);" );
274
+ append (lines , "int i = 4;" );
275
+ append (lines , "while (iter.readArray()) {" );
276
+ append (lines , "obj.add(({{comp}}) {{op}});" );
277
+ append (lines , "}" );
278
+ append (lines , "return obj;" );
279
+ append (lines , "}" );
280
+ String op = String .format ("iter.read(%s.class)" , compType .getCanonicalName ());
281
+ if (nativeRead != null ) {
282
+ op = nativeRead ;
283
+ }
284
+ return lines .toString ().replace (
285
+ "{{clazz}}" , clazz .getName ()).replace (
286
+ "{{comp}}" , compType .getCanonicalName ()).replace (
287
+ "{{op}}" , op );
288
+ }
289
+
290
+ private static String genCollection (Class clazz , Class compType ) {
228
291
String nativeRead = NATIVE_READS .get (compType .getName ());
229
292
StringBuilder lines = new StringBuilder ();
230
293
append (lines , "public Object decode(java.lang.reflect.Type type, com.github.jsoniter.Jsoniter iter) {" );
0 commit comments