views:

44

answers:

2

I want to change the packages of multiple classes in my application. A nice eclipse redactor good have been great but some of my classes are Serializables and I need to support cross version compatibility.

Any Idea on how it can be done? Can it be done automatically/semi-automatically?

A: 

You simply can't change neither the name of your class nor your package without breaking the compatibility with the Serialization.

You'll have to find a way to keep your classes in the same package or you have to break the retro-compatibility.

If you still do updates on previous versions of your application, you can create a mandatory update which will change the way you serialize your data and go without the default java serialization.


On the same topic :

Colin Hebert
Yes - run with both versions of the classes for a while, and have the application convert old versions into new versions when it starts up. Once this has gone on sufficiently, change the code over.
Erick Robertson
A: 

If the class structure remains the same, you can subclass a custom ObjectInputStream, call enableResolveObject(true) in its constructor and then override readClassDescriptor() with something like that:

protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException {
    ObjectStreamClass read = super.readClassDescriptor();
    if (read.getName().startsWith("com.old.package.")) {
        return ObjectStreamClass.lookup(Class.forName(read.getName().replace("com.old.package.", "org.new.package.")));
    }
    return read;
}
vanto
10x, Where can I read more on what you are saying here?
ekeren
http://download.oracle.com/javase/6/docs/platform/serialization/spec/serialTOC.html - it reads a bit cryptic but is IMO the first class reference. Bloch's Effective Java has also some hints, but does not go that much into the details of ObjectInputStream. While looking at the code snippet I believe that calling `enableResolveObject(true)` is not necessary in this case.
vanto