0

Is there a reflective way to access classes methods' AST?

class Clazz {
    def hello(a: Int) = (a + 1).toLong
}

val src = 
"""
    class Clazz {
        def hello(a: Int) = (a + 1).toLong
    }
"""

I want to get something like this

scala> val a = 100
scala> showRaw(reify((a + 1).toLong).tree)
res2: String = Select(Apply(Select(Select(Select(Select(Select(Select(Ident($line11.$read), newTermName("$iw")), newTermName("$iw")), newTermName("$iw")), newTermName("$iw")), newTermName("a")), newTermName("$plus")), List(Literal(Constant(1)))), newTermName("toLong"))

as a result.

I believe this is going to be a compile-time universe.

Anton
  • 494
  • 5
  • 19
  • Do you want to get an AST from a string or from a classname/methodname pair? – Eugene Burmako Mar 25 '15 at 10:04
  • Any of the two ways will do, yet getting an AST from a classname/methodname pair is preferable. – Anton Mar 25 '15 at 11:36
  • 2
    The former is easy: just do c.parse (in a macro) or ToolBox.parse (if you're not in a macro). The latter doesn't have a reliable implementation - all approaches that I'm aware of have various limitations and some of them only work if the user (not the metaprogrammer, but their user!) does something special. – Eugene Burmako Mar 25 '15 at 15:50

0 Answers0