简体   繁体   中英

Java Reflection in Scala, Error in Passing parameters to function

I am using Java Reflection in Scala, However when I try to invoke the function I am getting the following Error : java.lang.IllegalArgumentException: argument type mismatch

Following function present in testClass

def test(mystr:String):String ={
  return "hi"
}

I am calling the above function the following way

var params = new Array[Object](1)
params(0) = "hi"

//extractOject is instance of testClass
val featuresJsonStr = extractFunction.invoke(extractOject,params)

I read that invoke function expects parameters of function as array of objects, I am trying to do the same thing here

You forgot to use varargs syntax in Scala

val featuresJsonStr = extractFunction.invoke(extractOject, params: _*) // hi

How to pass Scala array into Scala vararg method?

Or just

val featuresJsonStr = extractFunction.invoke(extractOject, "hi") // hi

(Please don't use return keyword in Scala.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM