reevolt
reevolt

Reputation: 807

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

Upvotes: 8

Views: 4421

Answers (1)

thoredge
thoredge

Reputation: 12611

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

Upvotes: 4

Related Questions