1

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?

2
  • AFAIK, scala use Array from java. So just convert list.toArray and this is enough. Commented Jul 19, 2015 at 12:14
  • I do know that. But list.toArray is not working. The thing is that invokeStaticMethod has two overload methods. the first one has object as argument and the second one has object[] as argument. No matter what argument I give to it, it just regard my argument as object. Commented Jul 19, 2015 at 15:39

1 Answer 1

3

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.

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.