简体   繁体   English

如何更改一堆java可序列化类的包

[英]How can I change package for a bunch of java serializable classes

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. 一个很好的eclipse redactor很好,但我的一些类是Serializables,我需要支持跨版本兼容性。

Any Idea on how it can be done? 关于如何做到的任何想法? Can it be done automatically/semi-automatically? 可以自动/半自动完成吗?

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: 如果类结构保持不变,则可以readClassDescriptor()自定义ObjectInputStream,在其构造函数中调用enableResolveObject(true) ,然后使用以下内容覆盖readClassDescriptor()

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;
}

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. 如果您仍然对应用程序的先前版本进行更新,则可以创建强制更新,这将更改序列化数据的方式,并且不使用默认的Java序列化。


On the same topic : 在同一主题上:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM