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 Answer 1

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")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.