summaryrefslogtreecommitdiff
path: root/src/library
diff options
context:
space:
mode:
authormichelou <michelou@epfl.ch>2008-11-25 16:04:12 +0000
committermichelou <michelou@epfl.ch>2008-11-25 16:04:12 +0000
commit2d61f09332dbc6038f869c6a23a95dca1bc3b6c7 (patch)
treedefa829dd7c0757a68b10b6b7b4235df2cb485de /src/library
parent6700e9988435eb981972754ecf86d11567457e4d (diff)
downloadscala-2d61f09332dbc6038f869c6a23a95dca1bc3b6c7.tar.gz
scala-2d61f09332dbc6038f869c6a23a95dca1bc3b6c7.tar.bz2
scala-2d61f09332dbc6038f869c6a23a95dca1bc3b6c7.zip
added manifest tests and util.Marshal
Diffstat (limited to 'src/library')
-rw-r--r--src/library/scala/reflect/Manifest.scala40
-rw-r--r--src/library/scala/util/Marshal.scala44
2 files changed, 71 insertions, 13 deletions
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]
+ }
+
+}