1
votes

I'm stupid with Scala :( i have got some helps about invoking and Scala type. But new ones appear again:

class TagCalculation {
  def test(arg1: java.lang.Integer, arg2: String, arg3: scala.collection.immutable.$colon$colon[Any]) = "test mix2"
}

val getTest =  new TagCalculation
val arg1: java.lang.Integer = 10
val arg2: String = "foobar"
val arg3: scala.collection.immutable.$colon$colon[Any] = scala.collection.immutable.$colon$colon.apply('z', List("foo", 10))

//val test = Array(arg1,arg2,arg3) It's Ok if i use that.
var calcParamsArray : scala.collection.mutable.ArrayBuffer[Any] = scala.collection.mutable.ArrayBuffer()

calcParamsArray += arg1
calcParamsArray += arg2
calcParamsArray += arg3

val argtypes4 = calcParamsArray.map(_.getClass)
val method4 = getTest.getClass.getMethod("test", argtypes4: _*)
method4.invoke(getTest,calcParamsArray: _*) //also errors in toArray and toSqe

The outputs:

scala> method4.invoke(getTest,calcParamsArray: _) :29: error: type mismatch; found : scala.collection.mutable.ArrayBuffer[Any] required: Seq[Object] method4.invoke(getTest,calcParamsArray: _)

Any idea to solve this issue?

1

1 Answers

3
votes

The error message clearly says

found : scala.collection.mutable.ArrayBuffer[Any] 
required: Seq[Object] 

You are passing ArrayBuffer[Any] but required is Seq[Object]

Change the last line of your code to

method4.invoke(getTest,calcParamsArray.asInstanceOf[Seq[Object]]: _*)

It should work