summaryrefslogtreecommitdiff
path: root/org/freedesktop/dbus/Marshalling.java
blob: 7f6797c5302420a5e2a23463c6475c67dafededb (plain)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/*
   D-Bus Java Implementation
   Copyright (c) 2005-2006 Matthew Johnson

   This program is free software; you can redistribute it and/or modify it
   under the terms of either the GNU Lesser General Public License Version 2 or the
   Academic Free Licence Version 2.1.

   Full licence texts are included in the COPYING file with this program.
*/
package org.freedesktop.dbus;

import static org.freedesktop.dbus.Gettext._;

import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.freedesktop.dbus.exceptions.DBusException;
import org.freedesktop.dbus.types.DBusListType;
import org.freedesktop.dbus.types.DBusMapType;
import org.freedesktop.dbus.types.DBusStructType;

import cx.ath.matthew.debug.Debug;

/**
 * Contains static methods for marshalling values.
 */
public class Marshalling
{
   private static Map<Type, String[]> typeCache = new HashMap<Type, String[]>();
   /**
    * Will return the DBus type corresponding to the given Java type.
    * Note, container type should have their ParameterizedType not their
    * Class passed in here.
    * @param c The Java types.
    * @return The DBus types.
    * @throws DBusException If the given type cannot be converted to a DBus type.
    */
   public static String getDBusType(Type[] c) throws DBusException
   {
      StringBuffer sb = new StringBuffer();
      for (Type t: c) 
         for (String s: getDBusType(t))
            sb.append(s);
      return sb.toString();
   }
   /**
    * Will return the DBus type corresponding to the given Java type.
    * Note, container type should have their ParameterizedType not their
    * Class passed in here.
    * @param c The Java type.
    * @return The DBus type.
    * @throws DBusException If the given type cannot be converted to a DBus type.
    */
   public static String[] getDBusType(Type c) throws DBusException
   {
      String[] cached = typeCache.get(c);
      if (null != cached) return cached;
      cached = getDBusType(c, false);
      typeCache.put(c, cached);
      return cached;
   }
   /**
    * Will return the DBus type corresponding to the given Java type.
    * Note, container type should have their ParameterizedType not their
    * Class passed in here.
    * @param c The Java type.
    * @param basic If true enforces this to be a non-compound type. (compound types are Maps, Structs and Lists/arrays).
    * @return The DBus type.
    * @throws DBusException If the given type cannot be converted to a DBus type.
    */
   public static String[] getDBusType(Type c, boolean basic) throws DBusException
   {
      return recursiveGetDBusType(c, basic, 0);
   }
   private static StringBuffer[] out = new StringBuffer[10];
   @SuppressWarnings("unchecked")
   public static String[] recursiveGetDBusType(Type c, boolean basic, int level) throws DBusException
   {
      if (out.length <= level) {
         StringBuffer[] newout = new StringBuffer[out.length];
         System.arraycopy(out, 0, newout, 0, out.length);
         out = newout;
      }
      if (null == out[level]) out[level] = new StringBuffer();
      else out[level].delete(0, out[level].length());      

      if (basic && !(c instanceof Class))
         throw new DBusException(c+_(" is not a basic type"));

      if (c instanceof TypeVariable) out[level].append((char) Message.ArgumentType.VARIANT);
      else if (c instanceof GenericArrayType) {
         out[level].append((char) Message.ArgumentType.ARRAY);
         String[] s = recursiveGetDBusType(((GenericArrayType) c).getGenericComponentType(), false, level+1);
         if (s.length != 1) throw new DBusException(_("Multi-valued array types not permitted"));
         out[level].append(s[0]);
      } else if ((c instanceof Class && 
               DBusSerializable.class.isAssignableFrom((Class<? extends Object>) c)) ||
            (c instanceof ParameterizedType &&
             DBusSerializable.class.isAssignableFrom((Class<? extends Object>) ((ParameterizedType) c).getRawType()))) {
         // it's a custom serializable type
         Type[] newtypes = null;
         if (c instanceof Class)  {
            for (Method m: ((Class<? extends Object>) c).getDeclaredMethods()) 
               if (m.getName().equals("deserialize")) 
                  newtypes = m.getGenericParameterTypes();
         }
         else 
            for (Method m: ((Class<? extends Object>) ((ParameterizedType) c).getRawType()).getDeclaredMethods()) 
               if (m.getName().equals("deserialize")) 
                  newtypes = m.getGenericParameterTypes();

         if (null == newtypes) throw new DBusException(_("Serializable classes must implement a deserialize method"));

         String[] sigs = new String[newtypes.length];
         for (int j = 0; j < sigs.length; j++) {
            String[] ss = recursiveGetDBusType(newtypes[j], false, level+1);
            if (1 != ss.length) throw new DBusException(_("Serializable classes must serialize to native DBus types"));
            sigs[j] = ss[0];
         }
         return sigs;
      } 
      else if (c instanceof ParameterizedType) {
         ParameterizedType p = (ParameterizedType) c;
         if (p.getRawType().equals(Map.class)) {
            out[level].append("a{");
            Type[] t = p.getActualTypeArguments();
            try {
               String[] s = recursiveGetDBusType(t[0], true, level+1);
               if (s.length != 1) throw new DBusException(_("Multi-valued array types not permitted"));
               out[level].append(s[0]);
               s = recursiveGetDBusType(t[1], false, level+1);
               if (s.length != 1) throw new DBusException(_("Multi-valued array types not permitted"));
               out[level].append(s[0]);
            } catch (ArrayIndexOutOfBoundsException AIOOBe) {
               if (AbstractConnection.EXCEPTION_DEBUG && Debug.debug) Debug.print(Debug.ERR, AIOOBe);
               throw new DBusException(_("Map must have 2 parameters"));
            }
            out[level].append('}');
         }
         else if (List.class.isAssignableFrom((Class<? extends Object>) p.getRawType())) {
            for (Type t: p.getActualTypeArguments()) {
               if (Type.class.equals(t)) 
                  out[level].append((char) Message.ArgumentType.SIGNATURE);
               else {
                  String[] s = recursiveGetDBusType(t, false, level+1);
                  if (s.length != 1) throw new DBusException(_("Multi-valued array types not permitted"));
                  out[level].append((char) Message.ArgumentType.ARRAY);
                  out[level].append(s[0]);
               }
            }
         } 
         else if (p.getRawType().equals(Variant.class)) {
            out[level].append((char) Message.ArgumentType.VARIANT);
         }
         else if (DBusInterface.class.isAssignableFrom((Class<? extends Object>) p.getRawType())) {
            out[level].append((char) Message.ArgumentType.OBJECT_PATH);
         }
         else if (Tuple.class.isAssignableFrom((Class<? extends Object>) p.getRawType())) {
            Type[] ts = p.getActualTypeArguments();
            Vector<String> vs = new Vector<String>();
            for (Type t: ts)
               for (String s: recursiveGetDBusType(t, false, level+1))
                  vs.add(s);
            return vs.toArray(new String[0]);
         }
         else
            throw new DBusException(_("Exporting non-exportable parameterized type ")+c);
      }
      
      else if (c.equals(Byte.class)) out[level].append((char) Message.ArgumentType.BYTE);
      else if (c.equals(Byte.TYPE)) out[level].append((char) Message.ArgumentType.BYTE);
      else if (c.equals(Boolean.class)) out[level].append((char) Message.ArgumentType.BOOLEAN);
      else if (c.equals(Boolean.TYPE)) out[level].append((char) Message.ArgumentType.BOOLEAN);
      else if (c.equals(Short.class)) out[level].append((char) Message.ArgumentType.INT16);
      else if (c.equals(Short.TYPE)) out[level].append((char) Message.ArgumentType.INT16);
      else if (c.equals(UInt16.class)) out[level].append((char) Message.ArgumentType.UINT16);
      else if (c.equals(Integer.class)) out[level].append((char) Message.ArgumentType.INT32);
      else if (c.equals(Integer.TYPE)) out[level].append((char) Message.ArgumentType.INT32);
      else if (c.equals(UInt32.class)) out[level].append((char) Message.ArgumentType.UINT32);
      else if (c.equals(Long.class)) out[level].append((char) Message.ArgumentType.INT64);
      else if (c.equals(Long.TYPE)) out[level].append((char) Message.ArgumentType.INT64);
      else if (c.equals(UInt64.class)) out[level].append((char) Message.ArgumentType.UINT64);
      else if (c.equals(Double.class)) out[level].append((char) Message.ArgumentType.DOUBLE);
      else if (c.equals(Double.TYPE)) out[level].append((char) Message.ArgumentType.DOUBLE);
      else if (c.equals(Float.class) && AbstractConnection.FLOAT_SUPPORT) out[level].append((char) Message.ArgumentType.FLOAT);
      else if (c.equals(Float.class)) out[level].append((char) Message.ArgumentType.DOUBLE);
      else if (c.equals(Float.TYPE) && AbstractConnection.FLOAT_SUPPORT) out[level].append((char) Message.ArgumentType.FLOAT);
      else if (c.equals(Float.TYPE)) out[level].append((char) Message.ArgumentType.DOUBLE);
      else if (c.equals(String.class)) out[level].append((char) Message.ArgumentType.STRING);
      else if (c.equals(Variant.class)) out[level].append((char) Message.ArgumentType.VARIANT);
      else if (c instanceof Class && 
            DBusInterface.class.isAssignableFrom((Class<? extends Object>) c)) out[level].append((char) Message.ArgumentType.OBJECT_PATH);
      else if (c instanceof Class && 
            Path.class.equals((Class<? extends Object>) c)) out[level].append((char) Message.ArgumentType.OBJECT_PATH);
      else if (c instanceof Class && 
            ObjectPath.class.equals((Class<? extends Object>) c)) out[level].append((char) Message.ArgumentType.OBJECT_PATH);
      else if (c instanceof Class && 
            ((Class<? extends Object>) c).isArray()) {
         if (Type.class.equals(((Class<? extends Object>) c).getComponentType()))
            out[level].append((char) Message.ArgumentType.SIGNATURE);
         else {
            out[level].append((char) Message.ArgumentType.ARRAY);
            String[] s = recursiveGetDBusType(((Class<? extends Object>) c).getComponentType(), false, level+1);
            if (s.length != 1) throw new DBusException(_("Multi-valued array types not permitted"));
            out[level].append(s[0]);
         }
      } else if (c instanceof Class && 
            Struct.class.isAssignableFrom((Class<? extends Object>) c)) {
         out[level].append((char) Message.ArgumentType.STRUCT1);
         Type[] ts = Container.getTypeCache(c);
         if (null == ts) {
            Field[] fs = ((Class<? extends Object>) c).getDeclaredFields();
            ts = new Type[fs.length];
            for (Field f : fs) {
               Position p = f.getAnnotation(Position.class);
               if (null == p) continue;
               ts[p.value()] = f.getGenericType();
           }
            Container.putTypeCache(c, ts);
         }

         for (Type t: ts)
            if (t != null)
               for (String s: recursiveGetDBusType(t, false, level+1))
                  out[level].append(s);
         out[level].append(')');
      } else {
         throw new DBusException(_("Exporting non-exportable type ")+c);
      }

      if (Debug.debug) Debug.print(Debug.VERBOSE, "Converted Java type: "+c+" to D-Bus Type: "+out[level]);

      return new String[] { out[level].toString() };
   }

