bug icon indicating copy to clipboard operation
bug copied to clipboard

anonymous class automatic serialVersionUID insertion

Open scabug opened this issue 15 years ago • 5 comments

Anonymous class can derive from serializable base class (either via java.io.Serializable or @serializable). Currently, there is no way to associate a serialVersionUID with the anonymous class.

@serializable
trait Foo {
  def xy: Int
  override def toString: String = "Foo["+ xy + "]"
}
// Can not associate serialVersionUID here
val x = new Foo {
  def xy = 2
}

This was noted in Ticket #832. There were two problems with 832. First it was marked as an enhancement. If scalability is important to Scala, then being able to detect during deserialization when there is a serialVersionUID mismatch is a base capability - not an enhancement. Secondly, it was stated that there was a work around, create a named class. Well, there is no need to even use Scala since the work around is to use Java ... but Scala lets one do things in an easier and, code-wise, more efficient manner - hence using a name class is a Java-like work around and is not in the spirit of Scala.

Proposal:

Add code to the Scala class tools/nsc/transform/CleanUp.scala which adds a serialVersionUID to an anonymous class if its base class is serializable. Something like:

case cdef @ ClassDef(mods, name, tparams, impl) =>
    val sym = cdef.symbol
    // is this an anonymous class?
    if (sym.isAnonymousClass && sym.parentHasAnnotation(SerializableAttr)) {
      sym addAnnotation serialVersionUIDAnnotation
    }
    super.transform(tree)

(About 90% down the file where a serialVersionUID is automatically generated for anonymous functions.)

At some future time, Scala syntax can be changed to increase its scalability by supporting the adding of serialVersionUID to anonymous classes. Until then, with the above change, during the preparation for application code release, a post-compile stage can use Java's serialver and class file editing scripts to set each anonymous class' serialVersionUID to the value generated by serialver. This will allow for the detection of application version mismatches in distributed environments when using serialization.

scabug avatar Oct 01 '10 14:10 scabug

Imported From: https://issues.scala-lang.org/browse/SI-3889?orig=1 Reporter: Richard Emberson (rmemberson) See #832

scabug avatar Oct 01 '10 14:10 scabug

@harrah said: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4862448

http://download.oracle.com/javase/6/docs/platform/serialization/spec/serial-arch.html

scabug avatar Oct 01 '10 14:10 scabug

Richard Emberson (rmemberson) said: As a clarification, Scala does allow anonymous function class automatic serialVersionUID insertion. see: tools/nsc/transform/CleanUp.scala

/* Adds @serializable annotation to anonymous function classes */
case cdef @ ClassDef(mods, name, tparams, impl) =>
  if (settings.target.value == "jvm-1.5") {
    val sym = cdef.symbol
    // is this an anonymous function class?
    if (sym.isAnonymousFunction && !sym.hasAnnotation(SerializableAttr)) {
      sym addAnnotation serializableAnnotation
      sym addAnnotation serialVersionUIDAnnotation
    }
  }
  super.transform(tree)

But does not allow anonymous class automatic serialVersionUID insertion.

And the reasoning in the provided links apply to an anonymous class but not an anonymous function class. I understand, thanks.

scabug avatar Oct 01 '10 14:10 scabug

@harrah said: Although serializing anonymous classes (including anonymous functions) is only reliable between binaries compiled from the same exact source by the same compiler, it seems from #527 that Scala intends to support it. In this case, I guess a serialVersionUID should be inserted.

scabug avatar Oct 01 '10 15:10 scabug

@harrah said: I'll add that I think automatic serialVersionUID generation for anonymous classes is sufficient. Manually specifying it would be unreliable for the reasons above.

scabug avatar Oct 01 '10 15:10 scabug