IntelliJ IDEA generating serialVersionUID

前端 未结 12 2112
生来不讨喜
生来不讨喜 2020-12-04 05:04

How do generate this value in IntelliJ IDEA?

I go to Settings -> Errors -> Serialization issues -> Serializable class without ‘serialVersi

相关标签:
12条回答
  • 2020-12-04 05:40

    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.

    0 讨论(0)
  • 2020-12-04 05:44

    Another way to generate the serialVersionUID is to use >Analyze >Run Inspection by Name from the context menu ( or the keyboard short cut, which is ctrl+alt+shift+i by default) and then type "Serializable class without 'serialVersionUID'" (or simply type "serialVersionUID" and the type ahead function will find it for you.

    You will then get a context menu where you can choose where to run the inspections on (e.g. all from a specific module, whole project, one file, ...)

    With this approach you don't even have to set the general inspection rules to anything.

    0 讨论(0)
  • 2020-12-04 05:48

    Easiest modern method: Alt+Enter on

    private static final long serialVersionUID = ;
    

    IntelliJ will underline the space after the =. put your cursor on it and hit alt+Enter (Option+Enter on Mac). You'll get a popover that says "Randomly Change serialVersionUID Initializer". Just hit enter, and it'll populate that space with a random long.

    0 讨论(0)
  • 2020-12-04 05:50

    I am using Android Studio 2.1 and I have better consistency of getting the lightbulb by clicking on the class Name and hover over it for a second.

    0 讨论(0)
  • 2020-12-04 05:52

    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/

    0 讨论(0)
  • 2020-12-04 05:52

    If you want to add the absent serialVersionUID for a bunch of files, IntelliJ IDEA may not work very well. I come up some simple script to fulfill this goal with ease:

    base_dir=$(pwd)
    src_dir=$base_dir/src/main/java
    ic_api_cp=$base_dir/target/classes
    
    while read f
    do
        clazz=${f//\//.}
        clazz=${clazz/%.java/}
        seruidstr=$(serialver -classpath $ic_api_cp $clazz | cut -d ':' -f 2 | sed -e 's/^\s\+//')
        perl -ni.bak -e "print $_; printf qq{%s\n}, q{    private $seruidstr} if /public class/" $src_dir/$f
    done
    

    You save this script, say as add_serialVersionUID.sh in your ~/bin folder. Then you run it in the root directory of your Maven or Gradle project like:

    add_serialVersionUID.sh < myJavaToAmend.lst
    

    This .lst includes the list of Java files to add the serialVersionUID in the following format:

    com/abc/ic/api/model/domain/item/BizOrderTransDO.java
    com/abc/ic/api/model/domain/item/CardPassFeature.java
    com/abc/ic/api/model/domain/item/CategoryFeature.java
    com/abc/ic/api/model/domain/item/GoodsFeature.java
    com/abc/ic/api/model/domain/item/ItemFeature.java
    com/abc/ic/api/model/domain/item/ItemPicUrls.java
    com/abc/ic/api/model/domain/item/ItemSkuDO.java
    com/abc/ic/api/model/domain/serve/ServeCategoryFeature.java
    com/abc/ic/api/model/domain/serve/ServeFeature.java
    com/abc/ic/api/model/param/depot/DepotItemDTO.java
    com/abc/ic/api/model/param/depot/DepotItemQueryDTO.java
    com/abc/ic/api/model/param/depot/InDepotDTO.java
    com/abc/ic/api/model/param/depot/OutDepotDTO.java
    

    This script uses the JDK serialVer tool. It is ideal for a situation when you want to amend a huge number of classes which had no serialVersionUID set in the first place while maintain the compatibility with the old classes.

    0 讨论(0)
提交回复
热议问题