This week's book giveaway is in the Go forum.
We're giving away four copies of Head First Go and have Jay McGavren on-line!
See this thread for details.
Win a copy of Head First Go this week in the Go forum!
  • 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
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

how serialization methods definition is picked up?  RSS feed

 
Ranch Hand
Posts: 387
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: 39175
724
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: 387
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: 39175
724
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: 387
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.
 
Saloon Keeper
Posts: 9861
199
  • 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: 63478
207
  • 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: 387
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: 63478
207
  • 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.
 
Saloon Keeper
Posts: 20511
115
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.
 
A wop bop a lu bop a womp bam boom! Tiny ad:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!