Skip to content
This repository was archived by the owner on Feb 23, 2018. It is now read-only.

Commit def8296

Browse files
author
michelou
committed
added manifest tests and util.Marshal
git-svn-id: http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk@16625 5e8d7ff9-d8ef-0310-90f0-a4852d11357a
1 parent ae02e44 commit def8296

File tree

5 files changed

+191
-42
lines changed

5 files changed

+191
-42
lines changed

src/compiler/scala/tools/nsc/ast/NodePrinters.scala

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* NSC -- new Scala compiler
2-
* Copyright 2005-2007 LAMP/EPFL
2+
* Copyright 2005-2009 LAMP/EPFL
33
* @author Martin Odersky
44
*/
55
// $Id$
@@ -41,7 +41,7 @@ abstract class NodePrinters {
4141
if (comma) buf.append(",")
4242
buf.append(EOL)
4343
}
44-
def annotationInfoToString(attr: AnnotationInfo) = {
44+
def annotationInfoToString(attr: AnnotationInfo): String = {
4545
val str = new StringBuilder
4646
str.append(attr.atp.toString())
4747
if (!attr.args.isEmpty)
@@ -123,7 +123,7 @@ abstract class NodePrinters {
123123
(if (buf.length() > 2) buf.substring(3)
124124
else "0") + ", // flags=" + flagsToString(sym.flags) + attrs
125125
}
126-
def nodeinfo(tree: Tree) =
126+
def nodeinfo(tree: Tree): String =
127127
if (infolevel == InfoLevel.Quiet) ""
128128
else {
129129
val buf = new StringBuilder(" // sym=" + tree.symbol)
@@ -155,7 +155,7 @@ abstract class NodePrinters {
155155
}
156156
buf.toString
157157
}
158-
def nodeinfo2(tree: Tree) =
158+
def nodeinfo2(tree: Tree): String =
159159
(if (comma) "," else "") + nodeinfo(tree)
160160
tree match {
161161
case AppliedTypeTree(tpt, args) =>
@@ -200,13 +200,13 @@ abstract class NodePrinters {
200200
case Block(stats, expr) =>
201201
println("Block(" + nodeinfo(tree))
202202
if (stats.isEmpty)
203-
println(" List() // no statement")
203+
println(" List(), // no statement")
204204
else {
205205
val n = stats.length
206206
println(" List( // " + n + " statement(s)")
207207
for (i <- 0 until n)
208208
traverse(stats(i), level + 2, i < n-1)
209-
println(" )")
209+
println(" ),")
210210
}
211211
traverse(expr, level + 1, false)
212212
printcln(")")
@@ -273,7 +273,9 @@ abstract class NodePrinters {
273273
printcln("Super(\"" + qual + "\", \"" + mix + "\")" + nodeinfo2(tree))
274274
case Template(parents, self, body) =>
275275
println("Template(" + nodeinfo(tree))
276-
println(" " + parents.map(p => if (p.tpe ne null) p.tpe.typeSymbol else "null-" + p) + ", // parents")
276+
println(" " + parents.map(p =>
277+
if (p.tpe ne null) p.tpe.typeSymbol else "null-" + p
278+
) + ", // parents")
277279
traverse(self, level + 1, true)
278280
if (body.isEmpty)
279281
println(" List() // no body")

src/library/scala/reflect/Manifest.scala

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* __ *\
22
** ________ ___ / / ___ Scala API **
3-
** / __/ __// _ | / / / _ | (c) 2007-2008, LAMP/EPFL **
3+
** / __/ __// _ | / / / _ | (c) 2007-2009, LAMP/EPFL **
44
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
55
** /____/\___/_/ |_/____/_/ | | **
66
** |/ **
@@ -22,7 +22,8 @@ package scala.reflect
2222
* these operators should be on the unerased type.
2323
* </p>
2424
*/
25-
@serializable trait Manifest[T] {
25+
@serializable
26+
trait Manifest[T] {
2627

2728
/** A class representing the type U to which T would be erased. Note
2829
* that there is no subtyping relationship between T and U. */
@@ -75,7 +76,7 @@ object Manifest {
7576

7677
/** Manifest for the singleton type `value.type'. */
7778
def singleType[T](value: Any): Manifest[T] =
78-
new Manifest[T] with java.io.Serializable {
79+
new (Manifest[T] @serializable) {
7980
lazy val erasure =
8081
value match {
8182
case anyRefValue: AnyRef => anyRefValue.getClass
@@ -87,55 +88,68 @@ object Manifest {
8788
/** Manifest for the class type `clazz', where `clazz' is
8889
* a top-level or static class. */
8990
def classType[T](clazz: Predef.Class[T]): Manifest[T] =
90-
new Manifest[T] with java.io.Serializable {
91+
new (Manifest[T] @serializable) {
9192
val erasure = clazz
9293
override lazy val toString = erasure.getName
9394
}
9495

9596
/** Manifest for the class type `clazz[args]', where `clazz' is
9697
* a top-level or static class. */
9798
def classType[T](clazz: Predef.Class[_], args: Manifest[_]*): Manifest[T] =
98-
new Manifest[T] with java.io.Serializable {
99+
new (Manifest[T] @serializable) {
99100
val erasure = clazz
100101
val typeArguments: Seq[Manifest[_]] = args
101-
override lazy val toString = erasure.getName + typeArguments.mkString("[", ", ", "]")
102+
override def <:<(that: Manifest[_]): Boolean = {
103+
try {
104+
val meth = that.getClass().getMethod("typeArguments", null)
105+
val args1 = meth.invoke(that, null).asInstanceOf[Array[Manifest[_]]]
106+
super.<:<(that) && args.equalsWith(args1)((x, y) => x <:< y)
107+
} catch {
108+
case _ => false
109+
}
110+
}
111+
override lazy val toString =
112+
(if (erasure.isArray) "Array" else erasure.getName) +
113+
typeArguments.mkString("[", ", ", "]")
102114
}
103115

104116
/** Manifest for the class type `prefix # clazz'. */
105117
def classType[T](prefix: Manifest[_], clazz: Predef.Class[_]): Manifest[T] =
106-
new Manifest[T] with java.io.Serializable {
118+
new (Manifest[T] @serializable) {
107119
val erasure = clazz
108120
override lazy val toString = prefix.toString + "#" + erasure.getName
109121
}
110122

111123
/** Manifest for the class type `prefix # clazz[args]'. */
112124
def classType[T](prefix: Manifest[_], clazz: Predef.Class[_], args: Manifest[_]*): Manifest[T] =
113-
new Manifest[T] with java.io.Serializable {
125+
new (Manifest[T] @serializable) {
114126
val erasure = clazz
115127
val typeArguments: Seq[Manifest[_]] = args
116-
override lazy val toString = prefix.toString + "#" + erasure.getName + typeArguments.mkString("[", ", ", "]")
128+
override lazy val toString =
129+
prefix.toString + "#" + erasure.getName + typeArguments.mkString("[", ", ", "]")
117130
}
118131

119132
/** Manifest for the abstract type `prefix # name'. `upperBound' is not
120133
* strictly necessary as it could be obtained by reflection. It was
121134
* added so that erasure can be calculated without reflection. */
122135
def abstractType[T](prefix: Manifest[_], name: String, upperBound: Manifest[_]): Manifest[T] =
123-
new Manifest[T] with java.io.Serializable {
136+
new (Manifest[T] @serializable) {
124137
lazy val erasure = upperBound.erasure
125138
override lazy val toString = prefix.toString + "#" + name
126139
}
127140

128141
/** Manifest for the abstract type `prefix # name[args]'. */
129142
def abstractType[T](prefix: Manifest[_], name: String, upperBound: Manifest[_], args: Manifest[_]*): Manifest[T] =
130-
new Manifest[T] with java.io.Serializable {
143+
new (Manifest[T] @serializable) {
131144
lazy val erasure = upperBound.erasure
132145
val typeArguments: Seq[Manifest[_]] = args
133-
override lazy val toString = prefix.toString + "#" + name + typeArguments.mkString("[", ", ", "]")
146+
override lazy val toString =
147+
prefix.toString + "#" + name + typeArguments.mkString("[", ", ", "]")
134148
}
135149

136150
/** Manifest for the intersection type `parents_0 with ... with parents_n'. */
137151
def intersectionType[T](parents: Manifest[_]*): Manifest[T] =
138-
new Manifest[T] with java.io.Serializable {
152+
new (Manifest[T] @serializable) {
139153
lazy val erasure = parents.first.erasure
140154
override lazy val toString = parents.mkString(" with ")
141155
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* __ *\
2+
** ________ ___ / / ___ Scala API **
3+
** / __/ __// _ | / / / _ | (c) 2008-2009, LAMP/EPFL **
4+
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
5+
** /____/\___/_/ |_/____/_/ | | **
6+
** |/ **
7+
\* */
8+
9+
// $Id: $
10+
11+
12+
package scala.util
13+
14+
/**
15+
* Marshalling of Scala objects using Scala manifests.
16+
*
17+
* @author Stephane Micheloud
18+
* @version 1.0
19+
*/
20+
object Marshal {
21+
import java.io._
22+
import scala.reflect.Manifest
23+
24+
def dump[A](o: A)(implicit m: Manifest[A]): Array[Byte] = {
25+
val ba = new ByteArrayOutputStream(512)
26+
val out = new ObjectOutputStream(ba)
27+
out.writeObject(m)
28+
out.writeObject(o)
29+
out.close()
30+
ba.toByteArray()
31+
}
32+
33+
@throws(classOf[ClassCastException])
34+
def load[A](buffer: Array[Byte])(implicit expected: Manifest[A]): A = {
35+
val in = new ObjectInputStream(new ByteArrayInputStream(buffer))
36+
val found = in.readObject.asInstanceOf[Manifest[_]]
37+
if (! (found <:< expected))
38+
throw new ClassCastException("type mismatch;"+
39+
"\n found : "+found+
40+
"\n required: "+expected)
41+
in.readObject.asInstanceOf[A]
42+
}
43+
44+
}

test/files/jvm/manifests.check

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,57 @@ x=true, m=boolean
33
x=a, m=char
44
x=1, m=int
55
x=abc, m=java.lang.String
6+
x='abc, m=scala.Symbol
7+
68
x=List(()), m=scala.List[void]
79
x=List(true), m=scala.List[boolean]
810
x=List(1), m=scala.List[int]
911
x=List(abc), m=scala.List[java.lang.String]
10-
x=[Z, m=[Z[boolean]
11-
x=[C, m=[C[char]
12-
x=[I, m=[I[int]
13-
x=Array(abc), m=[Ljava.lang.String;[java.lang.String]
12+
x=List('abc), m=scala.List[scala.Symbol]
13+
14+
x=[Z, m=Array[boolean]
15+
x=[C, m=Array[char]
16+
x=[I, m=Array[int]
17+
x=Array(abc), m=Array[java.lang.String]
18+
x=Array('abc), m=Array[scala.Symbol]
19+
1420
x=((),()), m=scala.Tuple2[void, void]
1521
x=(true,false), m=scala.Tuple2[boolean, boolean]
1622
x=(1,2), m=scala.Tuple2[int, int]
1723
x=(abc,xyz), m=scala.Tuple2[java.lang.String, java.lang.String]
18-
x=Serialize$, m=Serialize$
24+
x=('abc,'xyz), m=scala.Tuple2[scala.Symbol, scala.Symbol]
25+
1926
x=Test$, m=Test$
2027
x=scala.List$, m=scala.List$
21-
x=Test$Foo, m=Test$Foo[int]
22-
x=Test$Foo, m=Test$Foo[scala.List[int]]
23-
x=Test$Foo, m=Test$Foo[Test$Foo[int]]
24-
x=Test$Foo, m=Test$Foo[scala.List[Test$Foo[int]]]
25-
x=Test$$anon$1, m=Test$$anon$1
28+
29+
x=Foo, m=Foo[int]
30+
x=Foo, m=Foo[scala.List[int]]
31+
x=Foo, m=Foo[Foo[int]]
32+
x=Foo, m=Foo[scala.List[Foo[int]]]
33+
34+
x=Test1$$anon$1, m=Test1$$anon$1
35+
36+
()=()
37+
true=true
38+
a=a
39+
1=1
40+
'abc='abc
41+
42+
List(())=List(())
43+
List(true)=List(true)
44+
List('abc)=List('abc)
45+
46+
Array()=Array()
47+
Array(true)=Array(true)
48+
Array(a)=Array(a)
49+
Array(1)=Array(1)
50+
51+
((),())=((),())
52+
(true,false)=(true,false)
53+
54+
List(List(1), List(2))=List(List(1), List(2))
55+
56+
Array(Array(1), Array(2))=Array(Array(1), Array(2))
57+
58+
x=char, m=scala.reflect.Manifest[char]
59+

0 commit comments

Comments
 (0)