Skip to content

Commit 4267444

Browse files
committed
Fix for SerialVersionUID instability.
Can hardly believe this has been broken for a decade or so, but there it is - see test case. Four classes attempt to set their SerialVersionUID to 13. One succeeds. No warnings or errors. The output before this patch (for me anyway - your random numbers may differ) was: 860336111422349646 13 8409527228024057943 -7852527872932878365 There was already code in place for rejecting annotations with non-constant args when constant args are required, but that check is only performed on ClassfileAnnotations, and SerialVersionUID was a StaticAnnotation. Maybe people don't reach for ClassfileAnnotation because of this giant warning which I see no way to suppress: warning: Implementation restriction: subclassing Classfile does not make your annotation visible at runtime. If that is what you want, you must write the annotation class in Java. Why did I change the name of the field from uid to value? If you don't use the name 'value', you have to name the argument every time you use it, even if it's the only parameter. I didn't relish breaking every usage of scala's @serialversionuid in the known universe.
1 parent 9afc00c commit 4267444

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

src/library/scala/SerialVersionUID.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ package scala
1212
* Annotation for specifying the `static SerialVersionUID` field
1313
* of a serializable class.
1414
*/
15-
class SerialVersionUID(uid: Long) extends scala.annotation.StaticAnnotation
15+
class SerialVersionUID(value: Long) extends scala.annotation.ClassfileAnnotation
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
serialversionuid-not-const.scala:1: error: annotation argument needs to be a constant; found: 13L.toLong
2+
@SerialVersionUID(13l.toLong) class C1 extends Serializable
3+
^
4+
serialversionuid-not-const.scala:3: error: annotation argument needs to be a constant; found: 13.asInstanceOf[Long]
5+
@SerialVersionUID(13.asInstanceOf[Long]) class C3 extends Serializable
6+
^
7+
serialversionuid-not-const.scala:4: error: annotation argument needs to be a constant; found: Test.bippy
8+
@SerialVersionUID(Test.bippy) class C4 extends Serializable
9+
^
10+
three errors found
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@SerialVersionUID(13l.toLong) class C1 extends Serializable
2+
@SerialVersionUID(13l) class C2 extends Serializable
3+
@SerialVersionUID(13.asInstanceOf[Long]) class C3 extends Serializable
4+
@SerialVersionUID(Test.bippy) class C4 extends Serializable
5+
6+
object Test {
7+
val bippy = 13L
8+
9+
def show(c: Class[_]) = println(java.io.ObjectStreamClass.lookup(c).getSerialVersionUID)
10+
def main(args: Array[String]): Unit = {
11+
show(classOf[C1])
12+
show(classOf[C2])
13+
show(classOf[C3])
14+
show(classOf[C4])
15+
}
16+
}

0 commit comments

Comments
 (0)