Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

serialVersionUID - can it be ignored?

Thread view: 
Mark Rafn - 01 Apr 2007 08:51 GMT
Somehow I managed not to specify serialVersionUID in some Serializable
classes, and now have saved data with multiple logically-compatible but
serialVersionUID-incompatible formats.

I have since changed the code to specify a serialVersionUID, which will
prevent this in the future.

Is there any way to write code to read the multiple old versions, which
differ ONLY in serialVersionUID?  Basically, I'd really love a subclass of
ObjectInputStream that just doesn't look at this field in objects it reads.

Looking at the ObjectInputStream javadoc, it appears I can subclass it and
write my own readObjectOverride, but it would appear to be quite a bit of
implementation to make it work.

Anyone know an easier way, or have any gotchas if I do it myself?
--
Mark Rafn    dagon@dagon.net    <http://www.dagon.net/>
Tom Hawtin - 01 Apr 2007 13:25 GMT
> Is there any way to write code to read the multiple old versions, which
> differ ONLY in serialVersionUID?  Basically, I'd really love a subclass of
> ObjectInputStream that just doesn't look at this field in objects it reads.

The quickest hack would be to do a search and replace on the data.

I guess you can probably override ObjectStreamClass.readClassDescriptor.
If you call the super on that method, then it will call
ObjectStreamClass.readUTF for the class name, followed by
ObjectStreamClass.readLong. That does rely on implementation specific
details. You could read the data yourself (format is in the specs) and
just return a value from ObjectStreamClass.lookup, or from another
ObjectStreamClass.

Another alternative would be to use ClassLoaders to have two copies of
the class involved. In this case any instance of one class would not be
an instance of the other, so you would have to copy the data across
(probably via an intermediary, as no class can link to both of the
serialisable classes).

Tom Hawtin
Mark Rafn - 02 Apr 2007 00:30 GMT
>> Is there any way to write code to read the multiple old versions, which
>> differ ONLY in serialVersionUID?  Basically, I'd really love a subclass of
>> ObjectInputStream that just doesn't look at this field in objects it reads.

>The quickest hack would be to do a search and replace on the data.

I was afraid someone would suggest that.  I guess I could subclass
ObjectInputStream so that it uses a private FilterInputStream which does
nothing but fixup serialVersionUID in the bytestream.  Ick.

>I guess you can probably override ObjectStreamClass.readClassDescriptor.

That's promising.  I'll see how that goes.

>Another alternative would be to use ClassLoaders to have two copies of
>the class involved.

No good for me.  Once in memory, I'm going to want to pass these around
normally, and having multiple distinct same-named classes gets very hairy.

Thanks for the ideas!
--
Mark Rafn    dagon@dagon.net    <http://www.dagon.net/>


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.