I'm mantaining some application and I have a class marked as Serializable but I suspect that is not being serialized anywhere so the Serializable mark is not needed.
What is the best way to be sure of that? It's possible to determine this statically?
Thanks
Not directly. Generally, serialization happens when the object is being written to ObjectOutputStream, but this most often happen outside of your code (for example in libraries you are using, or in your container). So you have two options:
dynamically - define this method on your class. Thus you will eventually know if the class is being serialized, without breaking any functionality. But you'd need the system to be running:
private void writeObject(java.io.ObjectOutputStream out) {
out.defaultWriteObject();
log.info("Object of type " +
getClass().getName() + " is being serialized");
// optionally include a stacktrace here, or use a debugger, to see
// when exactly it happened
}
try to understand where is your object going. If it transferred over a network, or stored in a temporary storage (session, cache), then it should be Serializable. Otherwise, most probably, not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With