How to generate serialVersionUID programmatically in Java?

I am developing a project to generate java files I want to be able to add serialVersionUID just like using the serialver tool

Is there any way to do this when generating java code, or do you need to ask the user of the tool to provide the uid manually? For clarity, I'm not going to do this automatically through eclipse or serialver tools, but through Java itself

Solution

There is a version of the serialver tool source available from openjdk It all boils down to this call:

ObjectStreamClass c = ObjectStreamClass.lookup(MyClass.class);
long serialID = c.getSerialVersionUID();
System.out.println(serialID);

In JDK 6, at least it uses the serialver tool to return the same number

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>