java 查找序列化对象的 serialVersionUID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1321988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding serialVersionUID of serialized object
提问by paweloque
Is there a way to determine the generated serialVersionUIDof a serialized Java object?
有没有办法确定serialVersionUID序列化Java对象的生成?
The problem is that I serialized an object without explicitely specifying the serialVersionUID. Now the deserialization process complains about class incompatibilities. However I didn't change the class in a way which would make it incompatible. So I assume that it is enough to specify the serialVersionUIDin the class as it is stored in the object data. In order to do this I need to read the serialVersionUIDfrom the serialized data.
问题是我序列化了一个对象而没有明确指定serialVersionUID. 现在反序列化过程抱怨类不兼容。但是,我没有以使其不兼容的方式更改类。所以我假设serialVersionUID在类中指定 就足够了,因为它存储在对象数据中。为了做到这一点,我需要serialVersionUID从序列化数据中读取。
采纳答案by McDowell
You can do this by extending ObjectInputStream:
您可以通过扩展ObjectInputStream来做到这一点:
public class PrintUIDs extends ObjectInputStream {
public PrintUIDs(InputStream in) throws IOException {
super(in);
}
@Override
protected ObjectStreamClass readClassDescriptor() throws IOException,
ClassNotFoundException {
ObjectStreamClass descriptor = super.readClassDescriptor();
System.out.println("name=" + descriptor.getName());
System.out.println("serialVersionUID=" + descriptor.getSerialVersionUID());
return descriptor;
}
public static void main(String[] args) throws IOException,
ClassNotFoundException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
List<Object> list = Arrays.asList((Object) new Date(), UUID.randomUUID());
oos.writeObject(list);
oos.close();
InputStream in = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new PrintUIDs(in);
ois.readObject();
}
}
I believe it would be possible to read all the serialized data by replacing the descriptor returned by the method, but I haven't tried it.
我相信可以通过替换方法返回的描述符来读取所有序列化数据,但我还没有尝试过。
回答by Keshav
There's an easy way to find out serialversionUID of a class-
有一种简单的方法可以找出类的 serialversionUID-
Suppose you have class in which you have forgotten to mention serialversionUID-
假设您有一个忘记提及 serialversionUID-的类
import java.io.Serializable;
public class TestSerializable implements Serializable {
}
Just do this-
就这样做-
serialver -classpath . TestSerializable
This prints-
这打印-
static final long serialVersionUID = 5832063776451490808L;
serialver is a utility that comes along with JDK
serialver 是 JDK 附带的实用程序
回答by Martins
This works for me:
这对我有用:
long serialVersionUID = ObjectStreamClass.lookup(YourObject.getClass()).getSerialVersionUID();
Hope it helps you too.
希望它也能帮助你。
回答by McDowell
There is metadata associated with the serialized bits (a header if you like). You can read the value from the metadata if you know at which position it is (the SerialVersionUIDis written there along with other info such as the class name).
有与序列化位相关联的元数据(如果您愿意,可以使用标头)。如果您知道它在哪个位置,则可以从元数据中读取该值(该SerialVersionUID值与其他信息如类名一起写入)。
I think this article might help you: The Java serialization algorithm revealed.
我认为这篇文章可能对您有所帮助:Java 序列化算法揭示了.
Note that the bits are written "in clear" (unless you encrypted the stream explicitly) so a HEX editor might be all you need to see what is the SerialVersionUID.
请注意,这些位是“明文”写入的(除非您明确加密了流),因此您可能只需要一个十六进制编辑器来查看SerialVersionUID.
回答by shanwu
you can also enable serialization inspection from Settings -> Editor -> Inspections -> enable "Serializable class without 'serialVersionUID' check" first, then ALT+ ENTER, the studio will create serialVersionUID for you.
您还可以从 Settings -> Editor -> Inspections -> 启用“Serializable class without 'serialVersionUID' check”,然后按 ALT+ ENTER,工作室将为您创建 serialVersionUID。
回答by WMR
There is a specified grammar for the serialization of objects:
对象的序列化有一个指定的语法:
See chapter 6.4 in http://java.sun.com/javase/6/docs/platform/serialization/spec/serialTOC.html
请参阅http://java.sun.com/javase/6/docs/platform/serialization/spec/serialTOC.html 中的第 6.4 章
Using this, you should be able to determine the SerialVersionUID of your serialized object.
使用它,您应该能够确定序列化对象的 SerialVersionUID。
回答by FindOut_Quran
The easiest way to get it (especially on hard to find cases) is to define any number for serialVersionUID and monitor the stacktrace when the deserialization fails, it will print out the original one.
获得它的最简单方法(尤其是在难以找到的情况下)是为 serialVersionUID 定义任意数字并在反序列化失败时监视堆栈跟踪,它将打印出原始的。
回答by psychollek
there is much simpler way to get the serialVersionUID of serialized object - just deserialize it with apache commons serialization utils to same class with different serial version uid and read the exception message which will be more or less:
有更简单的方法来获取序列化对象的 serialVersionUID - 只需使用 apache commons 序列化工具将其反序列化为具有不同序列版本 uid 的同一个类,并读取或多或少的异常消息:
org.apache.commons.lang.SerializationException: java.io.InvalidClassException: my.example.SerializableClass; local class incompatible: stream classdesc serialVersionUID = -1, local class serialVersionUID = -2
org.apache.commons.lang.SerializationException: java.io.InvalidClassException: my.example.SerializableClass; 本地类不兼容:流 classdesc serialVersionUID = -1,本地类 serialVersionUID = -2
回答by Nick Holt
That's exactly what you should do - specify your own static final long serialVersionUID.
这正是您应该做的 - 指定您自己的static final long serialVersionUID.
There's a section about it in the docs for Serializable.
在Serializable的文档中有一个关于它的部分。
Unless you've specified a serialVersionUIDI don't believe there's an easy way to get it other than deciphering the stream as @WMR suggests.
除非您指定了 aserialVersionUID我不相信除了像@WMR 建议的那样解密流之外,还有其他简单的方法可以获得它。


