+ /**
+ * Create an empty object of the given type.
+ *
+ * @param type
+ * the object type (its class name)
+ *
+ * @return the new object
+ *
+ * @throws ClassNotFoundException
+ * if the class cannot be found
+ * @throws NoSuchMethodException
+ * if the given class is not compatible with this code
+ */
+ public static Object createObject(String type)
+ throws ClassNotFoundException, NoSuchMethodException {
+
+ try {
+ Class<?> clazz = getClass(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 (ClassNotFoundException e) {
+ throw e;
+ } catch (NoSuchMethodException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new NoSuchMethodException("Cannot instantiate: " + type);
+ }
+ }
+
+ /**
+ * Insert a custom serialiser that will take precedence over the default one
+ * or the target class.
+ *
+ * @param serializer
+ * the custom serialiser
+ */