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

Commit 43086fe

Browse files
committed
Adds support for classes defined in objects, fixes #187
1 parent dab70c0 commit 43086fe

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,11 @@ class Tools[C <: Context](val c: C) {
7979
val subclasses = MutableList[Symbol]()
8080
def analyze(sym: Symbol) = if (isRelevantSubclass(baseSym, sym)) subclasses += sym
8181
def loop(tree: Tree): Unit = tree match {
82-
// NOTE: only looking for top-level classes!
82+
// NOTE: only looking for classes defined in objects or top-level classes!
8383
case PackageDef(_, stats) => stats.foreach(loop)
8484
case cdef: ClassDef => analyze(cdef.symbol)
85-
case mdef: ModuleDef => analyze(mdef.symbol.asModule.moduleClass)
85+
case mdef: ModuleDef => // analyze(mdef.symbol.asModule.moduleClass)
86+
mdef.impl.body.foreach(loop)
8687
case _ => // do nothing
8788
}
8889
c.enclosingRun.units.map(_.body).foreach(loop)

core/src/test/scala/pickling/run/nested-class-in-object.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ object Commands {
1010
}
1111

1212
case class CommandMessage(cmd: Command)
13+
case class CommandMessage2(cmd: AnyRef)
1314

1415
class NestedObjectTest extends FunSuite {
1516
test("json") {
@@ -30,3 +31,23 @@ class NestedObjectTest extends FunSuite {
3031
assert(c.cmd == up.cmd)
3132
}
3233
}
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)