X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fserial%2FSerialUtils.java;h=502d0ba72ccdd0da2b77cfca67e4cc5e3c25a87c;hb=cd26ee071b3853e21afe1732244d666e231f21ed;hp=61bca4f6d1a9b0e238c44b72c4589327d53d09e8;hpb=72648e757f648cd152bc00dfb83f895260f037a0;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/serial/SerialUtils.java b/src/be/nikiroo/utils/serial/SerialUtils.java index 61bca4f..502d0ba 100644 --- a/src/be/nikiroo/utils/serial/SerialUtils.java +++ b/src/be/nikiroo/utils/serial/SerialUtils.java @@ -173,7 +173,6 @@ public class SerialUtils { throws ClassNotFoundException, NoSuchMethodException { String desc = null; - try { Class clazz = getClass(type); String className = clazz.getName(); @@ -183,11 +182,11 @@ public class SerialUtils { if (className.contains("$")) { for (String parentName = className.substring(0, className.lastIndexOf('$'));; parentName = parentName - .substring(0, parentName.lastIndexOf('$'))) { + .substring(0, parentName.lastIndexOf('$'))) { Object parent = createObject(parentName); args.add(parent); classes.add(parent.getClass()); - + if (!parentName.contains("$")) { break; } @@ -199,7 +198,7 @@ public class SerialUtils { String end = ""; for (Class parent = clazz; parent != null && !parent.equals(Object.class); parent = parent - .getSuperclass()) { + .getSuperclass()) { if (!desc.isEmpty()) { desc += " [:"; end += "]"; @@ -209,8 +208,18 @@ public class SerialUtils { desc += end; // - ctor = clazz.getDeclaredConstructor( - classes.toArray(new Class[] {})); + try { + ctor = clazz.getDeclaredConstructor(classes + .toArray(new Class[] {})); + } catch (NoSuchMethodException nsme) { + // TODO: it seems e do not always need a parameter for each + // level, so we currently try "ALL" levels or "FIRST" level + // only -> we should check the actual rule and use it + ctor = clazz.getDeclaredConstructor(classes.get(0)); + Object firstParent = args.get(0); + args.clear(); + args.add(firstParent); + } desc = null; } else { ctor = clazz.getDeclaredConstructor(); @@ -222,8 +231,8 @@ public class SerialUtils { throw e; } catch (NoSuchMethodException e) { if (desc != null) { - throw new NoSuchMethodException( - "Empty constructor not found: " + desc); + throw new NoSuchMethodException("Empty constructor not found: " + + desc); } throw e; } catch (Exception e) { @@ -326,7 +335,13 @@ public class SerialUtils { } /** - * Encode the object into the given builder if possible (if supported). + * Encode the object into the given builder if possible and if supported. + *

+ * A supported object in this context means an object we can directly + * encode, like an Integer or a String. Custom objects and arrays are also + * considered supported, but compound objects are not supported here. + *

+ * For compound objects, you should use {@link Exporter}. * * @param builder * the builder to append to @@ -377,7 +392,13 @@ public class SerialUtils { } /** - * Decode the data into an equivalent source object. + * Decode the data into an equivalent supported source object. + *

+ * A supported object in this context means an object we can directly + * encode, like an Integer or a String. Custom objects and arrays are also + * considered supported, but compound objects are not supported here. + *

+ * For compound objects, you should use {@link Importer}. * * @param encodedValue * the encoded data, cannot be NULL