Version 4.5.0
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / SerialUtils.java
index 61bca4f6d1a9b0e238c44b72c4589327d53d09e8..706d579970f9456fd430d4ae2020185aa8c7ed24 100644 (file)
@@ -66,8 +66,8 @@ public class SerialUtils {
                                                if (!SerialUtils.encode(builder, item)) {
                                                        try {
                                                                // use ZIP: if not
-                                                               builder.append(new Exporter().append(item)
-                                                                               .toString(true));
+                                                               new Exporter().append(item).appendTo(builder,
+                                                                               true, false);
                                                        } catch (NotSerializableException e) {
                                                                throw new UnknownFormatConversionException(e
                                                                                .getMessage());
@@ -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.
+        * <p>
+        * 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 <b>compound objects are not supported here</b>.
+        * <p>
+        * 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.
+        * <p>
+        * 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 <b>compound objects are not supported here</b>.
+        * <p>
+        * For compound objects, you should use {@link Importer}.
         * 
         * @param encodedValue
         *            the encoded data, cannot be NULL