如何更改一堆 Java 可序列化类的包 [英] How can I change package for a bunch of java serializable classes

查看:41
本文介绍了如何更改一堆 Java 可序列化类的包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中更改多个类的包.一个不错的 eclipse redactor 很好,但我的一些类是可序列化的,我需要支持跨版本兼容性.

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?

推荐答案

如果类结构保持不变,你可以子类化一个自定义的 ObjectInputStream,在其构造函数中调用 enableResolveObject(true) 然后覆盖readClassDescriptor() 类似这样:

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

这篇关于如何更改一堆 Java 可序列化类的包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