Anurag Sharma
Anurag Sharma

Reputation: 2605

How can I call Scala function with class name and function name as strings

I have to write scheduler that callbacks a function after processing. I can register the class name and method name as strings and have to callback it in future.

How can I call a function with class name and method name in Scala.

Upvotes: 1

Views: 686

Answers (1)

atline
atline

Reputation: 31574

Same with java, use reflect.

package tst

class A {
  def fun(a:String) = print(a)
}

object B extends App {
  val classA = Class.forName("tst.A")
  val method = classA.getDeclaredMethod("fun", classOf[String])
  method.invoke(classA.newInstance(), "hello world")
}

Upvotes: 2

Related Questions