diff options
-rw-r--r-- | src/com/vaadin/data/util/MethodProperty.java | 25 | ||||
-rw-r--r-- | src/com/vaadin/util/SerializerHelper.java | 78 |
2 files changed, 94 insertions, 9 deletions
diff --git a/src/com/vaadin/data/util/MethodProperty.java b/src/com/vaadin/data/util/MethodProperty.java index 87915bda88..5b15b66cd5 100644 --- a/src/com/vaadin/data/util/MethodProperty.java +++ b/src/com/vaadin/data/util/MethodProperty.java @@ -11,6 +11,7 @@ import java.lang.reflect.Method; import java.util.LinkedList; import com.vaadin.data.Property; +import com.vaadin.util.SerializerHelper; /** * <p> @@ -78,7 +79,7 @@ public class MethodProperty implements Property, Property.ValueChangeNotifier, /** * Type of the property. */ - private Class type; + private transient Class<?> type; /** * List of listeners who are interested in the read-only status changes of @@ -95,20 +96,25 @@ public class MethodProperty implements Property, Property.ValueChangeNotifier, /* Special serialization to handle method references */ private void writeObject(java.io.ObjectOutputStream out) throws IOException { out.defaultWriteObject(); + SerializerHelper.writeClass(out, type); out.writeObject(instance); out.writeObject(setArgs); out.writeObject(getArgs); if (setMethod != null) { out.writeObject(setMethod.getName()); - out.writeObject(setMethod.getParameterTypes()); + SerializerHelper + .writeClassArray(out, setMethod.getParameterTypes()); } else { - out.writeObject(""); + out.writeObject(null); + out.writeObject(null); } if (getMethod != null) { out.writeObject(getMethod.getName()); - out.writeObject(getMethod.getParameterTypes()); + SerializerHelper + .writeClassArray(out, getMethod.getParameterTypes()); } else { - out.writeObject(""); + out.writeObject(null); + out.writeObject(null); } }; @@ -117,20 +123,21 @@ public class MethodProperty implements Property, Property.ValueChangeNotifier, ClassNotFoundException { in.defaultReadObject(); try { + type = SerializerHelper.readClass(in); instance = in.readObject(); setArgs = (Object[]) in.readObject(); getArgs = (Object[]) in.readObject(); String name = (String) in.readObject(); - if (name != null && !name.equals("")) { - Class<?>[] paramTypes = (Class<?>[]) in.readObject(); + Class<?>[] paramTypes = SerializerHelper.readClassArray(in); + if (name != null) { setMethod = instance.getClass().getMethod(name, paramTypes); } else { setMethod = null; } name = (String) in.readObject(); - if (name != null && !name.equals("")) { - Class<?>[] paramTypes = (Class<?>[]) in.readObject(); + paramTypes = SerializerHelper.readClassArray(in); + if (name != null) { getMethod = instance.getClass().getMethod(name, paramTypes); } else { getMethod = null; diff --git a/src/com/vaadin/util/SerializerHelper.java b/src/com/vaadin/util/SerializerHelper.java new file mode 100644 index 0000000000..fe9eb28918 --- /dev/null +++ b/src/com/vaadin/util/SerializerHelper.java @@ -0,0 +1,78 @@ +package com.vaadin.util; + +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +/** + * Helper class for performing serialization. Most of the methods are here are + * workarounds for problems in Google App Engine. + * + */ +public class SerializerHelper { + + public static void writeClass(ObjectOutputStream out, Class<?> cls) + throws IOException { + if (cls == null) { + out.writeObject(null); + } else { + out.writeObject(cls.getName()); + } + + } + + public static void writeClassArray(ObjectOutputStream out, + Class<?>[] classes) throws IOException { + if (classes == null) { + out.writeObject(null); + } else { + String[] classNames = new String[classes.length]; + for (int i = 0; i < classes.length; i++) { + classNames[i] = classes[i].getName(); + } + out.writeObject(classNames); + } + } + + public static Class<?>[] readClassArray(ObjectInputStream in) + throws ClassNotFoundException, IOException { + String[] classNames = (String[]) in.readObject(); + if (classNames == null) { + return null; + } + Class<?>[] classes = new Class<?>[classNames.length]; + for (int i = 0; i < classNames.length; i++) { + classes[i] = resolveClass(classNames[i]); + } + + return classes; + } + + private static Class<?>[] primitiveClasses = new Class<?>[] { byte.class, + short.class, int.class, long.class, float.class, double.class, + boolean.class, char.class }; + + public static Class<?> resolveClass(String className) + throws ClassNotFoundException { + for (Class<?> c : primitiveClasses) { + if (className.equals(c.getName())) { + return c; + } + } + + return Class.forName(className); + } + + public static Class<?> readClass(ObjectInputStream in) throws IOException, + ClassNotFoundException { + String className = (String) in.readObject(); + if (className == null) { + return null; + } else { + return resolveClass(className); + + } + + } + +} |