IntelliJ IDEA generating serialVersionUID

2019-01-20 21:45发布

How do generate this value in IntelliJ IDEA?

I go to Settings -> Errors -> Serialization issues -> Serializable class without ‘serialVersionUID’, but it still doesn't show me the warning. My class PKladrBuilding parent implements interface Serializable.

Part of the code:

public class PKladrBuilding extends PRQObject

public abstract class PRQObject extends PObject

public abstract class PObject implements Serializable

10条回答
萌系小妹纸
2楼-- · 2019-01-20 22:20

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.

查看更多
太酷不给撩
3楼-- · 2019-01-20 22:23

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.

查看更多
淡お忘
4楼-- · 2019-01-20 22:24

Install the GenerateSerialVersionUID plugin by Olivier Descout.

Go to: menu FileSettingsPluginsBrowse repositoriesGenerateSerialVersionUID

Install the plugin and restart.

Now you can generate the id from menu CodeGenerate → serialVersionUID` or the shortcut.

查看更多
姐就是有狂的资本
5楼-- · 2019-01-20 22:31

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.

查看更多
The star\"
6楼-- · 2019-01-20 22:37

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 serialTAB in class body.

查看更多
不美不萌又怎样
7楼-- · 2019-01-20 22:39

In order to generate the value use

private static final long serialVersionUID = $randomLong$L;
$END$

and provide the randomLong template variable with the following value: groovyScript("new Random().nextLong().abs()")

https://pharsfalvi.wordpress.com/2015/03/18/adding-serialversionuid-in-idea/

查看更多
登录 后发表回答