Skip to content

add readResolve to objects with serializable interface #4450

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

Closed
wants to merge 1 commit into from
Closed
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: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,8 @@ class Definitions {
case _ => false
}).symbol.asTerm
lazy val JavaSerializableClass = ctx.requiredClass("java.io.Serializable")
def readResolve(cls: ClassSymbol, flags: FlagSet) = enterMethod(cls, nme.readResolve, MethodType(Nil, AnyRefType), flags)

lazy val ComparableClass = ctx.requiredClass("java.lang.Comparable")

lazy val SystemClass = ctx.requiredClass("java.lang.System")
Expand Down
38 changes: 27 additions & 11 deletions compiler/src/dotty/tools/dotc/transform/SyntheticMethods.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@ package dotty.tools.dotc
package transform

import core._
import Symbols._, Types._, Contexts._, Names._, StdNames._, Constants._, SymUtils._
import scala.collection.{ mutable, immutable }
import Symbols._, Types._, Contexts._, StdNames._, Constants._, SymUtils._
import Flags._
import MegaPhase._
import DenotTransformers._
import ast.Trees._
import ast.untpd
import Decorators._
import NameOps._
import Annotations.Annotation
import ValueClasses.isDerivedValueClass
import scala.collection.mutable.ListBuffer
import scala.language.postfixOps

/** Synthetic method implementations for case classes, case objects,
Expand Down Expand Up @@ -57,7 +52,7 @@ class SyntheticMethods(thisPhase: DenotTransformer) {
def caseModuleSymbols(implicit ctx: Context) = { initSymbols; myCaseModuleSymbols }

/** The synthetic methods of the case or value class `clazz`. */
def syntheticMethods(clazz: ClassSymbol)(implicit ctx: Context): List[Tree] = {
def syntheticMethods(clazz: ClassSymbol, isSerializableObject: Boolean)(implicit ctx: Context): List[Tree] = {
val clazzType = clazz.appliedRef
lazy val accessors =
if (isDerivedValueClass(clazz)) clazz.paramAccessors.take(1) // Tail parameters can only be `erased`
Expand Down Expand Up @@ -261,12 +256,33 @@ class SyntheticMethods(thisPhase: DenotTransformer) {
*/
def canEqualBody(that: Tree): Tree = that.isInstance(AnnotatedType(clazzType, Annotation(defn.UncheckedAnnot)))

symbolsToSynthesize flatMap syntheticDefIfMissing
val methods = symbolsToSynthesize flatMap syntheticDefIfMissing

def createReadResolveMethod(implicit ctx: Context): Tree = {
ctx.log(s"adding readResolve to $clazz at ${ctx.phase}")
val readResolve = defn.readResolve(clazz, Private | Synthetic)
DefDef(readResolve, _ => ref(clazz.sourceModule)).withPos(ctx.owner.pos.focus)
}

if (isSerializableObject)
createReadResolveMethod :: methods
else
methods
}

def addSyntheticMethods(impl: Template)(implicit ctx: Context) =
if (ctx.owner.is(Case) || isDerivedValueClass(ctx.owner))
cpy.Template(impl)(body = impl.body ++ syntheticMethods(ctx.owner.asClass))
def addSyntheticMethods(impl: Template)(implicit ctx: Context) = {
val isSerializableObject =
(ctx.owner.is(Module)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To match scalac behavior, we should only do this for static objects (that is, objects which are not inside classes or methods), this can be checked using .isStatic.

&& ctx.owner.isStatic
&& ctx.owner.derivesFrom(defn.JavaSerializableClass)
&& !ctx.owner.asClass.membersNamed(nme.readResolve)
.filterWithPredicate(s => s.signature == Signature(defn.AnyRefType, isJava = false))
.exists)

if (ctx.owner.is(Case) || isDerivedValueClass(ctx.owner) || isSerializableObject)
cpy.Template(impl)(body = impl.body ++ syntheticMethods(ctx.owner.asClass, isSerializableObject))
else
impl
}

}
14 changes: 14 additions & 0 deletions tests/run/serialize.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,23 @@ object Test {
in.readObject.asInstanceOf[T]
}

object Foo extends Serializable {}

object Baz extends Serializable {
private def readResolve(): AnyRef = {
this
}
}

def main(args: Array[String]): Unit = {
val x: PartialFunction[Int, Int] = { case x => x + 1 }
val adder = serializeDeserialize(x)
assert(adder(1) == 2)

val foo = serializeDeserialize(Foo)
assert(foo eq Foo)

val baz = serializeDeserialize(Baz)
assert(baz ne Baz)
}
}