• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

how serialization methods definition is picked up?  RSS feed

 
Ranch Hand
Posts: 370
2
Java
  • 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
Posts: 38906
684
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: 370
2
Java
  • 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
Posts: 38906
684
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: 370
2
Java
  • 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.
 
Bartender
Posts: 9486
184
  • 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: 61690
192
  • 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: 370
2
Java
  • 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: 61690
192
  • 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: 19953
94
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!