2014-02-26 2 views
0

스칼라에서 메서드를 반사적으로 호출하려고합니다. 그러나 인수가 메서드 시그니처와 일치하는 것처럼 보일지라도 잘못된 개수의 예외가 계속 발생합니다.스칼라 : 잘못된 인수 개수 메서드 호출시 예외가 발생했습니다.

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

invoke 메서드는 가변 개수의 인수를 취하고 인수 배열을 사용하지 않습니다. 다음과 같아야합니다.

val r = method.invoke(new ReflectionTest(), "Hello", "World", "Bye") 
관련 문제