From 5762110d0dc2cb492e34d5595c473aa0f9ca786a Mon Sep 17 00:00:00 2001 From: Philipp Haller Date: Wed, 9 Jul 2014 14:43:12 +0200 Subject: SI-5919 TypeTags and Exprs should be serializable - Make TypeCreator and TreeCreator extend Serializable. - When replacing a SerializedTypeTag with a TypeTag or WeakTypeTag, do not use scala.reflect.runtime.universe.rootMirror, since it is unlikely to find user classes; instead, create a runtime mirror using the context ClassLoader of the current thread. Use the same logic for SerializedExpr. - Remove writeObject/readObject methods from SerializedTypeTag and SerializedExpr since they are unused. - Add @throws annotation on writeReplace and readResolve methods. - Handle SecurityException if the current thread cannot access the context ClassLoader. - To make type tags of primitive value classes serializable, make PredefTypeCreator a top-level class. Otherwise, it would retain a reference to the enclosing Universe, rendering the TypeCreator non-serializable. Binary compatibility: - Keep nested PredefTypeCreator class to avoid backward binary incompatible change. - Keep `var` modifiers on the class parameters of SerializedTypeTag for backward binary compatibility. - Adds filter rules to forward binary compatibility whitelist: - `TypeCreator`, `PredefTypeCreator`, and `TreeCreator` must now extend from `Serializable`. - Must have new class `scala.reflect.api.PredefTypeCreator` to avoid problematic outer reference. --- test/files/run/abstypetags_serialize.check | 4 ++-- test/files/run/exprs_serialize.check | 21 +++++++++++++++++++-- test/files/run/exprs_serialize.scala | 12 +++++++++++- test/files/run/typetags_serialize.check | 5 +++-- test/files/run/typetags_serialize.scala | 5 +++++ 5 files changed, 40 insertions(+), 7 deletions(-) (limited to 'test/files') diff --git a/test/files/run/abstypetags_serialize.check b/test/files/run/abstypetags_serialize.check index bddc4523e6..1b5e2ebddf 100644 --- a/test/files/run/abstypetags_serialize.check +++ b/test/files/run/abstypetags_serialize.check @@ -1,2 +1,2 @@ -java.io.NotSerializableException: Test$$typecreator1$1 -java.io.NotSerializableException: Test$$typecreator2$1 +WeakTypeTag[T] +WeakTypeTag[U[String]] diff --git a/test/files/run/exprs_serialize.check b/test/files/run/exprs_serialize.check index 20ad6c110c..551823ccdc 100644 --- a/test/files/run/exprs_serialize.check +++ b/test/files/run/exprs_serialize.check @@ -1,2 +1,19 @@ -java.io.NotSerializableException: Test$$treecreator1$1 -java.io.NotSerializableException: Test$$treecreator2$1 +Expr[Int(2)](2) +Expr[java.lang.String]({ + def foo = "hello"; + foo.$plus("world!") +}) +Expr[Boolean]({ + def foo(x: Int) = { + class Local extends AnyRef { + def () = { + super.(); + () + }; + val f = 2 + }; + val obj = new Local(); + x.$percent(obj.f).$eq$eq(0) + }; + foo(5) +}) diff --git a/test/files/run/exprs_serialize.scala b/test/files/run/exprs_serialize.scala index c4310b0fe1..91027803b4 100644 --- a/test/files/run/exprs_serialize.scala +++ b/test/files/run/exprs_serialize.scala @@ -26,4 +26,14 @@ object Test extends App { test(reify(2)) test(reify{def foo = "hello"; foo + "world!"}) -} \ No newline at end of file + test(reify { + def foo(x: Int) = { + class Local { + val f = 2 + } + val obj = new Local + x % obj.f == 0 + } + foo(5) + }) +} diff --git a/test/files/run/typetags_serialize.check b/test/files/run/typetags_serialize.check index f79436ea5d..22928a2e94 100644 --- a/test/files/run/typetags_serialize.check +++ b/test/files/run/typetags_serialize.check @@ -1,2 +1,3 @@ -java.io.NotSerializableException: scala.reflect.api.TypeTags$PredefTypeCreator -java.io.NotSerializableException: Test$$typecreator1$1 +TypeTag[Int] +TypeTag[String] +TypeTag[Test.C[Double]] diff --git a/test/files/run/typetags_serialize.scala b/test/files/run/typetags_serialize.scala index 3c842e6cc9..a7a7845232 100644 --- a/test/files/run/typetags_serialize.scala +++ b/test/files/run/typetags_serialize.scala @@ -4,6 +4,10 @@ import scala.reflect.runtime.{universe => ru} import scala.reflect.runtime.{currentMirror => cm} object Test extends App { + class C[A] { + def m(a: A): Int = 5 + } + def test(tag: TypeTag[_]) = try { val fout = new ByteArrayOutputStream() @@ -26,4 +30,5 @@ object Test extends App { test(implicitly[TypeTag[Int]]) test(implicitly[TypeTag[String]]) + test(implicitly[TypeTag[C[Double]]]) } \ No newline at end of file -- cgit v1.2.3