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

Enumeration.ValueSet should implement Serializable #5117

Closed
scabug opened this issue Oct 26, 2011 · 3 comments
Closed

Enumeration.ValueSet should implement Serializable #5117

scabug opened this issue Oct 26, 2011 · 3 comments
Assignees
Labels

Comments

@scabug
Copy link

@scabug scabug commented Oct 26, 2011

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

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Oct 26, 2011

Imported From: https://issues.scala-lang.org/browse/SI-5117?orig=1
Reporter: Brien Colwell (brien)
Affected Versions: 2.9.1
See #5211

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Nov 21, 2011

@soc said:
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.

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Dec 8, 2011

@szeiger said:
Fixed in 66b6ad4cacf69df95499b29f49d03a7e963a9eb9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.