diff options
author | michelou <michelou@epfl.ch> | 2008-11-25 16:04:12 +0000 |
---|---|---|
committer | michelou <michelou@epfl.ch> | 2008-11-25 16:04:12 +0000 |
commit | 2d61f09332dbc6038f869c6a23a95dca1bc3b6c7 (patch) | |
tree | defa829dd7c0757a68b10b6b7b4235df2cb485de /src | |
parent | 6700e9988435eb981972754ecf86d11567457e4d (diff) | |
download | scala-2d61f09332dbc6038f869c6a23a95dca1bc3b6c7.tar.gz scala-2d61f09332dbc6038f869c6a23a95dca1bc3b6c7.tar.bz2 scala-2d61f09332dbc6038f869c6a23a95dca1bc3b6c7.zip |
added manifest tests and util.Marshal
Diffstat (limited to 'src')
-rw-r--r-- | src/compiler/scala/tools/nsc/ast/NodePrinters.scala | 16 | ||||
-rw-r--r-- | src/library/scala/reflect/Manifest.scala | 40 | ||||
-rw-r--r-- | src/library/scala/util/Marshal.scala | 44 |
3 files changed, 80 insertions, 20 deletions
diff --git a/src/compiler/scala/tools/nsc/ast/NodePrinters.scala b/src/compiler/scala/tools/nsc/ast/NodePrinters.scala index 42be088b07..57e60f9fba 100644 --- a/src/compiler/scala/tools/nsc/ast/NodePrinters.scala +++ b/src/compiler/scala/tools/nsc/ast/NodePrinters.scala @@ -1,5 +1,5 @@ /* NSC -- new Scala compiler - * Copyright 2005-2007 LAMP/EPFL + * Copyright 2005-2009 LAMP/EPFL * @author Martin Odersky */ // $Id$ @@ -41,7 +41,7 @@ abstract class NodePrinters { if (comma) buf.append(",") buf.append(EOL) } - def annotationInfoToString(attr: AnnotationInfo) = { + def annotationInfoToString(attr: AnnotationInfo): String = { val str = new StringBuilder str.append(attr.atp.toString()) if (!attr.args.isEmpty) @@ -123,7 +123,7 @@ abstract class NodePrinters { (if (buf.length() > 2) buf.substring(3) else "0") + ", // flags=" + flagsToString(sym.flags) + attrs } - def nodeinfo(tree: Tree) = + def nodeinfo(tree: Tree): String = if (infolevel == InfoLevel.Quiet) "" else { val buf = new StringBuilder(" // sym=" + tree.symbol) @@ -155,7 +155,7 @@ abstract class NodePrinters { } buf.toString } - def nodeinfo2(tree: Tree) = + def nodeinfo2(tree: Tree): String = (if (comma) "," else "") + nodeinfo(tree) tree match { case AppliedTypeTree(tpt, args) => @@ -200,13 +200,13 @@ abstract class NodePrinters { case Block(stats, expr) => println("Block(" + nodeinfo(tree)) if (stats.isEmpty) - println(" List() // no statement") + println(" List(), // no statement") else { val n = stats.length println(" List( // " + n + " statement(s)") for (i <- 0 until n) traverse(stats(i), level + 2, i < n-1) - println(" )") + println(" ),") } traverse(expr, level + 1, false) printcln(")") @@ -273,7 +273,9 @@ abstract class NodePrinters { printcln("Super(\"" + qual + "\", \"" + mix + "\")" + nodeinfo2(tree)) case Template(parents, self, body) => println("Template(" + nodeinfo(tree)) - println(" " + parents.map(p => if (p.tpe ne null) p.tpe.typeSymbol else "null-" + p) + ", // parents") + println(" " + parents.map(p => + if (p.tpe ne null) p.tpe.typeSymbol else "null-" + p + ) + ", // parents") traverse(self, level + 1, true) if (body.isEmpty) println(" List() // no body") diff --git a/src/library/scala/reflect/Manifest.scala b/src/library/scala/reflect/Manifest.scala index cac391670a..f4388fc30c 100644 --- a/src/library/scala/reflect/Manifest.scala +++ b/src/library/scala/reflect/Manifest.scala @@ -1,6 +1,6 @@ /* __ *\ ** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2007-2008, LAMP/EPFL ** +** / __/ __// _ | / / / _ | (c) 2007-2009, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** @@ -22,7 +22,8 @@ package scala.reflect * these operators should be on the unerased type. * </p> */ -@serializable trait Manifest[T] { +@serializable +trait Manifest[T] { /** A class representing the type U to which T would be erased. Note * that there is no subtyping relationship between T and U. */ @@ -75,7 +76,7 @@ object Manifest { /** Manifest for the singleton type `value.type'. */ def singleType[T](value: Any): Manifest[T] = - new Manifest[T] with java.io.Serializable { + new (Manifest[T] @serializable) { lazy val erasure = value match { case anyRefValue: AnyRef => anyRefValue.getClass @@ -87,7 +88,7 @@ object Manifest { /** Manifest for the class type `clazz', where `clazz' is * a top-level or static class. */ def classType[T](clazz: Predef.Class[T]): Manifest[T] = - new Manifest[T] with java.io.Serializable { + new (Manifest[T] @serializable) { val erasure = clazz override lazy val toString = erasure.getName } @@ -95,47 +96,60 @@ object Manifest { /** Manifest for the class type `clazz[args]', where `clazz' is * a top-level or static class. */ def classType[T](clazz: Predef.Class[_], args: Manifest[_]*): Manifest[T] = - new Manifest[T] with java.io.Serializable { + new (Manifest[T] @serializable) { val erasure = clazz val typeArguments: Seq[Manifest[_]] = args - override lazy val toString = erasure.getName + typeArguments.mkString("[", ", ", "]") + override def <:<(that: Manifest[_]): Boolean = { + try { + val meth = that.getClass().getMethod("typeArguments", null) + val args1 = meth.invoke(that, null).asInstanceOf[Array[Manifest[_]]] + super.<:<(that) && args.equalsWith(args1)((x, y) => x <:< y) + } catch { + case _ => false + } + } + override lazy val toString = + (if (erasure.isArray) "Array" else erasure.getName) + + typeArguments.mkString("[", ", ", "]") } /** Manifest for the class type `prefix # clazz'. */ def classType[T](prefix: Manifest[_], clazz: Predef.Class[_]): Manifest[T] = - new Manifest[T] with java.io.Serializable { + new (Manifest[T] @serializable) { val erasure = clazz override lazy val toString = prefix.toString + "#" + erasure.getName } /** Manifest for the class type `prefix # clazz[args]'. */ def classType[T](prefix: Manifest[_], clazz: Predef.Class[_], args: Manifest[_]*): Manifest[T] = - new Manifest[T] with java.io.Serializable { + new (Manifest[T] @serializable) { val erasure = clazz val typeArguments: Seq[Manifest[_]] = args - override lazy val toString = prefix.toString + "#" + erasure.getName + typeArguments.mkString("[", ", ", "]") + override lazy val toString = + prefix.toString + "#" + erasure.getName + typeArguments.mkString("[", ", ", "]") } /** Manifest for the abstract type `prefix # name'. `upperBound' is not * strictly necessary as it could be obtained by reflection. It was * added so that erasure can be calculated without reflection. */ def abstractType[T](prefix: Manifest[_], name: String, upperBound: Manifest[_]): Manifest[T] = - new Manifest[T] with java.io.Serializable { + new (Manifest[T] @serializable) { lazy val erasure = upperBound.erasure override lazy val toString = prefix.toString + "#" + name } /** Manifest for the abstract type `prefix # name[args]'. */ def abstractType[T](prefix: Manifest[_], name: String, upperBound: Manifest[_], args: Manifest[_]*): Manifest[T] = - new Manifest[T] with java.io.Serializable { + new (Manifest[T] @serializable) { lazy val erasure = upperBound.erasure val typeArguments: Seq[Manifest[_]] = args - override lazy val toString = prefix.toString + "#" + name + typeArguments.mkString("[", ", ", "]") + override lazy val toString = + prefix.toString + "#" + name + typeArguments.mkString("[", ", ", "]") } /** Manifest for the intersection type `parents_0 with ... with parents_n'. */ def intersectionType[T](parents: Manifest[_]*): Manifest[T] = - new Manifest[T] with java.io.Serializable { + new (Manifest[T] @serializable) { lazy val erasure = parents.first.erasure override lazy val toString = parents.mkString(" with ") } diff --git a/src/library/scala/util/Marshal.scala b/src/library/scala/util/Marshal.scala new file mode 100644 index 0000000000..9875a37c2a --- /dev/null +++ b/src/library/scala/util/Marshal.scala @@ -0,0 +1,44 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2008-2009, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id: $ + + +package scala.util + +/** + * Marshalling of Scala objects using Scala manifests. + * + * @author Stephane Micheloud + * @version 1.0 + */ +object Marshal { + import java.io._ + import scala.reflect.Manifest + + def dump[A](o: A)(implicit m: Manifest[A]): Array[Byte] = { + val ba = new ByteArrayOutputStream(512) + val out = new ObjectOutputStream(ba) + out.writeObject(m) + out.writeObject(o) + out.close() + ba.toByteArray() + } + + @throws(classOf[ClassCastException]) + def load[A](buffer: Array[Byte])(implicit expected: Manifest[A]): A = { + val in = new ObjectInputStream(new ByteArrayInputStream(buffer)) + val found = in.readObject.asInstanceOf[Manifest[_]] + if (! (found <:< expected)) + throw new ClassCastException("type mismatch;"+ + "\n found : "+found+ + "\n required: "+expected) + in.readObject.asInstanceOf[A] + } + +} |