title: Scala: How do I dynamically instantiate an object and invoke a method using reflection?
body: <p>In Scala, what's the best way to dynamically instantiate an object and invoke a method using reflection?</p>
  
  <p>I would like to do Scala-equivalent of the following Java code:</p>
  
  <pre><code>Class class = Class.forName("Foo");
  Object foo = class.newInstance();
  Method method = class.getMethod("hello", null);
  method.invoke(foo, null);
  </code></pre>
  
  <p>In the above code, both the class name and the method name are passed in dynamically. The above Java mechanism could probably be used for <code>Foo</code> and <code>hello()</code>, but the Scala types don't match one-to-one with that of Java. For example, a class may be declared implicitly for a singleton object. Also Scala method allows all sorts of symbols to be its name. Both are resolved by name mangling. See <a href="http://www.codecommit.com/blog/java/interop-between-java-and-scala" rel="noreferrer">Interop Between Java and Scala</a>.</p>
  
  <p>Another issue seems to be the matching of parameters by resolving overloads and autoboxing, described in <a href="http://www.familie-kneissl.org/Members/martin/blog/reflection-from-scala-heaven-and-hell" rel="noreferrer">Reflection from Scala - Heaven and Hell</a>.  </p>