0

I am trying to invoke a method reflectively in Scala. But I keep encountering a wrong number of arguments exception, even though the arguments appear to match the method signature.

class ReflectionTest {

def myConcat (s1:String, s2:String, s3:String): String = {
return s1 + s2 + s3
}

@Test
def testReflectiveConcat = {
val s = myConcat ("Hello", "World", "Now")


val methods = new ReflectionTest ().getClass().getDeclaredMethods
for (method <- methods) {
  if (method.getName().startsWith("myConcat")) {
    // this throws IllegalArgumentException: wrong number of arguments
    val r = method.invoke(new ReflectionTest(), Array("Hello", "World", "Bye"))
    println (r)

   }
  }
 }
}

1 Answers1

1

invoke method takes variable number of arguments and not an array of arguments. It should be like this:

val r = method.invoke(new ReflectionTest(), "Hello", "World", "Bye")
Rado Buransky
  • 3,252
  • 18
  • 25