Skip to content
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

add readResolve to objects with serializable interface #4450

Closed
wants to merge 1 commit into from

Conversation

ingarabr
Copy link
Contributor

@ingarabr ingarabr commented May 3, 2018

If the object implements the serializable interface and not includes the
readResolve method then we add it. Since it's an object we reference it
to the objects singleton instance.

Resolves issue #4440

Copy link
Member

@dottybot dottybot left a comment

Choose a reason for hiding this comment

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

Hello, and thank you for opening this PR! 🎉

All contributors have signed the CLA, thank you! ❤️

Have an awesome day! ☀️

symbolsToSynthesize flatMap syntheticDefIfMissing
val methods = symbolsToSynthesize flatMap syntheticDefIfMissing

def createReadResolveMethod: Tree = {
Copy link
Member

Choose a reason for hiding this comment

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

It'd be nice to extract the common code between this method and syntheticDef in a separate method somehow.

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.

val isSerializableObject =
(ctx.owner.is(Module)
&& ctx.owner.derivesFrom(defn.JavaSerializableClass)
&& !ctx.owner.asClass.membersNamed(nme.readResolve).exists)
Copy link
Member

Choose a reason for hiding this comment

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

We should still emit our readResolve method if the user declared a readResolve method with a different signature (e.g. def readResolve(x: Int): AnyRef). We can filter for methods with (): AnyRef as signature by doing membersNamed(nme.readResolve).suchThat(_.signature == Signature(defn.AnyRefType, isJava = false)

@ingarabr ingarabr force-pushed the read-resolve branch 3 times, most recently from 0072dc2 to 7702780 Compare May 7, 2018 22:09
If the object implements the serializable interface and not includes the
readResolve method then we add it. Since it's an object we reference it
to the objects singleton instance.

Resolves issue scala#4440
@odersky
Copy link
Contributor

odersky commented Jan 12, 2019

@smarter Have your comments been addressed? If yes please merge.

@smarter
Copy link
Member

smarter commented Jan 12, 2019

I'll have a look next week, I think there's still one thing I wanted to do here.

smarter pushed a commit to dotty-staging/dotty that referenced this pull request Jan 22, 2019
In scala#4450 and Scala 2.12, readResolve is used to make sure deserializing
object returns the singleton instance of the object, but this doesn't
prevent the fields of the objects from being serializable in the first
place even though they're not used. Scala 2.13 switched to using
writeReplace to completely bypass serialization of the object in
scala/scala#7297. This commit adapts this to
Dotty.

Co-Authored-By: Ingar Abrahamsen <[email protected]>
Co-Authored-By: Jason Zaugg <[email protected]>
@smarter
Copy link
Member

smarter commented Jan 22, 2019

I've opened a new PR adapted from this PR and scala/scala#7297 at #5775, so I'll close this one, thanks for your work @ingarabr !

@smarter smarter closed this Jan 22, 2019
smarter pushed a commit to dotty-staging/dotty that referenced this pull request Jan 22, 2019
In scala#4450 and Scala 2.12, readResolve is used to make sure deserializing
an object returns the singleton instance of the object, but this doesn't
prevent the fields of the objects from being serialized in the first
place even though they're not used. Scala 2.13 switched to using
writeReplace to completely bypass serialization of the object in
scala/scala#7297. This commit adapts this to
Dotty.

Co-Authored-By: Ingar Abrahamsen <[email protected]>
Co-Authored-By: Jason Zaugg <[email protected]>
smarter pushed a commit to dotty-staging/dotty that referenced this pull request Jan 22, 2019
In scala#4450 and Scala 2.12, readResolve is used to make sure deserializing
an object returns the singleton instance of the object, but this doesn't
prevent the fields of the objects from being serialized in the first
place even though they're not used. Scala 2.13 switched to using
writeReplace to completely bypass serialization of the object in
scala/scala#7297. This commit adapts this to
Dotty.

Co-Authored-By: Ingar Abrahamsen <[email protected]>
Co-Authored-By: Jason Zaugg <[email protected]>
smarter pushed a commit to dotty-staging/dotty that referenced this pull request Jan 22, 2019
In scala#4450 and Scala 2.12, readResolve is used to make sure deserializing
an object returns the singleton instance of the object, but this doesn't
prevent the fields of the objects from being serialized in the first
place even though they're not used. Scala 2.13 switched to using
writeReplace to completely bypass serialization of the object in
scala/scala#7297. This commit adapts this to
Dotty.

Co-Authored-By: Ingar Abrahamsen <[email protected]>
Co-Authored-By: Jason Zaugg <[email protected]>
smarter pushed a commit to smarter/dotty that referenced this pull request Jan 25, 2019
In scala#4450 and Scala 2.12, readResolve is used to make sure deserializing
an object returns the singleton instance of the object, but this doesn't
prevent the fields of the objects from being serialized in the first
place even though they're not used. Scala 2.13 switched to using
writeReplace to completely bypass serialization of the object in
scala/scala#7297. This commit adapts this to
Dotty.

Co-Authored-By: Ingar Abrahamsen <[email protected]>
Co-Authored-By: Jason Zaugg <[email protected]>
smarter pushed a commit to dotty-staging/dotty that referenced this pull request Jan 25, 2019
In scala#4450 and Scala 2.12, readResolve is used to make sure deserializing
an object returns the singleton instance of the object, but this doesn't
prevent the fields of the objects from being serialized in the first
place even though they're not used. Scala 2.13 switched to using
writeReplace to completely bypass serialization of the object in
scala/scala#7297. This commit adapts this to
Dotty.

Co-Authored-By: Ingar Abrahamsen <[email protected]>
Co-Authored-By: Jason Zaugg <[email protected]>
smarter pushed a commit to dotty-staging/dotty that referenced this pull request Jan 25, 2019
In scala#4450 and Scala 2.12, readResolve is used to make sure deserializing
an object returns the singleton instance of the object, but this doesn't
prevent the fields of the objects from being serialized in the first
place even though they're not used. Scala 2.13 switched to using
writeReplace to completely bypass serialization of the object in
scala/scala#7297. This commit adapts this to
Dotty.

Co-Authored-By: Ingar Abrahamsen <[email protected]>
Co-Authored-By: Jason Zaugg <[email protected]>
smarter pushed a commit to dotty-staging/dotty that referenced this pull request Jan 25, 2019
In scala#4450 and Scala 2.12, readResolve is used to make sure deserializing
an object returns the singleton instance of the object, but this doesn't
prevent the fields of the objects from being serialized in the first
place even though they're not used. Scala 2.13 switched to using
writeReplace to completely bypass serialization of the object in
scala/scala#7297. This commit adapts this to
Dotty.

Co-Authored-By: Ingar Abrahamsen <[email protected]>
Co-Authored-By: Jason Zaugg <[email protected]>
smarter added a commit to dotty-staging/dotty that referenced this pull request Jan 25, 2019
In scala#4450 and Scala 2.12, readResolve is used to make sure deserializing
an object returns the singleton instance of the object, but this doesn't
prevent the fields of the objects from being serialized in the first
place even though they're not used. Scala 2.13 switched to using
writeReplace to completely bypass serialization of the object in
scala/scala#7297. This commit adapts this to
Dotty.

Co-Authored-By: Ingar Abrahamsen <[email protected]>
Co-Authored-By: Jason Zaugg <[email protected]>
smarter added a commit to dotty-staging/dotty that referenced this pull request Jan 26, 2019
In scala#4450 and Scala 2.12, readResolve is used to make sure deserializing
an object returns the singleton instance of the object, but this doesn't
prevent the fields of the objects from being serialized in the first
place even though they're not used. Scala 2.13 switched to using
writeReplace to completely bypass serialization of the object in
scala/scala#7297. This commit adapts this to
Dotty.

Co-Authored-By: Ingar Abrahamsen <[email protected]>
Co-Authored-By: Jason Zaugg <[email protected]>
smarter added a commit to dotty-staging/dotty that referenced this pull request Feb 2, 2019
In scala#4450 and Scala 2.12, readResolve is used to make sure deserializing
an object returns the singleton instance of the object, but this doesn't
prevent the fields of the objects from being serialized in the first
place even though they're not used. Scala 2.13 switched to using
writeReplace to completely bypass serialization of the object in
scala/scala#7297. This commit adapts this to
Dotty.

Co-Authored-By: Ingar Abrahamsen <[email protected]>
Co-Authored-By: Jason Zaugg <[email protected]>
smarter added a commit to dotty-staging/dotty that referenced this pull request Feb 2, 2019
In scala#4450 and Scala 2.12, readResolve is used to make sure deserializing
an object returns the singleton instance of the object, but this doesn't
prevent the fields of the objects from being serialized in the first
place even though they're not used. Scala 2.13 switched to using
writeReplace to completely bypass serialization of the object in
scala/scala#7297. This commit adapts this to
Dotty.

Co-Authored-By: Ingar Abrahamsen <[email protected]>
Co-Authored-By: Jason Zaugg <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants