Ok, this happened again... For some unknown reason the list strips my attachments if they are not with txt extension...
I'm using Outlook XP (and except for the crashing I haven't seen any problems with it)... Here is the attachment again.. -- dimiter > -----Original Message----- > From: Dimiter Dimitrov [mailto:[EMAIL PROTECTED]] > Sent: Tuesday, February 04, 2003 8:42 PM > To: 'Jakarta Commons Developers List' > Subject: [beanutils][lang] how to serialize/deserialize > Methods/PropertyDescriptors > > > Hi all, > > Currently I'm in process of replacing most of our util > classes with Jakarta-Commons. > > But I can't find a way to serialize Method instance. I have > looked in commons.lang and commons.beanutils packages. > > I'm attaching a possible solution. Don't know if it's not out > of scope though.. > > cheers, > Dimiter > >
import java.io.ObjectOutput; import java.io.IOException; import java.io.ObjectInput; import java.lang.reflect.Method; import java.beans.PropertyDescriptor; public class SerializationUtils { /** * @param method can be null * * @serialData declaringClassName:Object * @serialData methodName:UTF string * @serialData methodParameterTypes:Object[] */ public static void writeMethod(ObjectOutput out, Method method) throws IOException { if (method==null) { out.writeObject(null); return; } out.writeObject(method.getDeclaringClass().getName()); out.writeUTF(method.getName()); Class[] paramTypes = method.getParameterTypes(); String[] paramTypeNames = new String[paramTypes.length]; for (int i = 0; i < paramTypeNames.length; i++) { paramTypeNames[i] = paramTypes[i].getName(); } out.writeObject(paramTypeNames); } public static Method readMethod(ObjectInput in) throws IOException { try { final String className = (String) in.readObject(); if (className == null) return null; Class cls = Class.forName(className); final String methodName = in.readUTF(); final String[] paramTypeNames = (String[]) in.readObject(); final Class[] paramTypes = new Class[paramTypeNames.length]; for (int i = 0; i < paramTypeNames.length; i++) { paramTypes[i] = typeForName(paramTypeNames[i]); } return cls.getMethod(methodName, paramTypes); } catch (IOException e) { throw e; } catch (Exception e) { throw new IOException("Method deserialization failed: " + e); } } /** * @serialData propertyName:UTF string * @serialData getter:Method * @serialData setter:Method */ public static void writePropertyDescriptorMethods(ObjectOutput out, PropertyDescriptor pd) throws IOException { out.writeUTF(pd.getName()); writeMethod(out, pd.getReadMethod()); writeMethod(out, pd.getWriteMethod()); } public static PropertyDescriptor readPropertyDescriptorMethods(ObjectInput in) throws IOException { String propertyName = in.readUTF(); try { Method getter = readMethod(in); Method setter = readMethod(in); return new PropertyDescriptor(propertyName, getter, setter); } catch (IOException e) { throw e; } catch (Exception e) { throw new IOException("Property deserialization failed: "+e); } } /** * @serialData propertyName:UTF string * @serialData beanClass:Class */ public static void writePropertyDescriptorBean(ObjectOutput out, PropertyDescriptor pd, Class beanClass) throws IOException { out.writeUTF(pd.getName()); out.writeObject(beanClass); } public static PropertyDescriptor readPropertyDescriptorBean(ObjectInput in) throws IOException { try { String propertyName = in.readUTF(); Class beanClass = (Class) in.readObject(); return new PropertyDescriptor(propertyName, beanClass); } catch (IOException e) { throw e; } catch (Exception e) { throw new IOException("Property deserialization failed: " + e); } } public static Class primitive2refType(Class type) { if (type.equals(byte.class)) return Byte.class; if (type.equals(short.class)) return Short.class; if (type.equals(int.class)) return Integer.class; if (type.equals(long.class)) return Long.class; if (type.equals(char.class)) return Character.class; if (type.equals(float.class)) return Float.class; if (type.equals(double.class)) return Double.class; if (type.equals(boolean.class)) return Boolean.class; if (type.equals(void.class)) return Void.class; return type; } public static Class ref2primitiveType(Class type) { if (type.equals(Byte.class)) return byte.class; if (type.equals(Short.class)) return short.class; if (type.equals(Integer.class)) return int.class; if (type.equals(Long.class)) return long.class; if (type.equals(Character.class)) return char.class; if (type.equals(Float.class)) return float.class; if (type.equals(Double.class)) return double.class; if (type.equals(Boolean.class)) return boolean.class; if (type.equals(Void.class)) return void.class; return type; } public static Class typeForName(String type) throws ClassNotFoundException { if (type.equals("byte")) return byte.class; if (type.equals("short")) return short.class; if (type.equals("int")) return int.class; if (type.equals("long")) return long.class; if (type.equals("char")) return char.class; if (type.equals("float")) return float.class; if (type.equals("double")) return double.class; if (type.equals("boolean")) return boolean.class; if (type.equals("void")) return void.class; return Class.forName(type); } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]