Java Serializing Methods
It is possible to serialize a method? Because when I look at the documentation the Method class do开发者_Python百科es not implements Serializable. So are there any alternatives?
Since Method does not implement Serializable, it cannot be serialized using the standard Java Serialization API.
A workaround would be to manually serialize just the name of the class and method and its parameter types. You can then recreate the Method instance during deserialization.
It depends what do you mean. Really, as guys already mentioned method cannot be serialized. But if you wish to serialize logic that method implement and (for example) send it over network it is possible. This serialization can be done by serializing of class itself. Actually .class is already a sequence of bytes that can be stored (and and typically stored) in files. You can just send it over network if you want and then instantiate by using Class.forName(). Moreover you can do better: use HttpClassLoader, pass to it the HTTP URL where your class is available, create class instance using this class loader and run any method you want.
you can use the following wrapper to allow method serialiation:
public class SerializableMethod implements Serializable
{
private static final long serialVersionUID = 6631604036553063657L;
private Method method;
public SerializableMethod(Method method)
{
this.method = method;
}
public Method getMethod()
{
return method;
}
private void writeObject(ObjectOutputStream out) throws IOException
{
out.writeObject(method.getDeclaringClass());
out.writeUTF(method.getName());
out.writeObject(method.getParameterTypes());
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
{
Class<?> declaringClass = (Class<?>)in.readObject();
String methodName = in.readUTF();
Class<?>[] parameterTypes = (Class<?>[])in.readObject();
try
{
method = declaringClass.getMethod(methodName, parameterTypes);
}
catch (Exception e)
{
throw new IOException(String.format("Error occurred resolving deserialized method '%s.%s'", declaringClass.getSimpleName(), methodName), e);
}
}
}
If you just wonder, is it possible, than no: it doesn't implement Serializable
. Also, Method
is a final class, so you can't subclass it in order to add some functionality.
Actually, all methods are part of their class, and Class
does implement Serializable. Since you'll need an object of the class anyway to call the method, you could just send the class (or the object).
On the receiving side, though, usually there would be created an object of the version of the class there.
If you want to use this to invoke your method at a remote side, take a look at RMI (package java.rmi.*
) - it supports also sending of objects when the remote side does not (yet) have the class data.
Python does allow pickling of top level methods, in Java the way to call methods on another host is via some sort of RMI mechanisms. The class bytecode can be sent over the wire and given all dependencies are satisfied - use the URLClassloader to do this.
Thought Works library xStream does it very good. Here's a two minute tutorial.
精彩评论