views:

132

answers:

5

Just like the title says, is there a way to check if an object is serializable, and if not, make it so at run time?

+1  A: 

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://xstream.codehaus.org/tutorial.html .

whaley
Proxies are made by interface, not on a class.
Bozho
@Bozho - that part is detailed in the documentation I linked to. It would require some changes in his codebase if he isn't already using Interface to polymorphically(sp?) reference his objects.
whaley
His original object would be a member of the InvocationHandler, which is also serialized, which will fail.
Bozho
@Bozho - you are right, it would fail. He could make the original object a transient field... but in terms of Serialization that doesn't do much good :D (barring some fancy recreation of the object state in the proxy object itself + plus making the original object transient, which is well beyond fugly). In general, using Proxy objects is how one would dynamically add interfaces to an object, but in the case of Serializable this wouldn't do any good. I'll modify my answer as such.
whaley
+7  A: 

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.
Bozho
+1 for including appropriate warnings.
DJClayworth
A: 

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){
}
jancrot
A: 

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.

StaxMan
The reason I'm looking at serialization is for a project I would like to convert an object to a string (through a byte array) for storage and transmission where it is required to be a String due to external factors. I've seen several examples of how to use serialization to convert to a byte stream to capture the object as a String in the end, but nothing on what to do if the object is not serializable. I understand the need for certain objects to not be serialized, but in this case it would be something left to the discretion of the developer.
Chad Cook
Ok, then you definitely do NOT want to use JDK Serializable. I would recommend using JSON serialization (like Jackson, see http://wiki.fasterxml.com/JacksonInFiveMinutes). Result is more readable and much less fragile. Main issue with Serializable is that versioning is very difficult, so it is an anti-pattern to persist results (read Josh Bloch's explanation, for example). It's not readable, either (unlike xml or json). There is nothing wrong in serializing things per se, but Serializable is seldom right way.
StaxMan
A: 

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?

Jay