blob: 90bb4c4f885dd137749bb04081084177e8b51531 (
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
|
import scala.reflect.runtime.universe._
object Test {
/** Collects symbols by the given name, even if they're not
* named CC.
*/
def collectSymbols[T: TypeTag](inMethod: TermName, name: String): List[String] = {
val m = typeOf[T] member inMethod typeSignatureIn typeOf[T]
var buf: List[Symbol] = Nil
var seen: Set[Symbol] = Set()
def id(s: Symbol): Int = s.asInstanceOf[{ def id: Int }].id
def check(s: Symbol) {
if (!seen(s)) {
seen += s
if (s.name.toString == name) buf ::= s
}
}
def loop(t: Type) {
t match {
case TypeRef(pre, sym, args) => loop(pre) ; check(sym) ; args foreach loop
case PolyType(tparams, restpe) => tparams foreach { tp => check(tp) ; check(tp.owner) ; loop(tp.typeSignature) } ; loop(restpe)
case MethodType(params, restpe) => params foreach { p => check(p) ; loop(p.typeSignature) } ; loop(restpe)
case _ =>
}
}
loop(m)
buf.reverse.distinct map (s => s.name + "#" + id(s))
}
def main(args: Array[String]): Unit = {
val syms = collectSymbols[s.Bar]("to", "CC")
assert(syms.size == 1, syms)
println("OK!")
}
}
|