Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9215639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:14:57+00:00 2026-06-18T02:14:57+00:00

I am investigating different options in the Java Serialization mechanism to allow flexibility in

  • 0

I am investigating different options in the Java Serialization mechanism to allow flexibility in our class structures for version-tolerant storage (and advocating for a different mechanism, you don’t need to tell me).

For instance, the default serialization mechanism can handle both adding and removing fields, if only backwards compatibility is required.

Renaming a class or moving it to a different package has proved to be much harder, though. I found in this question that I was able to do a simple class rename and/or move package, by subclassing ObjectInputStream and overriding readClassDescriptor():

    if (resultClassDescriptor.getName().equals("package.OldClass"))
        resultClassDescriptor = ObjectStreamClass.lookup(newpackage.NewClass.class);

That is fine for simple renames. But if you then try to add or delete a field, you get a java.io.StreamCorruptedException. Worse, this happens even if a field has been added or deleted, and then you rename the class, which could cause problems with multiple developers or multiple checkins.

Based on some reading I had done, I experimented a bit with also overriding resolveClass(), with the idea that we were correctly repointing the name to the new class, but not loading the old class itself and bombing on the field changes. But this comes from a very vague understanding of some the details of the Serialization mechanism, and I’m not sure if I’m even barking up the right tree.

So 2 precise questions:

  1. Why is repointing the class name using readClassDescriptor() causing
    deserialization to fail on normal, compatible class changes?
  2. Is there a way using resolveClass() or another mechanism to get around
    this and allow classes to both evolve (add and remove fields) and be
    renamed/repackaged?

I poked around and could not find an equivalent question on SO. By all means, point me to such a question if it exists, but please read the question carefully enough that you do not close me unless another question actually answers my precise question.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:14:58+00:00Added an answer on June 18, 2026 at 2:14 am

    I had same problems with flexibility like you and I found the way.
    So here my version of readClassDescriptor()

        static class HackedObjectInputStream extends ObjectInputStream
    {
    
        /**
         * Migration table. Holds old to new classes representation.
         */
        private static final Map<String, Class<?>> MIGRATION_MAP = new HashMap<String, Class<?>>();
    
        static
        {
            MIGRATION_MAP.put("DBOBHandler", com.foo.valueobjects.BoardHandler.class);
            MIGRATION_MAP.put("DBEndHandler", com.foo.valueobjects.EndHandler.class);
            MIGRATION_MAP.put("DBStartHandler", com.foo.valueobjects.StartHandler.class);
        }
    
        /**
         * Constructor.
         * @param stream input stream
         * @throws IOException if io error
         */
        public HackedObjectInputStream(final InputStream stream) throws IOException
        {
            super(stream);
        }
    
        @Override
        protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException
        {
            ObjectStreamClass resultClassDescriptor = super.readClassDescriptor();
    
            for (final String oldName : MIGRATION_MAP.keySet())
            {
                if (resultClassDescriptor.getName().equals(oldName))
                {
                    String replacement = MIGRATION_MAP.get(oldName).getName();
    
                    try
                    {
                        Field f = resultClassDescriptor.getClass().getDeclaredField("name");
                        f.setAccessible(true);
                        f.set(resultClassDescriptor, replacement);
                    }
                    catch (Exception e)
                    {
                        LOGGER.severe("Error while replacing class name." + e.getMessage());
                    }
    
                }
            }
    
            return resultClassDescriptor;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am investigating different options for storing data for my Azure cloud app. I
We are currently investigating different remote-desktop support solutions to help our clients if they
I'm investigating if it is possible to send three different Ax 2009 reports to
I'm currently investigating my options to run automated tests from within Visual Studio 2008
I have been investigating the best way to store our solutions in SVN and
I am currently in the process of investigating a very peculiar problem on our
I'm investigating different optimization techniques, and I came across this post Analyzing Code for
I am investigating different versioning systems for my company and am currently looking in
I'm investigating the possibility of putting together a custom storage scheme for my application.
I'm investigating the different types of NoSQL database types and I'm trying to wrap

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.