36

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

sanjib
  • 593
  • 1
  • 4
  • 7

3 Answers3

25
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
Thomas Jung
  • 32,428
  • 9
  • 84
  • 114
20

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
Ambling
  • 446
  • 5
  • 12
15

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
Arjan Blokzijl
  • 6,878
  • 2
  • 31
  • 27
  • 2
    I think it does break the expectation of clients of a companion (singleton) object if you create new instances of it. – Thomas Jung Jun 15 '10 at 07:36
  • Arjan - thanks. That's exactly what I was looking for. Thomas - That's true. But you could always wrap the code in an object factory and provide the singleton behavior yourself. – sanjib Jun 15 '10 at 17:10