   /**
    * Converts a dbus type string into Java Type objects, 
    * @param dbus The DBus type or types.
    * @param rv Vector to return the types in.
    * @param limit Maximum number of types to parse (-1 == nolimit).
    * @return number of characters parsed from the type string.
    */
   public static int getJavaType(String dbus, List<Type> rv, int limit) throws DBusException
   {
      if (null == dbus || "".equals(dbus) || 0 == limit) return 0;

      try {
         int i = 0;
         for (; i < dbus.length() && (-1 == limit || limit > rv.size()); i++) 
            switch(dbus.charAt(i)) {
               case Message.ArgumentType.STRUCT1:
                  int j = i+1;
                  for (int c = 1; c > 0; j++) {
                     if (')' == dbus.charAt(j)) c--;
                     else if (Message.ArgumentType.STRUCT1 == dbus.charAt(j)) c++;
                  }

                  Vector<Type> contained = new Vector<Type>();
                  int c = getJavaType(dbus.substring(i+1, j-1), contained, -1);
                  rv.add(new DBusStructType(contained.toArray(new Type[0])));
                  i = j;
                  break;                     
               case Message.ArgumentType.ARRAY:
                  if (Message.ArgumentType.DICT_ENTRY1 == dbus.charAt(i+1)) {
                     contained = new Vector<Type>();
                     c = getJavaType(dbus.substring(i+2), contained, 2);
                     rv.add(new DBusMapType(contained.get(0), contained.get(1)));
                     i += (c+2);
                  } else {
                     contained = new Vector<Type>();
                     c = getJavaType(dbus.substring(i+1), contained, 1);
                     rv.add(new DBusListType(contained.get(0)));
                     i += c;
                  }
                  break;
               case Message.ArgumentType.VARIANT:
                  rv.add(Variant.class);
                  break;
               case Message.ArgumentType.BOOLEAN:
                  rv.add(Boolean.class);
                  break;
               case Message.ArgumentType.INT16:
                  rv.add(Short.class);
                  break;
               case Message.ArgumentType.BYTE:
                  rv.add(Byte.class);
                  break;
               case Message.ArgumentType.OBJECT_PATH:
                  rv.add(DBusInterface.class);
                  break;
               case Message.ArgumentType.UINT16:
                  rv.add(UInt16.class);
                  break;
               case Message.ArgumentType.INT32:
                  rv.add(Integer.class);
                  break;
               case Message.ArgumentType.UINT32:
                  rv.add(UInt32.class);
                  break;
               case Message.ArgumentType.INT64:
                  rv.add(Long.class);
                  break;
               case Message.ArgumentType.UINT64:
                  rv.add(UInt64.class);
                  break;
               case Message.ArgumentType.DOUBLE:
                  rv.add(Double.class);
                  break;
               case Message.ArgumentType.FLOAT:
                  rv.add(Float.class);
                  break;
               case Message.ArgumentType.STRING:
                  rv.add(String.class);
                  break;
               case Message.ArgumentType.SIGNATURE:
                  rv.add(Type[].class);
                  break;
               case Message.ArgumentType.DICT_ENTRY1:
                  rv.add(Map.Entry.class);
                  contained = new Vector<Type>();
                  c = getJavaType(dbus.substring(i+1), contained, 2);
                  i+=c+1;
                  break;
               default:
                  throw new DBusException(MessageFormat.format(_("Failed to parse DBus type signature: {0} ({1})."), new Object[] { dbus, dbus.charAt(i) }));
            }
         return i;
      } catch (IndexOutOfBoundsException IOOBe) {
         if (AbstractConnection.EXCEPTION_DEBUG && Debug.debug) Debug.print(Debug.ERR, IOOBe);
         throw new DBusException(_("Failed to parse DBus type signature: ")+dbus);
      }
   }
   /**
    * Recursively converts types for serialization onto DBus.
    * @param parameters The parameters to convert.
    * @param types The (possibly generic) types of the parameters.
    * @return The converted parameters.
    * @throws DBusException Thrown if there is an error in converting the objects.
    */
   @SuppressWarnings("unchecked")
   public static Object[] convertParameters(Object[] parameters, Type[] types, AbstractConnection conn) throws DBusException
   {
      if (null == parameters) return null;
      for (int i = 0; i < parameters.length; i++) {
         if (Debug.debug) Debug.print(Debug.VERBOSE,"Converting "+i+" from "+parameters[i]+" to "+types[i]);
         if (null == parameters[i]) continue;

         if (parameters[i] instanceof DBusSerializable) {
            for (Method m: parameters[i].getClass().getDeclaredMethods()) 
               if (m.getName().equals("deserialize")) {
                  Type[] newtypes = m.getParameterTypes();
                  Type[] expand = new Type[types.length + newtypes.length - 1];
                  System.arraycopy(types, 0, expand, 0, i); 
                  System.arraycopy(newtypes, 0, expand, i, newtypes.length); 
                  System.arraycopy(types, i+1, expand, i+newtypes.length, types.length-i-1); 
                  types = expand;
                  Object[] newparams = ((DBusSerializable) parameters[i]).serialize();
                  Object[] exparams = new Object[parameters.length + newparams.length - 1];
                  System.arraycopy(parameters, 0, exparams, 0, i);
                  System.arraycopy(newparams, 0, exparams, i, newparams.length);
                  System.arraycopy(parameters, i+1, exparams, i+newparams.length, parameters.length-i-1);
                  parameters = exparams;
               }
            i--;
         } else if (parameters[i] instanceof Tuple) {
            Type[] newtypes = ((ParameterizedType) types[i]).getActualTypeArguments();
            Type[] expand = new Type[types.length + newtypes.length - 1];
            System.arraycopy(types, 0, expand, 0, i);
            System.arraycopy(newtypes, 0, expand, i, newtypes.length);
            System.arraycopy(types, i+1, expand, i+newtypes.length, types. length-i-1);
            types = expand;
            Object[] newparams = ((Tuple) parameters[i]).getParameters();
            Object[] exparams = new Object[parameters.length + newparams.length - 1];
            System.arraycopy(parameters, 0, exparams, 0, i);
            System.arraycopy(newparams, 0, exparams, i, newparams.length);
            System.arraycopy(parameters, i+1, exparams, i+newparams.length, parameters.length-i-1);
            parameters = exparams;
            if (Debug.debug) Debug.print(Debug.VERBOSE, "New params: "+Arrays.deepToString(parameters)+" new types: "+Arrays.deepToString(types));
            i--;
         } else if (types[i] instanceof TypeVariable &&
               !(parameters[i] instanceof Variant)) 
            // its an unwrapped variant, wrap it
            parameters[i] = new Variant<Object>(parameters[i]);
         else if (parameters[i] instanceof DBusInterface)
            parameters[i] = conn.getExportedObject((DBusInterface) parameters[i]);
      }
      return parameters;
   }
   @SuppressWarnings("unchecked")
   static Object deSerializeParameter(Object parameter, Type type, AbstractConnection conn) throws Exception
   {
      if (Debug.debug) Debug.print(Debug.VERBOSE, "Deserializing from "+parameter.getClass()+" to "+type.getClass());
      if (null == parameter) 
         return null;

      // its a wrapped variant, unwrap it
      if (type instanceof TypeVariable 
            && parameter instanceof Variant) {
         parameter = ((Variant)parameter).getValue();
      }

      // Turn a signature into a Type[]
      if (type instanceof Class
            && ((Class) type).isArray()
            && ((Class) type).getComponentType().equals(Type.class)
            && parameter instanceof String) {
         Vector<Type> rv = new Vector<Type>();
         getJavaType((String) parameter, rv, -1);
         parameter = rv.toArray(new Type[0]);
      }

      // its an object path, get/create the proxy
      if (parameter instanceof ObjectPath) {
         if (type instanceof Class && DBusInterface.class.isAssignableFrom((Class) type))
            parameter = conn.getExportedObject(
                  ((ObjectPath) parameter).source,
                  ((ObjectPath) parameter).path);
         else
            parameter = new Path(((ObjectPath) parameter).path);
      }
      
      // it should be a struct. create it
      if (parameter instanceof Object[] && 
            type instanceof Class &&
            Struct.class.isAssignableFrom((Class) type)) {
         if (Debug.debug) Debug.print(Debug.VERBOSE, "Creating Struct "+type+" from "+parameter);
         Type[] ts = Container.getTypeCache(type);
         if (null == ts) {
            Field[] fs = ((Class) type).getDeclaredFields();
            ts = new Type[fs.length];
            for (Field f : fs) {
               Position p = f.getAnnotation(Position.class);
               if (null == p) continue;
               ts[p.value()] = f.getGenericType();
           }
            Container.putTypeCache(type, ts);
         }

         // recurse over struct contents
         parameter = deSerializeParameters((Object[]) parameter, ts, conn);
         for (Constructor con: ((Class) type).getDeclaredConstructors()) {
            try {
               parameter = con.newInstance((Object[]) parameter);
               break;
            } catch (IllegalArgumentException IAe) {}
         }
      }

      // recurse over arrays
      if (parameter instanceof Object[]) {
         Type[] ts = new Type[((Object[]) parameter).length];
         Arrays.fill(ts, parameter.getClass().getComponentType());
         parameter = deSerializeParameters((Object[]) parameter,
               ts, conn);
      }
      if (parameter instanceof List) {
         Type type2;
         if (type instanceof ParameterizedType)
            type2 = ((ParameterizedType) type).getActualTypeArguments()[0];
         else if (type instanceof GenericArrayType)
            type2 = ((GenericArrayType) type).getGenericComponentType();
         else if (type instanceof Class && ((Class) type).isArray())
            type2 = ((Class) type).getComponentType();
         else
            type2 = null;
         if (null != type2)
            parameter = deSerializeParameters((List) parameter, type2, conn);
      }

      // correct floats if appropriate
      if (type.equals(Float.class) || type.equals(Float.TYPE)) 
         if (!(parameter instanceof Float))
            parameter = ((Number) parameter).floatValue();

      // make sure arrays are in the correct format
      if (parameter instanceof Object[] ||
            parameter instanceof List ||
            parameter.getClass().isArray()) {
         if (type instanceof ParameterizedType)
            parameter = ArrayFrob.convert(parameter,
                  (Class<? extends Object>) ((ParameterizedType) type).getRawType());
         else if (type instanceof GenericArrayType) {
            Type ct = ((GenericArrayType) type).getGenericComponentType();
            Class cc = null;
            if (ct instanceof Class)
               cc = (Class) ct;
            if (ct instanceof ParameterizedType)
               cc = (Class) ((ParameterizedType) ct).getRawType();
            Object o = Array.newInstance(cc, 0);
            parameter = ArrayFrob.convert(parameter,
                  o.getClass());
         } else if (type instanceof Class &&
               ((Class) type).isArray()) {
            Class cc = ((Class) type).getComponentType();
            if ((cc.equals(Float.class) || cc.equals(Float.TYPE))
                  && (parameter instanceof double[])) {
               double[] tmp1 = (double[]) parameter;
               float[] tmp2 = new float[tmp1.length];
               for (int i = 0; i < tmp1.length; i++)
                  tmp2[i] = (float) tmp1[i];
               parameter = tmp2;
            }
            Object o = Array.newInstance(cc, 0);
            parameter = ArrayFrob.convert(parameter,
                  o.getClass());
         }
      }
      return parameter;
   }
   static List<Object> deSerializeParameters(List<Object> parameters, Type type, AbstractConnection conn) throws Exception
   {
      if (Debug.debug) Debug.print(Debug.VERBOSE, "Deserializing from "+parameters+" to "+type);
      if (null == parameters) return null;
      for (int i = 0; i < parameters.size(); i++) {
         if (null == parameters.get(i)) continue;

         /* DO NOT DO THIS! IT'S REALLY NOT SUPPORTED! 
          * if (type instanceof Class &&
               DBusSerializable.class.isAssignableFrom((Class) types[i])) {
            for (Method m: ((Class) types[i]).getDeclaredMethods()) 
               if (m.getName().equals("deserialize")) {
                  Type[] newtypes = m.getGenericParameterTypes();
                  try {
                     Object[] sub = new Object[newtypes.length];
                     System.arraycopy(parameters, i, sub, 0, newtypes.length); 
                     sub = deSerializeParameters(sub, newtypes, conn);
                     DBusSerializable sz = (DBusSerializable) ((Class) types[i]).newInstance();
                     m.invoke(sz, sub);
                     Object[] compress = new Object[parameters.length - newtypes.length + 1];
                     System.arraycopy(parameters, 0, compress, 0, i);
                     compress[i] = sz;
                     System.arraycopy(parameters, i + newtypes.length, compress, i+1, parameters.length - i - newtypes.length);
                     parameters = compress;
                  } catch (ArrayIndexOutOfBoundsException AIOOBe) {
                     if (AbstractConnection.EXCEPTION_DEBUG && Debug.debug) Debug.print(Debug.ERR, AIOOBe);
                     throw new DBusException("Not enough elements to create custom object from serialized data ("+(parameters.size()-i)+" < "+(newtypes.length)+")");
                  }
               }
         } else*/
            parameters.set(i, deSerializeParameter(parameters.get(i), type, conn));
      }
      return parameters;
   }

