-
Notifications
You must be signed in to change notification settings - Fork 21
readResolve for nested objects causes a new object to be created instead of using the de-serialized one #9375
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
Comments
Imported From: https://issues.scala-lang.org/browse/SI-9375?orig=1 |
@lrytz said (edited on Jul 28, 2015 7:43:12 PM UTC): def readResolve() = {
if ($outer.attr$module == null) this
else $outer.attr$module
} Since the field ( Q: should the method be synchronized? CC @axel22 |
@lrytz said: |
@ijuma said: |
@lrytz said: |
@ijuma said: |
@axel22 said: However, there might be multiple instances of the class Outer extends Serializable {
def foo = attr
object attr extends Serializable {
println("Ctor!")
var x = "default"
override def toString = s"attr($x)"
private def readObject(in: ObjectInputStream) {
println("dro begin")
in.defaultReadObject
println("dro done")
foo
}
}
override def toString = "Outer - " + attr
}
// Exiting paste mode, now interpreting.
defined class Outer
scala> val outer = new Outer
Ctor!
outer: Outer = Outer - attr(default)
scala> outer.attr.x = "custom"
outer.attr.x: String = custom
scala> serializeDeserialize(outer)
dro begin
dro done
Ctor!
res11: Outer = Outer - attr(default) To avoid this situation, the def readResolve() = {
if ($outer.attr$module == null) {
$outer.attr$module = this
this
} else $outer.attr$module
} I would avoid synchronization here, since it could result in subtle deadlocks, but does not buy you much. |
@lrytz said (edited on Jul 20, 2015 9:37:06 AM UTC): I'm not sure your suggestion would help in the situation you describe though. It seems
I don't see a way to prevent invoking the module constructor if the module is accessed by a custom case class C(var s: String) {
private def readObject(in: ObjectInputStream): Unit = {
println("Before Default")
println(this)
in.defaultReadObject
println("After Default")
println(this)
this.s = "readObject"
println(this)
}
private def readResolve(): Object = {
println("readResolve")
C("readResolve")
}
}
|
Adam Pocock (craigacp) said (edited on Jul 21, 2015 1:03:26 PM UTC): |
@lrytz said: |
@axel22 said: Long answer: // generated accesssor:
// public attr()LOuter$attr$ = {
// if (attr$module == null) attr$lzycompute()
// else attr$module
// } and at the same time, the This bug could manifest itself: So, to fix this bug, the accessor method does not need to be synchronized.
|
@lrytz said: |
@axel22 said: Still, I believe that my comment about the |
Adam Pocock (craigacp) said: |
@lrytz said: |
The text was updated successfully, but these errors were encountered: