如何在IntelliJ IDEA中生成这个值?
我去设置->错误-> Serialization问题-> Serializable类没有' serialVersionUID ',但它仍然没有显示警告。我的类PKladrBuilding父类实现了接口Serializable。
部分代码:
public class PKladrBuilding extends PRQObject
public abstract class PRQObject extends PObject
public abstract class PObject implements Serializable
在序列化上花了一些时间后,我发现,我们不应该用一些随机值生成serialVersionUID,我们应该给它一个有意义的值。
这里有一个详细的评论。我正在处理这里的评论。
Actually, you should not be "generating" serial version UIDs. It is a dumb "feature" that stems from the general misunderstanding of how that ID is used by Java. You should be giving these IDs meaningful, readable values, e.g. starting with 1L, and incrementing them each time you think the new version of the class should render all previous versions (that might be previously serialized) obsolete. All utilities that generate such IDs basically do what the JVM does when the ID is not defined: they generate the value based on the content of the class file, hence coming up with unreadable meaningless long integers. If you want each and every version of your class to be distinct (in the eyes of the JVM) then you should not even specify the serialVersionUID value isnce the JVM will produce one on the fly, and the value of each version of your class will be unique. The purpose of defining that value explicitly is to tell the serialization mechanism to treat different versions of the class that have the same SVUID as if they are the same, e.g. not to reject the older serialized versions. So, if you define the ID and never change it (and I assume that's what you do since you rely on the auto-generation, and you probably never re-generate your IDs) you are ensuring that all - even absolutely different - versions of your class will be considered the same by the serialization mechanism. Is that what you want? If not, and if you indeed want to have control over how your objects are recognized, you should be using simple values that you yourself can understand and easily update when you decide that the class has changed significantly. Having a 23-digit value does not help at all.
希望这能有所帮助。祝你好运。
在另一组中添加一个名为“ser”的活动模板,设置为“applied in Java: declaration”,并取消勾选“Shorten FQ names”。给它一个模板文本:
$serial$
现在编辑变量并设置serial为:
groovyScript("(System.env.JDK_HOME+'/bin/serialver -classpath '+com.intellij.openapi.fileEditor.FileDocumentManager.instance.getFile(_editor.document).path.replaceAll('/java/.*','').replaceAll('/src/','/build/classes/')+' '+_1).execute().text.replaceAll('.*: *','')",qualifiedClassName())
它采用标准的Gradle项目布局。为Maven更改/build/到/target/。