Scala Programming Language
  1. Scala Programming Language
  2. SI-5117

Enumeration.ValueSet should implement Serializable

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: Scala 2.9.1
    • Fix Version/s: None
    • Component/s: Enumeration
    • Labels:
      None

      Description

      The Enumeration.ValueSet type should properly implement java.io.Serializable. For example, the A class below won't serialize correctly, while the B class will. It's difficult to spot this detail.

      object Types extends Enumeration

      { val X = Value val Y = Value }

      class A extends java.io.Serializable

      { val types = Types.values }

      class B extends java.io.Serializable

      { val types = Set(Types.A, Types.B) }

      // A will not serialize because it contains a ValueSet

      def serialize(obj: AnyRef) =

      { val baos = new java.io.ByteArrayOutputStream() val oos = new java.io.ObjectOutputStream(baos) oos.writeObject(obj) oos.close() val bais = new java.io.ByteArrayInputStream(baos.toByteArray) val ois = new java.io.ObjectInputStream(bais) val prime = ois.readObject() oid.close() prime }

      serialize(new B()) // passes
      serialize(new A()) // fails

        Issue Links

          Activity

          Hide
          Simon Ochsenreither added a comment -

          The code above does not even compile, because it is full of typos.

          The creator of this issue probably meant something like this:

          object Types extends Enumeration {
            val X = Value
            val Y = Value
          }
          class A extends java.io.Serializable { val types = Types.values }
          class B extends java.io.Serializable { val types = Set(Types.X, Types.Y) }
          
          // A will not serialize because it contains a ValueSet
          
          def serialize(obj: AnyRef) = {
            val baos = new java.io.ByteArrayOutputStream()
            val oos = new java.io.ObjectOutputStream(baos)
            oos.writeObject(obj)
            oos.close()
            val bais = new java.io.ByteArrayInputStream(baos.toByteArray)
            val ois = new java.io.ObjectInputStream(bais)
            val prime = ois.readObject()
            ois.close()
            prime
          }
          
          serialize(new B()) // passes
          serialize(new A()) // fails
          

          Fails with:

          scala> serialize(new B()) // passes
          res3: Object = B@2326cd28
          
          scala> serialize(new A()) // fails
          java.io.NotSerializableException: scala.Enumeration$ValueSet
          	at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
          	at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528)
          	at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
          	at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
          	at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
          	at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
          	at .serialize(<console>:10)
          	at .<init>(<console>:11)
          	at .<clinit>(<console>)
          	at .<init>(<console>:11)
          	at .<clinit>(<console>)
          	at $print(<console>)
          	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
          	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
          	at java.lang.reflect.Method.invoke(Method.java:601)
          	at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:751)
          	at scala.tools.nsc.interpreter.IMain$Request$$anonfun$13.apply(IMain.scala:992)
          	at scala.tools.nsc.interpreter.Line.scala$tools$nsc$interpreter$Line$$runAndSetState(Line.scala:41)
          	at scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:47)
          	at scala.tools.nsc.io.package$$anon$2.run(package.scala:19)
          	at java.lang.Thread.run(Thread.java:722)
          

          Verified on 2.10.0.r26005-b20111114020239.

          Show
          Simon Ochsenreither added a comment - The code above does not even compile, because it is full of typos. The creator of this issue probably meant something like this: object Types extends Enumeration { val X = Value val Y = Value } class A extends java.io.Serializable { val types = Types.values } class B extends java.io.Serializable { val types = Set(Types.X, Types.Y) } // A will not serialize because it contains a ValueSet def serialize(obj: AnyRef) = { val baos = new java.io.ByteArrayOutputStream() val oos = new java.io.ObjectOutputStream(baos) oos.writeObject(obj) oos.close() val bais = new java.io.ByteArrayInputStream(baos.toByteArray) val ois = new java.io.ObjectInputStream(bais) val prime = ois.readObject() ois.close() prime } serialize(new B()) // passes serialize(new A()) // fails Fails with: scala> serialize(new B()) // passes res3: Object = B@2326cd28 scala> serialize(new A()) // fails java.io.NotSerializableException: scala.Enumeration$ValueSet at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180) at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528) at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493) at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416) at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346) at .serialize(<console>:10) at .<init>(<console>:11) at .<clinit>(<console>) at .<init>(<console>:11) at .<clinit>(<console>) at $print(<console>) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:751) at scala.tools.nsc.interpreter.IMain$Request$$anonfun$13.apply(IMain.scala:992) at scala.tools.nsc.interpreter.Line.scala$tools$nsc$interpreter$Line$$runAndSetState(Line.scala:41) at scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:47) at scala.tools.nsc.io.package$$anon$2.run(package.scala:19) at java.lang.Thread.run(Thread.java:722) Verified on 2.10.0.r26005-b20111114020239.
          Hide
          Stefan Zeiger added a comment -

          Fixed in 66b6ad4cacf69df95499b29f49d03a7e963a9eb9

          Show
          Stefan Zeiger added a comment - Fixed in 66b6ad4cacf69df95499b29f49d03a7e963a9eb9

            People

            • Assignee:
              Stefan Zeiger
              Reporter:
              Brien Colwell
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development