scala - How to find field annotations for a MethodSymbol? -
suppose have found set of methods type:
case class x() object a{ @someannotation val x=x() } def totype[t](a:t)(implicit tag: typetag[t]): type = tag.tpe val typ = totype(a) // public methods return x val intmethods = typ.members.collect{ case m: ru.methodsymbol if m.isgetter && m.ispublic && m.returntype <:< typeof[x] => m }
how efficiently find corresponding annotations each element of intmethods?
intmethods.head.annotations
is empty because there 2 entries in typ.decls x. 1 found method , other non-method field holds annotations. can search matching on:
getannotations(intmethods.head.toterm.name.tostring.trim) def getannotations( name: string) ={ typ.decls.filter{ s=>s.name.tostring.trim==name.trim && !s.ismethod}.flatmap(_.annotations) }
but tostringing , trimming extremely slow (the trim required because, 1 of decls contains trailing space, , other not). there better way directly lookup corresponding class field methodsymbol?
you can backing field of getter accessed
method:
intmethods.head.accessed.annotations
from documentation:
/** backing field accessor method, nosymbol other term symbols. */ abstract def accessed: universe.symbol
Comments
Post a Comment