0

I'm trying to take a private static method from a Java class and invoke it in Scala. Here's the code I have so far:

val blockClass = classOf[Block]
val aMethod: Method = blockClass.getDeclaredMethod("a", Integer.TYPE, classOf[String], classOf[Block])
aMethod.setAccessible(true)

aMethod.invoke(null, 145, "anvil", anvilPatch)

When I try to compile this, however, I get this error:

Error: the result type of an implicit conversion must be more specific than AnyRef
aMethod.invoke(null, 145, "anvil", null)
                     ^

That 145 is supposed to be a Java int, and Integer.TYPE is the only thing I could think of to get a Java int.

Any ideas?

2 Answers 2

1

Not sure why the error happens, but it can be fixed simply by casting 145 to AnyRef (which is the same as Object) explicitly:

aMethod.invoke(null, 145.asInstanceOf[AnyRef], "anvil", anvilPatch)
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of using Integer.TYPE, try using classOf[Int]. This will get you the class object of Scala's Int type.

val aMethod: Method = blockClass.getDeclaredMethod("a", classOf[Int], classOf[String], classOf[Block])

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.