Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

SI-8042 Use Serialization Proxy Pattern in List

Modify List to use the Serialization Proxy Pattern instead of
directly mutating its state during deserialization.

Use the proxy at the List level for both Nil and :: for simplicity.

Change one member variable (hd) to val from var as a result.  The
other member variable (tl) cannot yet be changed to val because
it is mutated by ListBuffer in the library and Types in reflection.
  • Loading branch information...
commit a5fc6e69e0c26ca8537e01d17109e1037600af76 1 parent 73cddba
Scott Carey authored
Showing with 34 additions and 24 deletions.
  1. +34 −24 src/library/scala/collection/immutable/List.scala
View
58 src/library/scala/collection/immutable/List.scala
@@ -322,6 +322,39 @@ sealed abstract class List[+A] extends AbstractSeq[A]
override def toStream : Stream[A] =
if (isEmpty) Stream.Empty
else new Stream.Cons(head, tail.toStream)
+
+ // Create a proxy for Java serialization that allows us to avoid mutation
+ // during de-serialization. This is the Serialization Proxy Pattern.
+ protected final def writeReplace(): AnyRef = new SerializationProxy(this)
+}
+
+@SerialVersionUID(1L)
+private class SerializationProxy[B](@transient private var orig: List[B]) extends Serializable {
+
+ private def writeObject(out: ObjectOutputStream) {
+ var xs: List[B] = orig
+ while (!xs.isEmpty) {
+ out.writeObject(xs.head)
+ xs = xs.tail
+ }
+ out.writeObject(ListSerializeEnd)
+ }
+
+ // Java serialization calls this before readResolve during de-serialization.
+ // Read the whole list and store it in `orig`.
+ private def readObject(in: ObjectInputStream) {
+ val builder = List.newBuilder[B]
+ while (true) in.readObject match {
+ case ListSerializeEnd =>
+ orig = builder.result()
+ return
+ case a =>
+ builder += a.asInstanceOf[B]
+ }
+ }
+
+ // Provide the result stored in `orig` for Java serialization
+ private def readResolve(): AnyRef = orig
}
/** The empty list.
@@ -352,33 +385,10 @@ case object Nil extends List[Nothing] {
* @version 1.0, 15/07/2003
* @since 2.8
*/
-@SerialVersionUID(0L - 8476791151983527571L)
-final case class ::[B](private var hd: B, private[scala] var tl: List[B]) extends List[B] {
+final case class ::[B](private val hd: B, private[scala] var tl: List[B]) extends List[B] {
override def head : B = hd
override def tail : List[B] = tl
override def isEmpty: Boolean = false
-
- private def readObject(in: ObjectInputStream) {
- val firstObject = in.readObject()
- hd = firstObject.asInstanceOf[B]
- assert(hd != ListSerializeEnd)
- var current: ::[B] = this
- while (true) in.readObject match {
- case ListSerializeEnd =>
- current.tl = Nil
- return
- case a =>
- val list : ::[B] = new ::(a.asInstanceOf[B], Nil)
- current.tl = list
- current = list
- }
- }
-
- private def writeObject(out: ObjectOutputStream) {
- var xs: List[B] = this
- while (!xs.isEmpty) { out.writeObject(xs.head); xs = xs.tail }
- out.writeObject(ListSerializeEnd)
- }
}
/** $factoryInfo
Please sign in to comment.
Something went wrong with that request. Please try again.