问题描述
我正在开发一个生成 Java 文件的项目.我希望能够像使用 工具.
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.
当我生成 Java 代码时,有没有办法做到这一点,还是需要让工具的用户手动提供 UID?需要明确的是,我不希望通过 Eclipse 或 serialver
工具自动执行此操作,而是希望通过 Java 本身来执行此操作.
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.
推荐答案
有一个版本的 serialver
工具源可用 来自 OpenJDK.这一切都归结为这个电话:
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);
至少在 JDK 6 中它返回与 serialver
工具相同的数字.
In JDK 6 at least it returns the same number with serialver
tool.
这篇关于如何在 Java 中以编程方式生成 serialVersionUID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!