This page is no longer maintained — Please continue to the home page at www.scala-lang.org

invoke method with an array using reflection

No replies
gdefacci
Joined: 2008-08-21,
User offline. Last seen 2 years 38 weeks ago.

Hi all.
I have a problem with the following snipplet:

class PiArr {
var intArr:Array[Int] = _
}

object ReflectTest {
def main(args : Array[String]) : Unit = {
val pa = new PiArr()
val mthd = pa.getClass.getMethods.find(_.getName == "intArr_$eq" ).get
val ar1 = new Array[Int](4)
ar1(0) = 12
ar1(1) = 13
ar1(2) = 14
ar1(3) = 14

mthd.invoke(ar1) // fail :
mthd.invoke(Array(1, 4, 5)) // fail
}
}

Both mthd.invoke fail with the exception:

java.lang.IllegalArgumentException: object is not an instance of declaring class

Is it the expected behaviour? Is there a way to make it working?

thanks

G

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland