1
votes

I need to override a Java method from a Java interface which makes use of a vararg parameter:

public interface Function<T> {
    T apply(Object... var1);
}

When I try to implement the method apply in Scala like follows

class ScalaFunction extends Function[String] {

  override def apply(args: Object*): String = args.toList match {
    // impl
  }

}

I get a "java.lang.AbstractMethodError: null" error during run-time.

This is what I already tried:

  1. using @varargs args Scala annotation which should generate a Java-friendly delegate method => resulted in the same error
  2. using a self-provided delegate method

    def apply(args: Array[Object]): Unit = apply(args: _*)

    which was actually successfully called, but I'm not able to use String as return type because then the compiler yields a

Error:(13, 7) double definition: def apply(args: Array[Object]): String at line 20 and override def apply(args: AnyRef*): String at line 13 have same type after erasure: (args: Array[Object])String

The calling Java code looks like this:

Object[] args = //...
Object returnValue = function.apply(args);

Any ideas?

See this Gist for an example: https://gist.github.com/mrueegg/f191e547bee6a24a6bcf

Thanks, Michael

1
No solution (sorry) but have you tried getting your IDE to override the method for you? I would imagine it's a standard process and they should do it correctly for you.John McClean
That looks like a bug to me.ghik
IntelliJ suggests override def apply(objects: AnyRef*): String = ??? which is what I already have...Michael Rueegg
Can't reproduce this on Scala 2.11.6 - what version are you on?ghik
Sorry, forgot to mention that: Scala 2.11.7Michael Rueegg

1 Answers

1
votes

This issue. I haven't followed it closely, but maybe someone wants to add info.