如何在运行时从类序列化中排除字段?
2022-09-01 06:39:56
如何在运行时从序列化进程中排除类字段?编译时间有瞬态修饰符,但是运行时呢?我的意思是使用ObjectOutputStream进行常见的Java序列化,而不是gson或其他东西。
对不起,我想我没有解释正确。这并不完全是关于序列化,而是关于反序列化。我有一批遗留文件,并像这样处理它们:
public class Deserialize {
/**
* @param args
* @throws IOException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws ClassNotFoundException, IOException {
File file = new File("/home/developer/workspace/DDFS/some.ddf");
HackedObjectInputStream in = new HackedObjectInputStream(new GZIPInputStream(new FileInputStream(file)));
System.out.println("Attempt to open " + file.getAbsolutePath());
Object obj = in.readObject();
in.close();
}
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("DBOBExit", Exit.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))
{
resultClassDescriptor = ObjectStreamClass.lookup(MIGRATION_MAP.get(oldName));
}
}
return resultClassDescriptor;
}
}
}
此代码适用于大多数文件,但某些文件会抛出
Exception in thread "main" java.lang.ClassCastException: cannot assign instance of java.awt.Polygon to field Exit.msgbackPt of type java.awt.Point in instance of Exit
at java.io.ObjectStreamClass$FieldReflector.setObjFieldValues(ObjectStreamClass.java:2053)
由于不同版本的 Exit 类 。新版本有新字段。当我向新字段添加瞬态时,错误消失,但另一个文件开始引发异常(最新文件)。
那么,如果我检测到遗留的序列化文件,是否可以在运行时向这些新文件添加瞬态?也许是反思什么的?