How do I call a Scala Object method using reflection?
say, I have the following:
trait SomeTrait {
def someMethod: String;
}
object SomeObject extends SomeTrait {
def someMethod = "something";
}
I would like to call "someMethod" using reflection as I have the object name as a String. Something like:
val objectName = "SomeObject"
val someTrait:SomeTrait = ???.asInstanceOf[SomeTrait]
someTrait.someMethod
or something similar.
Thanks
def companion[T](name : String)(implicit man: Manifest[T]) : T =
Class.forName(name + "$").getField("MODULE$").get(man.erasure).asInstanceOf[T]
val result = companion[SomeTrait]("SomeObject").someMethod
Since scala 2.10, we can use the reflection of Module:
import scala.reflect.runtime.universe
val runtimeMirror = universe.runtimeMirror(getClass.getClassLoader)
val module = runtimeMirror.staticModule("SomeObject")
val obj = runtimeMirror.reflectModule(module)
val someTrait:SomeTrait = obj.instance.asInstanceOf[SomeTrait]
someTrait.someMethod
For classes, this can be done pretty easily using the standard java reflection classOf method. For Scala objects, it is a bit more work, but it still can be done:
trait SomeTrait { def someMethod: String}
object SomeObject extends SomeTrait { def someMethod = "something"}
class SomeClass extends SomeTrait { def someMethod = "something"}
object Main {
def main(args:Array[String]) = {
val someClassTrait:SomeTrait = Class.forName("SomeClass").newInstance().asInstanceOf[SomeTrait]
println("calling someClassTrait: " + someClassTrait.someMethod)
val objectName = "SomeObject$"
val cons = Class.forName(objectName).getDeclaredConstructors();
cons(0).setAccessible(true);
val someObjectTrait:SomeTrait = cons(0).newInstance().asInstanceOf[SomeTrait]
println("calling someObjectTrait: " + someObjectTrait.someMethod)
}
}
//prints:
calling someClassTrait: something
calling someObjectTrait: something