@@ -2,18 +2,13 @@ package dotty.tools.dotc
2
2
package transform
3
3
4
4
import core ._
5
- import Symbols ._ , Types ._ , Contexts ._ , Names ._ , StdNames ._ , Constants ._ , SymUtils ._
6
- import scala .collection .{ mutable , immutable }
5
+ import Symbols ._ , Types ._ , Contexts ._ , StdNames ._ , Constants ._ , SymUtils ._
7
6
import Flags ._
8
- import MegaPhase ._
9
7
import DenotTransformers ._
10
- import ast .Trees ._
11
- import ast .untpd
12
8
import Decorators ._
13
9
import NameOps ._
14
10
import Annotations .Annotation
15
11
import ValueClasses .isDerivedValueClass
16
- import scala .collection .mutable .ListBuffer
17
12
import scala .language .postfixOps
18
13
19
14
/** Synthetic method implementations for case classes, case objects,
@@ -57,7 +52,7 @@ class SyntheticMethods(thisPhase: DenotTransformer) {
57
52
def caseModuleSymbols (implicit ctx : Context ) = { initSymbols; myCaseModuleSymbols }
58
53
59
54
/** 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 ] = {
61
56
val clazzType = clazz.appliedRef
62
57
lazy val accessors =
63
58
if (isDerivedValueClass(clazz)) clazz.paramAccessors.take(1 ) // Tail parameters can only be `erased`
@@ -261,12 +256,32 @@ class SyntheticMethods(thisPhase: DenotTransformer) {
261
256
*/
262
257
def canEqualBody (that : Tree ): Tree = that.isInstance(AnnotatedType (clazzType, Annotation (defn.UncheckedAnnot )))
263
258
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
265
271
}
266
272
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))
270
283
else
271
284
impl
285
+ }
286
+
272
287
}
0 commit comments