Calling Java method that receive variable amount of parameters from Scala

Fede E. :

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!

Dmytro Mitin :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=143989&siteId=1