dynamical creating a new instance of a case class in scala
reflection, scala
Solution
Just found it out by myself (respectively have seen it over here https://stackoverflow.com/a/2060503/55070). It's
method.invoke(t,seq: _*)
Sometimes it really helps to just write it down ;-)
Problem
Given a reference to a case class companion object `t` and a sequence of parameter `seq` how can I invoke a new instance of the case class? I can create a class when I type the number of the parameter by myself. ``` scala> case class B(n:String,a:Int,b:Int) defined class B scala> val t:AnyRef = B t: AnyRef = B scala> val m = t.getClass.getMethods.filter{m => m.getName == "apply"}. filterNot {_.getReturnType.getName == "java.lang.Object"}(0) m: java.lang.reflect.Method = public B B$.apply(java.lang.String,int,int) scala> m.invoke(t,"name",1:java.lang.Integer,2:java.lang.Integer) res99: Object = B(name,1,2) ``` The problem I couldn't solve is to call invoke with a sequence of arguments like `Seq("name",1:java.lang.Integer,2:java.lang.Integer)`. Any help how to do that is greatly appreciated. I use scala 2.10.0.