File: reflection-implClass.scala

package info (click to toggle)
scala 2.11.12-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 62,828 kB
  • sloc: javascript: 28,808; java: 13,415; xml: 3,250; sh: 1,620; python: 756; makefile: 38; awk: 36; ansic: 6
file content (40 lines) | stat: -rw-r--r-- 1,249 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
 * Tries to load a symbol for the `Foo$class` using Scala reflection.
 * Since trait implementation classes do not get pickling information
 * symbol for them should be created using fallback mechanism
 * that exposes Java reflection information dressed up in
 * a Scala symbol.
 */
object Test extends App with Outer {
  import scala.reflect.{ClassTag, classTag}
  import scala.reflect.runtime.universe._
  import scala.reflect.runtime.{currentMirror => cm}

  assert(cm.classSymbol(classTag[Foo].runtimeClass).info.decl(TermName("bar")).info ==
    cm.classSymbol(classTag[Bar].runtimeClass).info.decl(TermName("foo")).info)

  val s1 = implClass(classTag[Foo].runtimeClass)
  assert(s1 != NoSymbol)
  assert(s1.info != NoType)
  assert(s1.companion.info != NoType)
  assert(s1.companion.info.decl(TermName("bar")) != NoSymbol)
  val s2 = implClass(classTag[Bar].runtimeClass)
  assert(s2 != NoSymbol)
  assert(s2.info != NoType)
  assert(s2.companion.info != NoType)
  assert(s2.companion.info.decl(TermName("foo")) != NoSymbol)
  def implClass(clazz: Class[_]) = {
    val implClass = Class.forName(clazz.getName + "$class")
    cm.classSymbol(implClass)
  }
}

trait Foo {
  def bar = 1
}

trait Outer {
  trait Bar {
    def foo = 1
  }
}