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

Commit 1e3a6d3

Browse files
committed
Merge pull request #188 from scala/issue-187
Adds support for classes nested in objects, fixes #187
2 parents 6935cb2 + aedd4e2 commit 1e3a6d3

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

core/src/main/scala/pickling/FastTags.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,12 @@ object FastTypeTag {
164164
// debug(s"!!! could not find primitive tag for class ${clazz.getName} !!!")
165165
// handle arrays of non-primitive element type
166166
if (clazz.isArray) mkRawArray(clazz, mirror)
167-
else apply(mirror, clazz.getName())
167+
else {
168+
val clazzName =
169+
if (clazz.getName().contains("anonfun$")) clazz.getName()
170+
else clazz.getName().replace('$', '.')
171+
apply(mirror, clazzName)
172+
}
168173
})
169174
} catch {
170175
case t: Throwable =>

core/src/main/scala/pickling/Tools.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ class Tools[C <: Context](val c: C) {
7575
val subclasses = MutableList[Symbol]()
7676
def analyze(sym: Symbol) = if (isRelevantSubclass(baseSym, sym)) subclasses += sym
7777
def loop(tree: Tree): Unit = tree match {
78-
// NOTE: only looking for top-level classes!
78+
// NOTE: only looking for classes defined in objects or top-level classes!
7979
case PackageDef(_, stats) => stats.foreach(loop)
8080
case cdef: ClassDef => analyze(cdef.symbol)
81-
case mdef: ModuleDef => analyze(mdef.symbol.asModule.moduleClass)
81+
case mdef: ModuleDef => mdef.impl.body.foreach(loop)
8282
case _ => // do nothing
8383
}
8484
c.enclosingRun.units.map(_.body).foreach(loop)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package scala.pickling.privatepublicctorstest
2+
3+
import org.scalatest.FunSuite
4+
import scala.pickling._
5+
6+
trait Command
7+
8+
object Commands {
9+
case class SomeCommand(x: String) extends Command
10+
}
11+
12+
case class CommandMessage(cmd: Command)
13+
case class CommandMessage2(cmd: AnyRef)
14+
15+
class NestedObjectTest extends FunSuite {
16+
test("json") {
17+
import json._
18+
val cmd = Commands.SomeCommand("hey, this is a command!")
19+
val c = CommandMessage(cmd)
20+
val p: JSONPickle = c.pickle
21+
val up = p.unpickle[CommandMessage]
22+
assert(c.cmd == up.cmd)
23+
}
24+
25+
test("binary") {
26+
import binary._
27+
val cmd = Commands.SomeCommand("hey, this is a command!")
28+
val c = CommandMessage(cmd)
29+
val p: BinaryPickle = c.pickle
30+
val up = p.unpickle[CommandMessage]
31+
assert(c.cmd == up.cmd)
32+
}
33+
}
34+
35+
class NestedObjectRuntimeTest extends FunSuite {
36+
test("json") {
37+
import json._
38+
val cmd = Commands.SomeCommand("hey, this is a command!")
39+
val c = CommandMessage2(cmd)
40+
val p: JSONPickle = c.pickle
41+
val up = p.unpickle[CommandMessage2]
42+
assert(c.cmd == up.cmd)
43+
}
44+
45+
test("binary") {
46+
import binary._
47+
val cmd = Commands.SomeCommand("hey, this is a command!")
48+
val c = CommandMessage2(cmd)
49+
val p: BinaryPickle = c.pickle
50+
val up = p.unpickle[CommandMessage2]
51+
assert(c.cmd == up.cmd)
52+
}
53+
}

0 commit comments

Comments
 (0)