Search code examples
javascalascala-java-interop

Calling Java method that receive variable amount of parameters from Scala


I am trying to wrap a Java method that receives variable amount of parameters, example:

void info(String var1, Object... var2);

I used the following:

def info(message: String, any: Any*): Unit = {
   LOGGER.info(message, any)
}

But that doesn't work, it ends up calling an info that receives only 1 object:

void info(String var1, Object var2);

How can I solve this to call the java method that receives multiple parameters?

Thanks!


Solution

  • Try

    def info(message: String, any: Any*): Unit = {
      LOGGER.info(message, any.asInstanceOf[Seq[Object]]: _*)
    }
    

    or

    def info(message: String, any: AnyRef*): Unit = {
      LOGGER.info(message, any: _*)
    }
    

    without casting but not applicable to primitive types.