3
3
import java .lang .reflect .Array ;
4
4
import java .lang .reflect .Field ;
5
5
import java .lang .reflect .InvocationTargetException ;
6
- import java .lang .reflect .ParameterizedType ;
7
- import java .lang .reflect .Type ;
8
6
import java .math .BigDecimal ;
9
7
import java .math .BigInteger ;
10
8
import java .util .ArrayList ;
@@ -215,26 +213,12 @@ private void deserializeField(final VPackSlice vpack, final Object entity, final
215
213
final VPackSlice attr = new VPackSlice (vpack .getVpack (), vpack .getStart () + vpack .getByteSize ());
216
214
if (!attr .isNone ()) {
217
215
final Field field = fieldInfo .getField ();
218
- final Object value = getValue (attr , field , field .getType ());
216
+ final Object value = getValue (attr , field .getType (), fieldInfo );
219
217
field .set (entity , value );
220
218
}
221
219
}
222
220
223
- private Class <?> getComponentType (final Field field , final Class <?> type , final int i ) {
224
- Class <?> result ;
225
- final Class <?> componentType = type .getComponentType ();
226
- if (componentType != null ) {
227
- result = componentType ;
228
- } else {
229
- final ParameterizedType genericType = (ParameterizedType ) field .getGenericType ();
230
- final Type argType = genericType .getActualTypeArguments ()[i ];
231
- result = (Class <?>) (ParameterizedType .class .isAssignableFrom (argType .getClass ())
232
- ? ParameterizedType .class .cast (argType ).getRawType () : argType );
233
- }
234
- return result ;
235
- }
236
-
237
- private <T > Object getValue (final VPackSlice vpack , final Field field , final Class <T > type )
221
+ private <T > Object getValue (final VPackSlice vpack , final Class <T > type , final FieldInfo fieldInfo )
238
222
throws InstantiationException , IllegalAccessException , NoSuchMethodException , InvocationTargetException ,
239
223
VPackException {
240
224
final Object value ;
@@ -245,64 +229,68 @@ private <T> Object getValue(final VPackSlice vpack, final Field field, final Cla
245
229
if (deserializer != null ) {
246
230
value = ((VPackDeserializer <Object >) deserializer ).deserialize (vpack , deserializationContext );
247
231
} else if (type .isArray ()) {
248
- value = deserializeArray (vpack , field , type );
232
+ value = deserializeArray (vpack , type , fieldInfo );
249
233
} else if (type .isEnum ()) {
250
234
value = Enum .valueOf ((Class <? extends Enum >) type , vpack .getAsString ());
251
235
} else if (Collection .class .isAssignableFrom (type )) {
252
- value = deserializeCollection (vpack , field , type );
236
+ value = deserializeCollection (vpack , type , fieldInfo );
253
237
} else if (Map .class .isAssignableFrom (type )) {
254
- value = deserializeMap (vpack , field , type );
238
+ value = deserializeMap (vpack , type , fieldInfo );
255
239
} else {
256
240
value = deserializeObject (vpack , type );
257
241
}
258
242
}
259
243
return value ;
260
244
}
261
245
262
- private <T > Object deserializeArray (final VPackSlice vpack , final Field field , final Class <T > type )
246
+ private <T > Object deserializeArray (final VPackSlice vpack , final Class <T > type , final FieldInfo fieldInfo )
263
247
throws InstantiationException , IllegalAccessException , NoSuchMethodException , InvocationTargetException ,
264
- ArrayIndexOutOfBoundsException , IllegalArgumentException , VPackException {
248
+ VPackException {
265
249
final int length = (int ) vpack .getLength ();
266
- final Class <?> componentType = getComponentType (field , type , 0 );
250
+ final Class <?> componentType = fieldInfo != null ? fieldInfo .getParameterizedTypes ()[0 ]
251
+ : type .getComponentType ();
267
252
final Object value = Array .newInstance (componentType , length );
268
253
for (int i = 0 ; i < length ; i ++) {
269
- Array .set (value , i , getValue (vpack .at (i ), null , componentType ));
254
+ Array .set (value , i , getValue (vpack .at (i ), componentType , null ));
270
255
}
271
256
return value ;
272
257
}
273
258
274
- private <T > Object deserializeCollection (final VPackSlice vpack , final Field field , final Class <T > type )
259
+ private <T > Object deserializeCollection (final VPackSlice vpack , final Class <T > type , final FieldInfo fieldInfo )
275
260
throws InstantiationException , IllegalAccessException , NoSuchMethodException , InvocationTargetException ,
276
261
VPackException {
277
262
final Collection value = (Collection ) createInstance (type );
278
263
final long length = vpack .getLength ();
279
264
if (length > 0 ) {
280
- final Class <?> componentType = getComponentType ( field , type , 0 ) ;
265
+ final Class <?> componentType = fieldInfo . getParameterizedTypes ()[ 0 ] ;
281
266
for (int i = 0 ; i < length ; i ++) {
282
- value .add (getValue (vpack .at (i ), null , componentType ));
267
+ value .add (getValue (vpack .at (i ), componentType , null ));
283
268
}
284
269
}
285
270
return value ;
286
271
}
287
272
288
- private <T > Object deserializeMap (final VPackSlice vpack , final Field field , final Class <T > type )
273
+ private <T > Object deserializeMap (final VPackSlice vpack , final Class <T > type , final FieldInfo fieldInfo )
289
274
throws InstantiationException , IllegalAccessException , NoSuchMethodException , InvocationTargetException ,
290
275
VPackException {
291
276
final int length = (int ) vpack .getLength ();
292
277
final Map value = (Map ) createInstance (type );
293
278
if (length > 0 ) {
294
- final Class <?> keyType = getComponentType (field , type , 0 );
295
- final Class <?> valueType = getComponentType (field , type , 1 );
279
+ final Class <?>[] parameterizedTypes = fieldInfo .getParameterizedTypes ();
280
+ final Class <?> keyType = parameterizedTypes [0 ];
281
+ final Class <?> valueType = parameterizedTypes [1 ];
296
282
if (isStringableKeyType (keyType )) {
297
- for (int i = 0 ; i < vpack .getLength (); i ++) {
298
- value .put (getKeyfromString (vpack .keyAt (i ).makeKey ().getAsString (), keyType ),
299
- getValue (vpack .valueAt (i ), null , valueType ));
283
+ for (final Iterator <VPackSlice > iterator = vpack .iterator (); iterator .hasNext ();) {
284
+ final VPackSlice key = iterator .next ();
285
+ final VPackSlice valueAt = new VPackSlice (key .getVpack (), key .getStart () + key .getByteSize ());
286
+ value .put (getKeyfromString (key .makeKey ().getAsString (), keyType ),
287
+ getValue (valueAt , valueType , null ));
300
288
}
301
289
} else {
302
290
for (int i = 0 ; i < vpack .getLength (); i ++) {
303
291
final VPackSlice entry = vpack .at (i );
304
- final Object mapKey = getValue (entry .get (ATTR_KEY ), null , keyType );
305
- final Object mapValue = getValue (entry .get (ATTR_VALUE ), null , valueType );
292
+ final Object mapKey = getValue (entry .get (ATTR_KEY ), keyType , null );
293
+ final Object mapValue = getValue (entry .get (ATTR_VALUE ), valueType , null );
306
294
value .put (mapKey , mapValue );
307
295
}
308
296
}
@@ -381,15 +369,16 @@ private void serializeField(final Object entity, final VPackBuilder builder, fin
381
369
final Field field = fieldInfo .getField ();
382
370
final Class <?> type = field .getType ();
383
371
final Object value = field .get (entity );
384
- addValue (field , fieldName , type , value , builder );
372
+ addValue (field , fieldName , type , value , builder , fieldInfo );
385
373
}
386
374
387
375
private void addValue (
388
376
final Field field ,
389
377
final String name ,
390
378
final Class <?> type ,
391
379
final Object value ,
392
- final VPackBuilder builder )
380
+ final VPackBuilder builder ,
381
+ final FieldInfo fieldInfo )
393
382
throws NoSuchMethodException , IllegalAccessException , InvocationTargetException , VPackException {
394
383
395
384
if (value == null ) {
@@ -405,7 +394,7 @@ private void addValue(
405
394
} else if (Iterable .class .isAssignableFrom (type )) {
406
395
serializeIterable (name , value , builder );
407
396
} else if (Map .class .isAssignableFrom (type )) {
408
- serializeMap (field , name , type , value , builder );
397
+ serializeMap (name , type , value , builder , fieldInfo );
409
398
} else {
410
399
serializeObject (name , value , builder );
411
400
}
@@ -418,7 +407,7 @@ private void serializeArray(final String name, final Object value, final VPackBu
418
407
builder .add (name , new Value (ValueType .ARRAY ));
419
408
for (int i = 0 ; i < Array .getLength (value ); i ++) {
420
409
final Object element = Array .get (value , i );
421
- addValue (null , null , element .getClass (), element , builder );
410
+ addValue (null , null , element .getClass (), element , builder , null );
422
411
}
423
412
builder .close ();
424
413
}
@@ -428,35 +417,36 @@ private void serializeIterable(final String name, final Object value, final VPac
428
417
builder .add (name , new Value (ValueType .ARRAY ));
429
418
for (final Iterator iterator = Iterable .class .cast (value ).iterator (); iterator .hasNext ();) {
430
419
final Object element = iterator .next ();
431
- addValue (null , null , element .getClass (), element , builder );
420
+ addValue (null , null , element .getClass (), element , builder , null );
432
421
}
433
422
builder .close ();
434
423
}
435
424
436
425
private void serializeMap (
437
- final Field field ,
438
426
final String name ,
439
427
final Class <?> type ,
440
428
final Object value ,
441
- final VPackBuilder builder )
429
+ final VPackBuilder builder ,
430
+ final FieldInfo fieldInfo )
442
431
throws NoSuchMethodException , IllegalAccessException , InvocationTargetException , VPackException {
443
432
final Map map = Map .class .cast (value );
444
433
if (map .size () > 0 ) {
445
- final Class <?> keyType = getComponentType ( field , type , 0 ) ;
434
+ final Class <?> keyType = fieldInfo . getParameterizedTypes ()[ 0 ] ;
446
435
if (isStringableKeyType (keyType )) {
447
436
builder .add (name , new Value (ValueType .OBJECT ));
448
437
final Set <Entry <?, ?>> entrySet = map .entrySet ();
449
438
for (final Entry <?, ?> entry : entrySet ) {
450
- addValue (null , keyToString (entry .getKey ()), entry .getValue ().getClass (), entry .getValue (), builder );
439
+ addValue (null , keyToString (entry .getKey ()), entry .getValue ().getClass (), entry .getValue (), builder ,
440
+ null );
451
441
}
452
442
builder .close ();
453
443
} else {
454
444
builder .add (name , new Value (ValueType .ARRAY ));
455
445
final Set <Entry <?, ?>> entrySet = map .entrySet ();
456
446
for (final Entry <?, ?> entry : entrySet ) {
457
447
builder .add (null , new Value (ValueType .OBJECT ));
458
- addValue (null , ATTR_KEY , entry .getKey ().getClass (), entry .getKey (), builder );
459
- addValue (null , ATTR_VALUE , entry .getValue ().getClass (), entry .getValue (), builder );
448
+ addValue (null , ATTR_KEY , entry .getKey ().getClass (), entry .getKey (), builder , null );
449
+ addValue (null , ATTR_VALUE , entry .getValue ().getClass (), entry .getValue (), builder , null );
460
450
builder .close ();
461
451
}
462
452
builder .close ();
0 commit comments