score:6

Accepted answer

currently that's impossible to do with the public api, but there's a jira issue for that: https://issues.scala-lang.org/browse/si-7317. here's a workaround that uses internal apis:

scala> class c { def foo = 2 }defined class c

scala> val foo = typeof[c].declarations.tolist.apply(1).asmethod
foo: reflect.runtime.universe.methodsymbol = method foo

scala> val cm = runtimemirror(getclass.getclassloader)
cm: reflect.runtime.universe.mirror = javamirror...

scala> val cmx = cm.asinstanceof[{
    def methodtojava(sym: scala.reflect.internal.symbols#methodsymbol): java.lang.reflect.method
}]
cmx: anyref{def methodtojava(sym: scala.reflect.internal.symbols#methodsymbol): java.lang.reflect.method} = javamirror ...

scala> cmx.methodtojava(
    foo.asinstanceof[scala.reflect.internal.symbols#methodsymbol]
)
res1: java.lang.reflect.method = public int c.foo()

Related Query

More Query from same tag