I am working on a project that generates Java files. I'd like to be able to optionally add the serialVersionUID
as you would with the serialver tool.
Is there a way to do this when I generate the Java code, or will I need to ask the user of the tool to provide UIDs manually? To be clear, I'm not looking to do this automatically through Eclipse or the serialver
tool, but to do it via Java itself.
Once you've enabled this plugin, you just need to press Alt+Insert / Command+n when you are in the body of a class implementing the Serializable interface. You should see an option to generate the serialVersionUID field for you.
If class does not contain a serialVersionUID field, its serialVersionUID will be automatically generated by the compiler. Different compilers, or different versions of the same compiler, will generate potentially different values.
The serialization at runtime associates with each serializable class a version number called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization.
There is a version of the serialver
tool source available from OpenJDK. It all comes down to this call:
ObjectStreamClass c = ObjectStreamClass.lookup(MyClass.class);
long serialID = c.getSerialVersionUID();
System.out.println(serialID);
In JDK 6 at least it returns the same number with serialver
tool.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With