@@ -18,6 +18,7 @@ class Codegen {
18
18
put ("byte" , "iter.readShort()" );
19
19
put ("short" , "iter.readShort()" );
20
20
put ("int" , "iter.readInt()" );
21
+ put ("char" , "iter.readInt()" );
21
22
put ("long" , "iter.readLong()" );
22
23
put (Float .class .getName (), "Float.valueOf(iter.readFloat())" );
23
24
put (Double .class .getName (), "Double.valueOf(iter.readDouble())" );
@@ -161,10 +162,12 @@ public static void addNewDecoder(String cacheKey, Decoder decoder) {
161
162
private static String genObject (Class clazz , String cacheKey ) {
162
163
Map <Integer , Object > map = new HashMap <Integer , Object >();
163
164
for (Field field : clazz .getFields ()) {
164
- Decoder decoder = createFieldDecoder (cacheKey , field );
165
165
String [] alternativeFieldNames = null ;
166
- if (decoder instanceof FieldDecoder ) {
167
- alternativeFieldNames = ((FieldDecoder ) decoder ).getAlternativeFieldNames ();
166
+ for (FieldDecoderFactory fieldDecoderFactory : fieldDecoderFactories ) {
167
+ alternativeFieldNames = fieldDecoderFactory .getAlternativeFieldNames (field );
168
+ if (alternativeFieldNames != null ) {
169
+ break ;
170
+ }
168
171
}
169
172
if (alternativeFieldNames == null ) {
170
173
alternativeFieldNames = new String []{field .getName ()};
@@ -217,8 +220,7 @@ private static String genObject(Class clazz, String cacheKey) {
217
220
return lines .toString ().replace ("{{clazz}}" , clazz .getName ());
218
221
}
219
222
220
- private static Decoder createFieldDecoder (String cacheKey , Field field ) {
221
- String fieldCacheKey = field .getName () + "@" + cacheKey ;
223
+ private static Decoder createFieldDecoder (String fieldCacheKey , Field field ) {
222
224
for (FieldDecoderFactory fieldDecoderFactory : fieldDecoderFactories ) {
223
225
Decoder decoder = fieldDecoderFactory .createDecoder (field );
224
226
if (decoder != null ) {
@@ -246,14 +248,67 @@ private static void addFieldDispatch(StringBuilder lines, int len, int i, Map<By
246
248
}
247
249
248
250
private static void genField (StringBuilder lines , Field field , String cacheKey ) {
249
- String fieldTypeName = field .getType ().getCanonicalName ();
251
+ Class <?> fieldType = field .getType ();
252
+ String fieldTypeName = fieldType .getCanonicalName ();
250
253
String fieldCacheKey = field .getName () + "@" + cacheKey ;
251
- Decoder decoder = cache .get (fieldCacheKey );
252
- boolean useCustomizedDecoder = decoder != null ;
253
- if (useCustomizedDecoder && decoder instanceof FieldDecoder ) {
254
- useCustomizedDecoder = ((FieldDecoder ) decoder ).useDefaultDecoder ();
255
- }
256
- if (useCustomizedDecoder ) {
254
+ Decoder decoder = createFieldDecoder (fieldCacheKey , field );
255
+ if (decoder != null ) {
256
+ if (fieldType == boolean .class ) {
257
+ if (!(decoder instanceof Decoder .BooleanDecoder )) {
258
+ throw new RuntimeException ("decoder for field " + field + "must implement Decoder.BooleanDecoder" );
259
+ }
260
+ append (lines , String .format ("obj.%s = iter.readBoolean(\" %s\" );" , field .getName (), fieldCacheKey ));
261
+ return ;
262
+ }
263
+ if (fieldType == byte .class ) {
264
+ if (!(decoder instanceof Decoder .ShortDecoder )) {
265
+ throw new RuntimeException ("decoder for field " + field + "must implement Decoder.ShortDecoder" );
266
+ }
267
+ append (lines , String .format ("obj.%s = iter.readShort(\" %s\" );" , field .getName (), fieldCacheKey ));
268
+ return ;
269
+ }
270
+ if (fieldType == short .class ) {
271
+ if (!(decoder instanceof Decoder .ShortDecoder )) {
272
+ throw new RuntimeException ("decoder for field " + field + "must implement Decoder.ShortDecoder" );
273
+ }
274
+ append (lines , String .format ("obj.%s = iter.readShort(\" %s\" );" , field .getName (), fieldCacheKey ));
275
+ return ;
276
+ }
277
+ if (fieldType == char .class ) {
278
+ if (!(decoder instanceof Decoder .IntDecoder )) {
279
+ throw new RuntimeException ("decoder for field " + field + "must implement Decoder.IntDecoder" );
280
+ }
281
+ append (lines , String .format ("obj.%s = iter.readInt(\" %s\" );" , field .getName (), fieldCacheKey ));
282
+ return ;
283
+ }
284
+ if (fieldType == int .class ) {
285
+ if (!(decoder instanceof Decoder .IntDecoder )) {
286
+ throw new RuntimeException ("decoder for field " + field + "must implement Decoder.IntDecoder" );
287
+ }
288
+ append (lines , String .format ("obj.%s = iter.readInt(\" %s\" );" , field .getName (), fieldCacheKey ));
289
+ return ;
290
+ }
291
+ if (fieldType == long .class ) {
292
+ if (!(decoder instanceof Decoder .LongDecoder )) {
293
+ throw new RuntimeException ("decoder for field " + field + "must implement Decoder.LongDecoder" );
294
+ }
295
+ append (lines , String .format ("obj.%s = iter.readLong(\" %s\" );" , field .getName (), fieldCacheKey ));
296
+ return ;
297
+ }
298
+ if (fieldType == float .class ) {
299
+ if (!(decoder instanceof Decoder .FloatDecoder )) {
300
+ throw new RuntimeException ("decoder for field " + field + "must implement Decoder.FloatDecoder" );
301
+ }
302
+ append (lines , String .format ("obj.%s = iter.readFloat(\" %s\" );" , field .getName (), fieldCacheKey ));
303
+ return ;
304
+ }
305
+ if (fieldType == double .class ) {
306
+ if (!(decoder instanceof Decoder .DoubleDecoder )) {
307
+ throw new RuntimeException ("decoder for field " + field + "must implement Decoder.DoubleDecoder" );
308
+ }
309
+ append (lines , String .format ("obj.%s = iter.readDouble(\" %s\" );" , field .getName (), fieldCacheKey ));
310
+ return ;
311
+ }
257
312
append (lines , String .format ("obj.%s = (%s)iter.read(\" %s\" , %s.class);" ,
258
313
field .getName (), fieldTypeName , fieldCacheKey , fieldTypeName ));
259
314
return ;
@@ -433,4 +488,28 @@ private static void append(StringBuilder lines, String str) {
433
488
public static void addFieldDecoderFactory (FieldDecoderFactory fieldDecoderFactory ) {
434
489
fieldDecoderFactories .add (fieldDecoderFactory );
435
490
}
491
+
492
+ public static Decoder .IntDecoder getIntDecoder (String cacheKey ) {
493
+ return (Decoder .IntDecoder ) cache .get (cacheKey );
494
+ }
495
+
496
+ public static Decoder .BooleanDecoder getBooleanDecoder (String cacheKey ) {
497
+ return (Decoder .BooleanDecoder ) cache .get (cacheKey );
498
+ }
499
+
500
+ public static Decoder .ShortDecoder getShortDecoder (String cacheKey ) {
501
+ return (Decoder .ShortDecoder ) cache .get (cacheKey );
502
+ }
503
+
504
+ public static Decoder .LongDecoder getLongDecoder (String cacheKey ) {
505
+ return (Decoder .LongDecoder ) cache .get (cacheKey );
506
+ }
507
+
508
+ public static Decoder .FloatDecoder getFloatDecoder (String cacheKey ) {
509
+ return (Decoder .FloatDecoder ) cache .get (cacheKey );
510
+ }
511
+
512
+ public static Decoder .DoubleDecoder getDoubleDecoder (String cacheKey ) {
513
+ return (Decoder .DoubleDecoder ) cache .get (cacheKey );
514
+ }
436
515
}
0 commit comments