Update SerialUtils to be public
authorNiki Roo <niki@nikiroo.be>
Fri, 30 Jun 2017 16:01:12 +0000 (18:01 +0200)
committerNiki Roo <niki@nikiroo.be>
Fri, 30 Jun 2017 16:01:12 +0000 (18:01 +0200)
src/be/nikiroo/utils/serial/Importer.java
src/be/nikiroo/utils/serial/SerialUtils.java

index b3307fdc7220c4fa757b04e9038e720106ddc8a4..0709a96883c77b005f57ddf04de4ac15b04165ac 100644 (file)
@@ -1,11 +1,10 @@
 package be.nikiroo.utils.serial;
 
-import java.lang.reflect.Constructor;
-import java.lang.reflect.Field;
-import java.lang.reflect.InvocationTargetException;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Scanner;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Field;
 
 import be.nikiroo.utils.IOUtils;
 import be.nikiroo.utils.StringUtils;
@@ -91,7 +90,7 @@ public class Importer {
                        if (link) {
                                me = map.get(ref);
                        } else {
-                               me = createSelf(line.substring(4).split("@")[0]);
+                               me = SerialUtils.createObject(line.substring(4).split("@")[0]);
                                map.put(ref, me);
                        }
                } else { // FIELD: new field
@@ -118,89 +117,6 @@ public class Importer {
                return false;
        }
 
-       /**
-        * Create an empty object of the given type.
-        * 
-        * @param type
-        *            the object type
-        * @return the object
-        * 
-        * @throws NoSuchMethodException
-        * @throws SecurityException
-        * @throws InstantiationException
-        * @throws IllegalAccessException
-        * @throws ClassNotFoundException
-        * @throws IllegalArgumentException
-        * @throws InvocationTargetException
-        */
-       private Object createSelf(String type) throws NoSuchMethodException,
-                       SecurityException, InstantiationException, IllegalAccessException,
-                       ClassNotFoundException, IllegalArgumentException,
-                       InvocationTargetException {
-
-               try {
-                       Class<?> clazz = getClass(type);
-                       if (clazz == null) {
-                               throw new ClassNotFoundException("Class not found: " + type);
-                       }
-
-                       String className = clazz.getName();
-                       Object[] args = null;
-                       Constructor<?> ctor = null;
-                       if (className.contains("$")) {
-                               Object javaParent = createSelf(className.substring(0,
-                                               className.lastIndexOf('$')));
-                               args = new Object[] { javaParent };
-                               ctor = clazz.getDeclaredConstructor(new Class[] { javaParent
-                                               .getClass() });
-                       } else {
-                               args = new Object[] {};
-                               ctor = clazz.getDeclaredConstructor();
-                       }
-
-                       ctor.setAccessible(true);
-                       return ctor.newInstance(args);
-               } catch (NoSuchMethodException e) {
-                       throw new NoSuchMethodException(
-                                       String.format(
-                                                       "Objects of type \"%s\" cannot be created by this code: maybe the class"
-                                                                       + " or its enclosing class doesn't have an empty constructor?",
-                                                       type));
-
-               }
-       }
-
-       private Class<?> getClass(String type) throws ClassNotFoundException,
-                       NoSuchMethodException {
-               Class<?> clazz = null;
-               try {
-                       clazz = Class.forName(type);
-               } catch (ClassNotFoundException e) {
-                       int pos = type.length();
-                       pos = type.lastIndexOf(".", pos);
-                       if (pos >= 0) {
-                               String parentType = type.substring(0, pos);
-                               String nestedType = type.substring(pos + 1);
-                               Class<?> javaParent = null;
-                               try {
-                                       javaParent = getClass(parentType);
-                                       parentType = javaParent.getName();
-                                       clazz = Class.forName(parentType + "$" + nestedType);
-                               } catch (Exception ee) {
-                               }
-
-                               if (javaParent == null) {
-                                       throw new NoSuchMethodException(
-                                                       "Class not found: "
-                                                                       + type
-                                                                       + " (the enclosing class cannot be created: maybe it doesn't have an empty constructor?)");
-                               }
-                       }
-               }
-
-               return clazz;
-       }
-
        private void setField(String name, Object value)
                        throws NoSuchFieldException, SecurityException,
                        IllegalArgumentException, IllegalAccessException {
index 657dbec45fb069e26b77cc7423c0c606ffdc6a39..3770c588ce53bc25e77b623ee715da6aa58b937e 100644 (file)
@@ -1,5 +1,8 @@
 package be.nikiroo.utils.serial;
 
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
 import java.io.NotSerializableException;
 import java.lang.reflect.Field;
 import java.util.HashMap;
@@ -13,13 +16,64 @@ import java.util.Map;
  * 
  * @author niki
  */
-class SerialUtils {
+public class SerialUtils {
        private static Map<String, CustomSerializer> customTypes;
 
        static {
                customTypes = new HashMap<String, CustomSerializer>();
                // TODO: add "default" custom serialisers
        }
+       
+       /**
+        * Create an empty object of the given type.
+        * 
+        * @param type
+        *            the object type (its class name)
+        * 
+        * @return the new object
+        * 
+        * @throws NoSuchMethodException  if the given class is not compatible with this code
+        * @throws ClassNotFoundException if the class cannot be found or created
+        */
+       public static Object createObject(String type) throws NoSuchMethodException,
+                       ClassNotFoundException {
+
+               try {
+                       Class<?> clazz = getClass(type);
+                       if (clazz == null) {
+                               throw new ClassNotFoundException("Class not found: " + type);
+                       }
+
+                       String className = clazz.getName();
+                       Object[] args = null;
+                       Constructor<?> ctor = null;
+                       if (className.contains("$")) {
+                               Object javaParent = createObject(className.substring(0,
+                                               className.lastIndexOf('$')));
+                               args = new Object[] { javaParent };
+                               ctor = clazz.getDeclaredConstructor(new Class[] { javaParent
+                                               .getClass() });
+                       } else {
+                               args = new Object[] {};
+                               ctor = clazz.getDeclaredConstructor();
+                       }
+
+                       ctor.setAccessible(true);
+                       return ctor.newInstance(args);
+               } catch (NoSuchMethodException e) {
+                       throw new NoSuchMethodException(
+                                       String.format(
+                                                       "Objects of type \"%s\" cannot be created by this code: maybe the class"
+                                                                       + " or its enclosing class doesn't have an empty constructor?",
+                                                       type));
+
+               }
+               catch (SecurityException e) { throw new ClassNotFoundException("Cannot instantiate: " + type, e); }
+               catch (InstantiationException e) { throw new ClassNotFoundException("Cannot instantiate: " + type, e); }
+               catch (IllegalAccessException e) { throw new ClassNotFoundException("Cannot instantiate: " + type, e); }
+               catch (IllegalArgumentException e) { throw new ClassNotFoundException("Cannot instantiate: " + type, e); }
+               catch (InvocationTargetException e) { throw new ClassNotFoundException("Cannot instantiate: " + type, e); }
+       }
 
        static public void addCustomSerializer(CustomSerializer serializer) {
                customTypes.put(serializer.getType(), serializer);
@@ -152,7 +206,38 @@ class SerialUtils {
                        return Integer.parseInt(encodedValue);
                }
        }
+       
+       static private Class<?> getClass(String type) throws ClassNotFoundException,
+                       NoSuchMethodException {
+               Class<?> clazz = null;
+               try {
+                       clazz = Class.forName(type);
+               } catch (ClassNotFoundException e) {
+                       int pos = type.length();
+                       pos = type.lastIndexOf(".", pos);
+                       if (pos >= 0) {
+                               String parentType = type.substring(0, pos);
+                               String nestedType = type.substring(pos + 1);
+                               Class<?> javaParent = null;
+                               try {
+                                       javaParent = getClass(parentType);
+                                       parentType = javaParent.getName();
+                                       clazz = Class.forName(parentType + "$" + nestedType);
+                               } catch (Exception ee) {
+                               }
 
+                               if (javaParent == null) {
+                                       throw new NoSuchMethodException(
+                                                       "Class not found: "
+                                                                       + type
+                                                                       + " (the enclosing class cannot be created: maybe it doesn't have an empty constructor?)");
+                               }
+                       }
+               }
+
+               return clazz;
+       }
+       
        // aa bb -> "aa\tbb"
        private static void encodeString(StringBuilder builder, String raw) {
                builder.append('\"');