如何更改一堆 Java 可序列化类的包 [英] How can I change package for a bunch of java serializable classes
问题描述
我想在我的应用程序中更改多个类的包.一个不错的 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屋!