   @SuppressWarnings("unchecked")
   static Object[] deSerializeParameters(Object[] parameters, Type[] types, AbstractConnection conn) throws Exception
   {
      if (Debug.debug) Debug.print(Debug.VERBOSE, "Deserializing from "+Arrays.deepToString(parameters)+" to "+Arrays.deepToString(types));
      if (null == parameters) return null;

      if (types.length == 1 && types[0] instanceof ParameterizedType
            && Tuple.class.isAssignableFrom((Class) ((ParameterizedType) types[0]).getRawType())) {
         types = ((ParameterizedType) types[0]).getActualTypeArguments();
      }

      for (int i = 0; i < parameters.length; i++) {
         // CHECK IF ARRAYS HAVE THE SAME LENGTH <-- has to happen after expanding parameters
         if (i >= types.length) {
            if (Debug.debug) {
               for (int j = 0; j < parameters.length; j++) {
                  Debug.print(Debug.ERR, String.format("Error, Parameters difference (%1d, '%2s')", j, parameters[j].toString()));
               }
            }
            throw new DBusException(_("Error deserializing message: number of parameters didn't match receiving signature"));
         }
         if (null == parameters[i]) continue;

         if ((types[i] instanceof Class &&
               DBusSerializable.class.isAssignableFrom((Class<? extends Object>) types[i])) ||
               (types[i] instanceof ParameterizedType &&
               DBusSerializable.class.isAssignableFrom((Class<? extends Object>) ((ParameterizedType) types[i]).getRawType()))) {
            Class<? extends DBusSerializable> dsc;
            if (types[i] instanceof Class)
               dsc = (Class<? extends DBusSerializable>) types[i];
            else
               dsc = (Class<? extends DBusSerializable>) ((ParameterizedType) types[i]).getRawType();
            for (Method m: dsc.getDeclaredMethods()) 
               if (m.getName().equals("deserialize")) {
                  Type[] newtypes = m.getGenericParameterTypes();
                  try {
                     Object[] sub = new Object[newtypes.length];
                     System.arraycopy(parameters, i, sub, 0, newtypes.length); 
                     sub = deSerializeParameters(sub, newtypes, conn);
                     DBusSerializable sz = dsc.newInstance();
                     m.invoke(sz, sub);
                     Object[] compress = new Object[parameters.length - newtypes.length + 1];
                     System.arraycopy(parameters, 0, compress, 0, i);
                     compress[i] = sz;
                     System.arraycopy(parameters, i + newtypes.length, compress, i+1, parameters.length - i - newtypes.length);
                     parameters = compress;
                  } catch (ArrayIndexOutOfBoundsException AIOOBe) {
                     if (AbstractConnection.EXCEPTION_DEBUG && Debug.debug) Debug.print(Debug.ERR, AIOOBe);
                     throw new DBusException(MessageFormat.format(_("Not enough elements to create custom object from serialized data ({0} < {1})."), 
                                 new Object[] { parameters.length-i, newtypes.length }));
                  }
               }
         } else
            parameters[i] = deSerializeParameter(parameters[i], types[i], conn);
      }
      return parameters;
   }
}