@@ -2,18 +2,13 @@ package dotty.tools.dotc
22package transform
33
44import core ._
5- import Symbols ._ , Types ._ , Contexts ._ , Names ._ , StdNames ._ , Constants ._ , SymUtils ._
6- import scala .collection .{ mutable , immutable }
5+ import Symbols ._ , Types ._ , Contexts ._ , StdNames ._ , Constants ._ , SymUtils ._
76import Flags ._
8- import MegaPhase ._
97import DenotTransformers ._
10- import ast .Trees ._
11- import ast .untpd
128import Decorators ._
139import NameOps ._
1410import Annotations .Annotation
1511import ValueClasses .isDerivedValueClass
16- import scala .collection .mutable .ListBuffer
1712import scala .language .postfixOps
1813
1914/** Synthetic method implementations for case classes, case objects,
@@ -57,7 +52,7 @@ class SyntheticMethods(thisPhase: DenotTransformer) {
5752 def caseModuleSymbols (implicit ctx : Context ) = { initSymbols; myCaseModuleSymbols }
5853
5954 /** The synthetic methods of the case or value class `clazz`. */
60- def syntheticMethods (clazz : ClassSymbol )(implicit ctx : Context ): List [Tree ] = {
55+ def syntheticMethods (clazz : ClassSymbol , isSerializableObject : Boolean )(implicit ctx : Context ): List [Tree ] = {
6156 val clazzType = clazz.appliedRef
6257 lazy val accessors =
6358 if (isDerivedValueClass(clazz)) clazz.paramAccessors.take(1 ) // Tail parameters can only be `erased`
@@ -261,12 +256,32 @@ class SyntheticMethods(thisPhase: DenotTransformer) {
261256 */
262257 def canEqualBody (that : Tree ): Tree = that.isInstance(AnnotatedType (clazzType, Annotation (defn.UncheckedAnnot )))
263258
264- symbolsToSynthesize flatMap syntheticDefIfMissing
259+ val methods = symbolsToSynthesize flatMap syntheticDefIfMissing
260+
261+ def createReadResolveMethod (implicit ctx : Context ): Tree = {
262+ ctx.log(s " adding readResolve to $clazz at ${ctx.phase}" )
263+ val readResolve = defn.readResolve(clazz, Private | Synthetic )
264+ DefDef (readResolve, _ => ref(clazz.sourceModule)).withPos(ctx.owner.pos.focus)
265+ }
266+
267+ if (isSerializableObject)
268+ createReadResolveMethod :: methods
269+ else
270+ methods
265271 }
266272
267- def addSyntheticMethods (impl : Template )(implicit ctx : Context ) =
268- if (ctx.owner.is(Case ) || isDerivedValueClass(ctx.owner))
269- cpy.Template (impl)(body = impl.body ++ syntheticMethods(ctx.owner.asClass))
273+ def addSyntheticMethods (impl : Template )(implicit ctx : Context ) = {
274+ val isSerializableObject =
275+ (ctx.owner.isStatic
276+ && ctx.owner.derivesFrom(defn.JavaSerializableClass )
277+ && ! ctx.owner.asClass.membersNamed(nme.readResolve)
278+ .filterWithPredicate(s => s.signature == Signature (defn.AnyRefType , isJava = false ))
279+ .exists)
280+
281+ if (ctx.owner.is(Case ) || isDerivedValueClass(ctx.owner) || isSerializableObject)
282+ cpy.Template (impl)(body = impl.body ++ syntheticMethods(ctx.owner.asClass, isSerializableObject))
270283 else
271284 impl
285+ }
286+
272287}
0 commit comments