开发者

Can you make an object serializable at runtime?

开发者 https://www.devze.com 2023-01-15 08:08 出处:网络
Just like t开发者_JAVA技巧he title says, is there a way to check if an object is serializable, and if not, make it so at run time?Short answer - no.

Just like t开发者_JAVA技巧he title says, is there a way to check if an object is serializable, and if not, make it so at run time?


Short answer - no.

Longer answer - yes, using byte-code manipulation, for example with asm. But you should really consider whether this is needed. Serialization is a serious matter (Effective Java has a whole chapter on serialization)

Btw, there are alternatives to binary serialization, that do not require the object implementing Serializble (as pointed by Jacob in the comments):

  • XML - java.beans.XMLEncoder.encode(..) is the xml version of ObjectOutputStream
  • JSON - frameworks like Jacskon, Gson let you serialize an object with one line.


If somebody really really really need manipulate with bytecode in runtime. With library Javassist can be done this:

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


As others have said... Short Answer is No.

You can absolutely add an interface to any old object at runtime by using Proxy objects as described at http://download.oracle.com/javase/6/docs/technotes/guides/reflection/proxy.html . This includes java.io.Serializable too. However, in order for a proxy object to be useful, it must maintain an internal reference to the original object, which in your case does not implement Serializable. The only way your proxy object could be serialized is by making the internal reference to the original object a transient field and that wouldn't do you much good at all.

Further, after examining your comments, it looks like java serialization is definitely not what you want and you really just want to serialize to some string format. Others have suggested various solutions, but IMO if you want the least amount of fuss, go with XStream: http://x-stream.github.io/tutorial.html .


Whoa. Why would you need it to be checked at runtime? Isn't it easier to ensure it at compile time? Using a code just like this.

public void someMethod(Serializable serializable){
}

Or you can make it more complex.

public interface SerializableInterface implements Serializable{
// bla-bla
}

public void someMethod(SerializableInterface serializable){
}


Aside from existing good answers, another question is whether you specifically need Java Serializable for specific framework, or just need to be able to serializer objects. For latter there are many many more choices than JDK default one; and for many use cases alternatives are much better than JDK one.

If you don't specifically need JDK one, using JSON or XML based serialization is often a good choice.


Checking at run time? Sure. "if (someObject instanceof Serializable) ...".

You want to change the definition of an object at run time? There is very rarely a good reason to want to do this. Maybe there's a way to do this with reflection, but I've never had a reason to want to do such a thing. What are you trying to accomplish?


You could create a Serizlize wrapper class with an object field, then assign your object to the field. When you serialize the wrapper class, it will store the object, and you can deserialize and cast your object from the field.

public class SerialWrap implements Serializable{
public Object obj;
}

HashMap<String, String> hm = new HashMap<>();
    hm.put("great preacher", "Apostle Winston George Baker King Jesus Pentecostal Fellowship Negril, Jamaica");
   hm.put("Miracles","Sick healed, cancer healed,God is moving");
    SerialWrap wrap = new SerialWrap();
    wrap.obj = hm;

ObjectOutputStream.write(wrap)

SerialWrap wrap2 =(SerialWrap)ObjectInputStream.readObject();
hm =(HashMap<String, String>) wrap2.obj;
0

精彩评论

暂无评论...
验证码 换一张
取 消