In java, is it possible to add a Serializable interface to a class that does not have it at runtime?

There is a class that I want to serialize, and it implements Serializable, but one of the objects that it contains does not implement Serializable.

Is there a way to change the class at runtime so that it implements the Serializable interface so that I can serialize it? I cannot change it at compile time because its a third-party library.

Perhaps I would have to use some kind of script for bytecode or something else?

EDIT: both the containing class and the containing class are in a third-party library, so I don’t think I can mention something temporary. The containing class is marked as serializable, but contains an object that is not.

I'm fine with writing a custom serialization method for a class, not sure how I would do this, but should I use reflection to get the values ​​of private variables?

+6
java serialization
source share
5 answers

Reading javadoc for Serializable, I see:

Classes requiring special handling during serialization and the deserialization process must special methods with these exact Signatures:

private void writeObject(java.io.ObjectOutputStream out) throws IOException private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException; private void readObjectNoData() throws ObjectStreamException; 

which you can use to manually serialize non-auto fields. You can study the use of ASM, but it seems hard to believe that this is a supported solution.

+5
source share

Fields can be skipped using the transient modifier. Additional data can be added to the stream using the readObject and writeObject . Classes can be subclassed to make them sequential (although you will need to manage superclass data) or use a serial proxy (see "Effective Java").

+3
source share

First of all, do you really need to do this? Perhaps a class in a third-party library is not design serializable. Even if this is just an omission and not a deliberate decision, it's easy enough to write your own serialization method in a helper class. If you need to do this though (like me, for something like that). Take a look at the Javassist . You can do something like this:

 ClassPool pool = ClassPool.getDefault(); CtClass cc = pool.get("mypackage.MyClass"); cc.addInterface(pool.get("java.io.Serializable")) 

EDIT: You can use a third-party Serialization API like XStream , instead of doing all the dirty work yourself.

+1
source share

If the class is not marked as Serializable, which may have good reasons (take the database connection as an example). Therefore, first of all, make your clear if you really need to serialize this object. If you just need to serialize the surrounding object, you can consider labeling the contained object as transient.

0
source share

You can use java.lang.reflect.Proxy to add a Serializable interface to instances of objects over which you do not have lifecycle management (i.e. do not sub-calibrate them) and get them serialized this way. But then you still can’t read them without another hack (i.e., introducing a surrogate class).

I would recommend looking for an alternative to the Java Serializable engine in general. This is largely disrupted by design in several ways.

0
source share

All Articles