Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scala class serialization, impossible to fix SerialVersionUID

I'm currently testing remote actors to communicate between Android and Windows. Actors remote sends differents classes where I set the serialVersionUID.

This is the code of my serialized class:

@SerialVersionUID(13.asInstanceOf[Long]) case class IdentifyMessage(userName : String, user : User, code : Int)

the problem is that the remote actors debug says there is a problem with incompatible class:

caught java.io.InvalidClassException: scala.actors.remote.Node; local class incompatible: 
stream classdesc serialVersionUID = -6610463074147725500, local class serialVersionUID = -7525549079045563153

Why my SerialVersionUID doesn't matter for compiler?

How do I do to fix serialVersionUID? or maybe there is an another problem?

thanks

like image 216
reevolt Avatar asked Jun 23 '11 09:06

reevolt


People also ask

Is serializable consider declaring a serialVersionUID?

However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during ...

What is the role of serialVersionUID in serialization process?

Simply put, we use the serialVersionUID attribute to remember versions of a Serializable class to verify that a loaded class and the serialized object are compatible. The serialVersionUID attributes of different classes are independent. Therefore, it is not necessary for different classes to have unique values.

Why is serialVersionUID so long?

The SerialVersionUID must be declared as a private static final long variable in Java. This number is calculated by the compiler based on the state of the class and the class attributes. This is the number that will help the JVM to identify the state of an object when it reads the state of the object from a file.

What happens if two classes have same serialVersionUID?

The serialVersionUID is specific to a class, not an object, since they are static. I don't think it matters if two different classes have the same serialVersionUID, since it is used in deserializing objects of the same class.


1 Answers

For some reason using the long version of 13, 13l, works better:

@SerialVersionUID(13l) case class IdentifyMessage(userName : String, user : User, code : Int)

Tested in REPL with:

java.io.ObjectStreamClass.lookup(IdentifyMessage("hei", User(), 8).getClass).getSerialVersionUID()

Update

I also tried to run it as a program; like this:

object SerialTest extends App {
  case class User()
  @SerialVersionUID(13.asInstanceOf[Long]) case class IdentifyMessage1(userName: String, user: User, code: Int)
  @SerialVersionUID(13l) case class IdentifyMessage2(userName: String, user: User, code: Int)
  println("#1 " + java.io.ObjectStreamClass.lookup(IdentifyMessage1("hei", User(), 8).getClass).getSerialVersionUID)
  println("#2 " + java.io.ObjectStreamClass.lookup(IdentifyMessage2("hei", User(), 8).getClass).getSerialVersionUID)
}

... and got:

#1 6829060442504540290
#2 13
like image 182
thoredge Avatar answered Oct 13 '22 19:10

thoredge