diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2016-07-14 11:02:36 +1000 |
---|---|---|
committer | Jason Zaugg <jzaugg@gmail.com> | 2016-07-22 10:29:02 +1000 |
commit | 3e64fdda48b8f2506756fc458f01f2e549d71720 (patch) | |
tree | 2c06b2a2f272dfa4be387aea6cf82e5d1cd4378e /test | |
parent | 6b99dfe746cbb76828db495b58e55db2a0265ee2 (diff) | |
download | scala-3e64fdda48b8f2506756fc458f01f2e549d71720.tar.gz scala-3e64fdda48b8f2506756fc458f01f2e549d71720.tar.bz2 scala-3e64fdda48b8f2506756fc458f01f2e549d71720.zip |
SD-120 Non FunctionN lambdas should not be universally serializable
Instead, we follow the example set by javac, and predicate serializability
of bot anon-class and invokedynamic-based lambdas on whether or not the
SAM type extends java.io.Serializable.
Fixes https://github.com/scala/scala-dev/issues/120
Diffstat (limited to 'test')
-rw-r--r-- | test/files/run/lambda-serialization.scala | 2 | ||||
-rw-r--r-- | test/files/run/sammy_seriazable.scala | 47 |
2 files changed, 48 insertions, 1 deletions
diff --git a/test/files/run/lambda-serialization.scala b/test/files/run/lambda-serialization.scala index 0eee1193d7..08e235b1cb 100644 --- a/test/files/run/lambda-serialization.scala +++ b/test/files/run/lambda-serialization.scala @@ -1,6 +1,6 @@ import java.io.{ByteArrayInputStream, ObjectInputStream, ObjectOutputStream, ByteArrayOutputStream} -trait IntToString { def apply(i: Int): String } +trait IntToString extends java.io.Serializable { def apply(i: Int): String } object Test { def main(args: Array[String]): Unit = { diff --git a/test/files/run/sammy_seriazable.scala b/test/files/run/sammy_seriazable.scala new file mode 100644 index 0000000000..458b99238a --- /dev/null +++ b/test/files/run/sammy_seriazable.scala @@ -0,0 +1,47 @@ +import java.io._ + +trait NotSerializableInterface { def apply(a: Any): Any } +abstract class NotSerializableClass { def apply(a: Any): Any } +// SAM type that supports lambdas-as-invoke-dynamic +trait IsSerializableInterface extends java.io.Serializable { def apply(a: Any): Any } +// SAM type that still requires lambdas-as-anonhmous-classes +abstract class IsSerializableClass extends java.io.Serializable { def apply(a: Any): Any } + +object Test { + def main(args: Array[String]) { + val nsi: NotSerializableInterface = x => x + val nsc: NotSerializableClass = x => x + + import SerDes._ + assertNotSerializable(nsi) + assertNotSerializable(nsc) + assert(serializeDeserialize[IsSerializableInterface](x => x).apply("foo") == "foo") + assert(serializeDeserialize[IsSerializableClass](x => x).apply("foo") == "foo") + assert(ObjectStreamClass.lookup(((x => x): IsSerializableClass).getClass).getSerialVersionUID == 0) + } +} + +object SerDes { + def assertNotSerializable(a: AnyRef): Unit = { + try { + serialize(a) + assert(false) + } catch { + case _: NotSerializableException => // okay + } + } + + def serialize(obj: AnyRef): Array[Byte] = { + val buffer = new ByteArrayOutputStream + val out = new ObjectOutputStream(buffer) + out.writeObject(obj) + buffer.toByteArray + } + + def deserialize(a: Array[Byte]): AnyRef = { + val in = new ObjectInputStream(new ByteArrayInputStream(a)) + in.readObject + } + + def serializeDeserialize[T <: AnyRef](obj: T) = deserialize(serialize(obj)).asInstanceOf[T] +} |