Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 463
Giles Thompson Avatar asked Sep 20 '26 20:09

Giles Thompson


2 Answers

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

like image 154
Peter Lawrey Avatar answered Sep 23 '26 09:09

Peter Lawrey


No. You're confusing an instance of a class with the actual class itself. The class must be loaded before the serialized object is deserialized.

like image 37
David Grant Avatar answered Sep 23 '26 08:09

David Grant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!