• Post Reply Bookmark Topic Watch Topic
  • New Topic

how serialization methods definition is picked up?  RSS feed

 
Ranch Hand
Posts: 219
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that Serialization process is achieved by using Serializable interface which is a Marker Interface i.e. doesn't have any method defined in it. And, we can provide custom serialization by overriding readObject() and writeObject() in our Serializable implementing class. I am just wondering how these methods definition is picked up during serialization/deserialization process since default one is provided in ObjectInputStream and ObjectOutputStream classes.
 
author & internet detective
Marshal
Posts: 38144
617
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java has a concept called "reflection". This allows Java to look at what methods are defined. So Java can see if readObject/writeObject is implemented on the class.
 
Vaibhav Gargs
Ranch Hand
Posts: 219
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Jeanne!

Could you please point me to the code where this reflection code is written to check the existence of method in our custom classes.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 38144
617
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'd have to read the JDK code to find it. I didn't look; I'm assuming it is there.
 
Vaibhav Gargs
Ranch Hand
Posts: 219
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Jeanne, I checked it in JDK code but unfortunately didn't find any such coding over there.
 
Saloon Keeper
Posts: 8724
162
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To see what code is generated for your own serializable classes, you'll have to use a Java decompiler.

To see the reflective code that is used to read and write your serializable classes, check out the source files for the ObjectInputStream and ObjectOutputStream classes.
 
Marshal
Posts: 58823
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you mean you checked the Java® class source code. Jeanne meant to download the runtime's code which you can do by searching for openJDK. Somewhere inside that 100MB of C/C++ code you will find how objects are serialised. It may take a long time to find
 
Vaibhav Gargs
Ranch Hand
Posts: 219
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your response.

Below is the source code snippet of writeObject method but didn't find any reflection related code here:




Reference Link:
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/io/ObjectOutputStream.java#ObjectOutputStream.writeObjectOverride%28java.lang.Object%29


Could you please help me in understand how readObject/writeobject defined in our class is picked up during serialization process?
 
Campbell Ritchie
Marshal
Posts: 58823
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think that code is going to help you. As I said before, you need the runtime's code, not the Java® source.
 
Bartender
Posts: 19353
86
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The readObject and writeObject methods do not look for custom serialization methods, they are the custom serialization methods. Your class - or one of its parents - would implement these methods if you had custom serialization needs.

To determine if one of your classes implements one or more overrides to the default read/writeObject methods, you'd have to obtain a reference to that class (using class.forName or the getClass() method on an instance of the oject), then introspect for methods that match the read/writeObject method signatures. You can do that by brute force, but there's also a class helper library (whose name I forget) that's often used by heavily introspective systems. JavaServer Faces is one of them.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!