How do generate this value in IntelliJ IDEA?
I go to Settings -> Errors -> Serialization issues -> Serializable class without ‘serialVersi
In addition you can add live template that will do the work.
To do it press Ctrl+Alt+S -> "Live Templates"
section -> other
(or w/e you wish)
And then create a new one with a definition like this:
private static final long serialVersionUID = 1L;
$END$
Then select definition
scope and save it as 'serial'
Now you can type serial
TAB in class body.
Add a live template called "ser" to the other group, set it to "Applicable in Java: declaration", and untick "Shorten FQ names". Give it a template text of just:
$serial$
Now edit variables and set serial to:
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())
It assumes the standard Gradle project layout. Change /build/ to /target/ for Maven.
I am not sure if you have an old version of IntelliJ IDEA, but if I go to menu File → Settings... → Inspections → Serialization issues → Serializable class without 'serialVersionUID'` enabled, the class you provide give me warnings.
If I try the first class I see:
BTW: It didn't show me a warning until I added { }
to the end of each class to fix the compile error.
With version v2018.2.1
Go to
Preference > Editor > Inspections > Java > Serialization issues > toggle "Serializable class without 'serialVersionUID'".
A warning should appear next to the class declaration.
After spending some time on Serialization, I find that, we should not generate serialVersionUID
with some random value, we should give it a meaningful value.
Here is a details comment on this. I am coping the comment here.
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.
Hope this helps. Good luck.
Without any plugins: You just need to enable highlight in IntelliJ:
IntelliJ Preferences -> Editor -> Inspections -> Java -> Serialization issues -> Serializable class without 'serialVersionUID'
- set flag and click 'OK'
.
Now, if your class implements Serializable
, you will see highlight, and alt+Enter
on class name will propose to generate private static final long serialVersionUID
.
PS: Taken from here