blob: 9c726e55d4cbb01e553f86f584f0991f41b8db2b (
plain) (
blame)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
import scala.reflect.runtime.universe._
import scala.reflect.runtime.{currentMirror => cm}
import scala.reflect.{classTag, ClassTag}
object B {
class BB {
class B1 { override def toString = "B1"; def foo = 1 }
private class B2 { override def toString = "B2"; def foo = 2 }
object B3 { override def toString = "B3"; def foo = 3 }
private object B4 { override def toString = "B4"; def foo = 4 }
object B5 extends B1 { override def toString = "B5"; override def foo = 5 }
private object B6 extends B2 { override def toString = "B6"; override def foo = 6 }
}
}
object Test extends App {
val b = cm.classSymbol(classTag[B.BB].runtimeClass)
println(b)
println(b.typeSignature.declarations.toList)
def testMethodInvocation(instance: Any) = {
val instanceMirror = cm.reflect(instance)
val method = instanceMirror.symbol.typeSignature.declaration(TermName("foo")).asMethod
val methodMirror = instanceMirror.reflectMethod(method)
println(methodMirror())
}
def testInnerClass(name: String) = {
val sym = b.typeSignature.declaration(TypeName(name)).asClass
println(sym)
val ctor = sym.typeSignature.declaration(TermName("<init>")).asMethod
val ctorMirror = cm.reflect(new B.BB).reflectClass(sym).reflectConstructor(ctor)
val instance = ctorMirror()
println(instance)
testMethodInvocation(instance)
}
testInnerClass("B1")
testInnerClass("B2")
def testInnerModule(name: String) = {
val sym = b.typeSignature.declaration(TermName(name)).asModule
println(sym)
val moduleMirror = cm.reflect(new B.BB).reflectModule(sym)
val instance = moduleMirror.instance
println(instance)
testMethodInvocation(instance)
}
testInnerModule("B3")
testInnerModule("B4")
testInnerModule("B5")
testInnerModule("B6")
}
|