vincent chou
vincent chou

Reputation: 91

get java object array from scala list

I'm working with apache reflection, and there is a java method whose signature is

public static Object invokeStaticMethod(Class cls, String methodName,
  Object[] args)

And my code is

object Tobject {
  def echo(name: String) =
    println("echo 1")

  def echo2(name: String, arg: String) =
    println ("echo 2")
}



class ApacheReflection extends FunSuite {

  test("apache reflection") {
    val factory = ClassUtils.getClass("Tobject")

    MethodUtils.invokeStaticMethod(factory,"echo2", List("sf", "f").asJava)

  }

}

And I got exception message

No such accessible method: echo2() on class: Tobject
java.lang.NoSuchMethodException: No such accessible method: echo2() on class: Tobject

it seems that asJava can convert scala list to java list but not array, so how can I get java array from scala list?

Upvotes: 1

Views: 4421

Answers (1)

krcz
krcz

Reputation: 361

Idea to use .toArray is a good try. But it gives Array[String] (= String[] in Java), which is not the same as Array[Object] (= Object[]) for Java. On the other hand, if you use

List("sf", "f").toArray[Object]

everything works.

Upvotes: 3

Related Questions