@@ -356,14 +356,26 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
356
356
}
357
357
358
358
private def hasWriteReplace (clazz : ClassSymbol )(using Context ): Boolean =
359
- clazz.membersNamed(nme.writeReplace)
360
- .filterWithPredicate(s => s.signature == Signature (defn.AnyRefType , isJava = false ))
361
- .exists
359
+ hasAnyRefDef(clazz, nme.writeReplace)
362
360
363
361
private def writeReplaceDef (clazz : ClassSymbol )(using Context ): TermSymbol =
364
- newSymbol(clazz, nme.writeReplace, Method | Private | Synthetic ,
362
+ privateAnyRefDef(clazz, nme.writeReplace)
363
+
364
+ private def readResolveDef (clazz : ClassSymbol )(using Context ): TermSymbol =
365
+ privateAnyRefDef(clazz, nme.readResolve)
366
+
367
+ private def hasReadResolve (clazz : ClassSymbol )(using Context ): Boolean =
368
+ hasAnyRefDef(clazz, nme.readResolve)
369
+
370
+ private def privateAnyRefDef (clazz : ClassSymbol , name : TermName )(using Context ): TermSymbol =
371
+ newSymbol(clazz, name, Method | Private | Synthetic ,
365
372
MethodType (Nil , defn.AnyRefType ), coord = clazz.coord).entered.asTerm
366
373
374
+ private def hasAnyRefDef (clazz : ClassSymbol , name : TermName )(using Context ): Boolean =
375
+ clazz.membersNamed(name)
376
+ .filterWithPredicate(s => s.signature == Signature (defn.AnyRefType , isJava = false ))
377
+ .exists
378
+
367
379
/** If this is a serializable static object `Foo`, add the method:
368
380
*
369
381
* private def writeReplace(): AnyRef =
@@ -389,23 +401,24 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
389
401
/** If this the class backing a serializable singleton enum value with base class `MyEnum`,
390
402
* and not deriving from `java.lang.Enum` add the method:
391
403
*
392
- * private def writeReplace (): AnyRef =
393
- * new scala.runtime.EnumValueSerializationProxy(classOf[MyEnum], this.ordinal)
404
+ * private def readResolve (): AnyRef =
405
+ * MyEnum.values( this.ordinal)
394
406
*
395
407
* unless an implementation already exists, otherwise do nothing.
396
408
*/
397
409
def serializableEnumValueMethod (clazz : ClassSymbol )(using Context ): List [Tree ] =
398
410
if clazz.isEnumValueImplementation
399
411
&& ! clazz.derivesFrom(defn.JavaEnumClass )
400
412
&& clazz.isSerializable
401
- && ! hasWriteReplace (clazz)
413
+ && ! hasReadResolve (clazz)
402
414
then
403
415
List (
404
- DefDef (writeReplaceDef(clazz),
405
- _ => New (defn.EnumValueSerializationProxyClass .typeRef,
406
- defn.EnumValueSerializationProxyConstructor ,
407
- List (Literal (Constant (clazz.classParents.head)), This (clazz).select(nme.ordinal).ensureApplied)))
408
- .withSpan(ctx.owner.span.focus))
416
+ DefDef (readResolveDef(clazz),
417
+ _ => ref(clazz.classParents.head.typeSymbol.companionModule)
418
+ .select(nme.values).ensureApplied // `.values` must exist if `isEnumValueImplementation` is true
419
+ .select(nme.apply).appliedTo(This (clazz).select(nme.ordinal).ensureApplied)
420
+ ).withSpan(ctx.owner.span.focus)
421
+ )
409
422
else
410
423
Nil
411
424
0 commit comments