Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find serialization usage

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

like image 625
gerferra Avatar asked Jun 17 '26 10:06

gerferra


1 Answers

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.

like image 140
Bozho Avatar answered Jun 19 '26 00:06

Bozho