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
Enumeration.ValueSet should implement Serializable #5117
Labels
Comments
Imported From: https://issues.scala-lang.org/browse/SI-5117?orig=1 |
@soc said: 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. |
@szeiger said: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
The text was updated successfully, but these errors were encountered: