summaryrefslogtreecommitdiff
path: root/sources
diff options
context:
space:
mode:
authormihaylov <mihaylov@epfl.ch>2005-12-13 11:38:26 +0000
committermihaylov <mihaylov@epfl.ch>2005-12-13 11:38:26 +0000
commit95dbf1955feb2a2b6a48b81aecabb5308b9767b4 (patch)
tree81ed9aea30c9eafa2fc8fa2d8b63e368164b266d /sources
parent6ff6a40689b503f0e285dcd41ee8429c22cc3e21 (diff)
downloadscala-95dbf1955feb2a2b6a48b81aecabb5308b9767b4.tar.gz
scala-95dbf1955feb2a2b6a48b81aecabb5308b9767b4.tar.bz2
scala-95dbf1955feb2a2b6a48b81aecabb5308b9767b4.zip
- make every case class/object serializable
- create readResolve() for every serializable object that doesn't declare it (instead of only for case objects)
Diffstat (limited to 'sources')
-rwxr-xr-xsources/scala/tools/nsc/typechecker/SyntheticMethods.scala36
1 files changed, 26 insertions, 10 deletions
diff --git a/sources/scala/tools/nsc/typechecker/SyntheticMethods.scala b/sources/scala/tools/nsc/typechecker/SyntheticMethods.scala
index 3fef013f18..da6ac07d6c 100755
--- a/sources/scala/tools/nsc/typechecker/SyntheticMethods.scala
+++ b/sources/scala/tools/nsc/typechecker/SyntheticMethods.scala
@@ -31,8 +31,11 @@ import util.ListBuffer;
!(ObjectClass isSubClass sym.owner) && !(sym hasFlag DEFERRED)))
}
- def syntheticMethod(name: Name, flags: int, tpe: Type) = {
- val method = clazz.newMethod(clazz.pos, name) setFlag (flags | OVERRIDE) setInfo tpe;
+ def syntheticMethod(name: Name, flags: int, tpe: Type) =
+ newSyntheticMethod(name, flags | OVERRIDE, tpe);
+
+ def newSyntheticMethod(name: Name, flags: int, tpe: Type) = {
+ val method = clazz.newMethod(clazz.pos, name) setFlag (flags) setInfo tpe;
clazz.info.decls.enter(method);
method
}
@@ -82,25 +85,31 @@ import util.ListBuffer;
Apply(gen.mkRef(target), This(clazz) :: (vparamss.head map Ident))));
}
+ val SerializableAttr = definitions.SerializableAttr;
+
+ def isSerializable(clazz: Symbol): Boolean = {
+ clazz.attributes.exists(p => p match {
+ case Pair(SerializableAttr, _) => true;
+ case _ => false
+ })
+ }
+
def readResolveMethod: Tree = {
// !!! the synthetic method "readResolve" should be private,
// but then it is renamed !!!
- val method = syntheticMethod(nme.readResolve, PROTECTED, MethodType(List(), ObjectClass.tpe));
+ val method = newSyntheticMethod(nme.readResolve, PROTECTED,
+ MethodType(List(), ObjectClass.tpe));
typed(DefDef(method, vparamss => gen.mkRef(clazz.sourceModule)))
}
val ts = new ListBuffer[Tree];
if ((clazz hasFlag CASE) && !phase.erasedTypes) {
+ // case classes are implicitly declared serializable
+ clazz.attributes = Pair(SerializableAttr, List()) :: clazz.attributes;
+
ts += tagMethod;
if (clazz.isModuleClass) {
if (!hasImplementation(nme.toString_)) ts += moduleToStringMethod;
- if (clazz.isSubClass(SerializableClass)) {
- // If you serialize a singleton and then deserialize it twice,
- // you will have two instances of your singleton, unless you implement
- // the readResolve() method (see http://www.javaworld.com/javaworld/
- // jw-04-2003/jw-0425-designpatterns_p.html)
- if (!hasImplementation(nme.readResolve)) ts += readResolveMethod;
- }
} else {
if (!hasImplementation(nme.hashCode_)) ts += forwardingMethod(nme.hashCode_);
if (!hasImplementation(nme.toString_)) ts += forwardingMethod(nme.toString_);
@@ -110,6 +119,13 @@ import util.ListBuffer;
if (!hasImplementation(nme.caseArity)) ts += caseArityMethod;
if (!hasImplementation(nme.caseName)) ts += caseNameMethod;
}
+ if (!phase.erasedTypes && clazz.isModuleClass && isSerializable(clazz)) {
+ // If you serialize a singleton and then deserialize it twice,
+ // you will have two instances of your singleton, unless you implement
+ // the readResolve() method (see http://www.javaworld.com/javaworld/
+ // jw-04-2003/jw-0425-designpatterns_p.html)
+ if (!hasImplementation(nme.readResolve)) ts += readResolveMethod;
+ }
val synthetics = ts.toList;
copy.Template(
templ, templ.parents, if (synthetics.isEmpty) templ.body else templ.body ::: synthetics)