2

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
  • 48,194
  • 3
  • 28
  • 66
Fede E.
  • 2,118
  • 4
  • 23
  • 39
  • 3
    Take a look into https://stackoverflow.com/questions/2334200/transforming-scala-varargs-into-java-object-varargs – SMaZ Sep 10 '19 at 14:49

1 Answers1

3

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.

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
  • The first works, but the second doesn't. Could it be that AnyRef doesn't have the primitives for Int? I found that here: https://docs.scala-lang.org/tour/unified-types.html – Fede E. Sep 10 '19 at 15:28
  • 2
    @FedeE. I wrote that the second wouldn't work for primitive types like `Int`. – Dmytro Mitin Sep 10 '19 at 15:30