Skip to content

Fix #7319: Properly support @serialVersionUID #7331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/backend/jvm/BCodeHelpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ trait BCodeHelpers extends BCodeIdiomatic with BytecodeWriters {
def addSerialVUID(id: Long, jclass: asm.ClassVisitor): Unit = {
// add static serialVersionUID field if `clasz` annotated with `@SerialVersionUID(uid: Long)`
jclass.visitField(
GenBCodeOps.PublicStaticFinal,
GenBCodeOps.PrivateStaticFinal,
"serialVersionUID",
"J",
null, // no java-generic-signature
Expand Down
10 changes: 8 additions & 2 deletions compiler/src/dotty/tools/backend/jvm/DottyBackendInterface.scala
Original file line number Diff line number Diff line change
Expand Up @@ -791,8 +791,14 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
}
def methodSymbols: List[Symbol] =
for (f <- toDenot(sym).info.decls.toList if f.isMethod && f.isTerm && !f.isModule) yield f
def serialVUID: Option[Long] = None

def serialVUID: Option[Long] =
sym.getAnnotation(defn.SerialVersionUIDAnnot).flatMap { annot =>
val vuid = annot.argumentConstant(0).map(_.longValue)
if (vuid.isEmpty)
ctx.error("The argument passed to @SerialVersionUID must be a constant",
annot.argument(0).getOrElse(annot.tree).sourcePos)
vuid
}

def freshLocal(cunit: CompilationUnit, name: String, tpe: Type, pos: Position, flags: Flags): Symbol = {
ctx.newSymbol(sym, name.toTermName, termFlagSet(flags), tpe, NoSymbol, pos)
Expand Down
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/backend/jvm/GenBCodeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ class GenBCodeOps {

final val PublicStatic = asm.Opcodes.ACC_PUBLIC | asm.Opcodes.ACC_STATIC
final val PublicStaticFinal = asm.Opcodes.ACC_PUBLIC | asm.Opcodes.ACC_STATIC | asm.Opcodes.ACC_FINAL
final val PrivateStaticFinal = asm.Opcodes.ACC_PRIVATE | asm.Opcodes.ACC_STATIC | asm.Opcodes.ACC_FINAL
}
8 changes: 8 additions & 0 deletions tests/neg/serialversionuid-not-const.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@SerialVersionUID(13l.toLong) class C1 extends Serializable // error
@SerialVersionUID(13l) class C2 extends Serializable // OK
@SerialVersionUID(13.asInstanceOf[Long]) class C3 extends Serializable // error
@SerialVersionUID(Test.bippy) class C4 extends Serializable // error

object Test {
val bippy = 13L
}
20 changes: 20 additions & 0 deletions tests/run/t8549b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import java.lang.reflect.Modifier._

@SerialVersionUID(42)
class C extends Serializable

@SerialVersionUID(43 - 1)
class D extends Serializable


object Test extends App {
def checkId(cls: Class[_]): Unit = {
val field = cls.getDeclaredField("serialVersionUID")
assert(isPrivate(field.getModifiers))
field.setAccessible(true)
val id = field.get(null).asInstanceOf[Long]
assert(id == 42, (cls, id))
}
checkId(classOf[C])
checkId(classOf[D])
}