Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate serialVersionUID for kotlin exceptions?

As kotlin doesn't have static fields, should I define serialVersionUID in companion object?

like image 241
Winter Young Avatar asked Dec 12 '15 15:12

Winter Young


People also ask

How do I generate serialVersionUID?

Once you've enabled this plugin, you just need to press Alt+Insert / Command+n when you are in the body of a class implementing the Serializable interface. You should see an option to generate the serialVersionUID field for you.

How serialVersionUID is generated when it's not defined in code?

If class does not contain a serialVersionUID field, its serialVersionUID will be automatically generated by the compiler. Different compilers, or different versions of the same compiler, will generate potentially different values.

What value should I use for serialVersionUID?

4. 1L. Puts serialVersionUID=1L ; It should be sufficient in most cases.


2 Answers

To create the serialVersionUID for a class in Kotlin you have a few options all involving adding a member to the companion object of the class.

The most concise bytecode comes from a private const val which will become a private static variable on the containing class, in this case MySpecialCase:

class MySpecialCase : Serializable {
    companion object {
        private const val serialVersionUID: Long = 123
    }
}

You can also use these forms, each with a side effect of having getter/setter methods which are not necessary for serialization...

class MySpecialCase : Serializable {
    companion object {
        private val serialVersionUID: Long = 123
    }
}

This creates the static field but also creates a getter as well getSerialVersionUID on the companion object which is unnecessary.

class MySpecialCase : Serializable {
    companion object {
        @JvmStatic private val serialVersionUID: Long = 123
    }
}  

This creates the static field but also creates a static getter as well getSerialVersionUID on the containing class MySpecialCase which is unnecessary.

But all work as a method of adding the serialVersionUID to a Serializable class.

like image 132
4 revs, 2 users 97% Avatar answered Oct 15 '22 02:10

4 revs, 2 users 97%


Yes, you can declare it in the companion object. The doc says:

Also, public properties defined in objects and companion objects, as well as top-level properties annotated with const, are turned into static fields in Java

But that seems to be the case with private properties too:

class MyException: Exception() {
    companion object {
        private val serialVersionUid: Long = 1
    }
}

javap -c -p com.ninja_squad.kotlindiscovery.MyException.class

Compiled from "MyException.kt"
public final class com.ninja_squad.kotlindiscovery.MyException extends java.lang.Exception {
  private static final long serialVersionUid;

  public static final com.ninja_squad.kotlindiscovery.MyException$Companion Companion;

  static {};
    Code:
       0: getstatic     #38                 // Field com/ninja_squad/kotlindiscovery/MyException$Companion.INSTANCE:Lcom/ninja_squad/kotlindiscovery/MyException$Companion;
       3: putstatic     #40                 // Field Companion:Lcom/ninja_squad/kotlindiscovery/MyException$Companion;
       6: lconst_1      
       7: putstatic     #21                 // Field serialVersionUid:J
      10: return        

  public com.ninja_squad.kotlindiscovery.MyException();
    Code:
       0: aload_0       
       1: invokespecial #15                 // Method java/lang/Exception."<init>":()V
       4: return        

  public static final long access$getSerialVersionUid$cp();
    Code:
       0: getstatic     #21                 // Field serialVersionUid:J
       3: lreturn       
}
like image 11
JB Nizet Avatar answered Oct 15 '22 03:10

JB Nizet