Search code examples
javaserializationclassloader

Is Default Classloader Identity Serializable


Where a class has been loaded with a custom ClassLoader it does, of course, follow that a call to the resulting object's "getClassLoader()" method will return the custom ClassLoader by default.

My query is what happens if the same object is Serialized, sent over the network and then Deserialized; will a call to "getClassLoader()" on the Deserialised class still return the custom ClassLoader?


Solution

  • My query is what happens if the same object is Serialized, sent over the network and then Deserialized; will a call to "getClassLoader()" on the Deserialised class still return the custom ClassLoader?

    The class loader is not serialised. The getClassLoader() will give you the class loader the ObjectInputStream is using.


    From ObjectInputStream.resolveClass

    The default implementation of this method in ObjectInputStream returns the result of calling

     Class.forName(desc.getName(), false, loader)
    

    where loader is determined as follows: if there is a method on the current thread's stack whose declaring class was defined by a user-defined class loader (and was not a generated to implement reflective invocations), then loader is class loader corresponding to the closest such method to the currently executing frame; otherwise, loader is null