jruby icon indicating copy to clipboard operation
jruby copied to clipboard

Cannot call a Java method whose parameter is RubyArray when it is overloaded

Open Omegas7 opened this issue 12 years ago • 4 comments

Have a Java method that accepts a RubyArray parameter. If you call it, it works fine. But then, if you overload that method with another with a different parameter, you won't be able to call the RubyArray one:

    import javax.script.*;

    import org.jruby.RubyArray;

    public class Main {
        public static void main(String[] args) throws ScriptException, NoSuchMethodException {
            Main app = new Main();
        }
        public Main() throws ScriptException, NoSuchMethodException {
            ScriptEngine jruby = new ScriptEngineManager().getEngineByName("jruby");
            jruby.eval("include Java;\n" + "def start(main) main.test([1,2,3]); end");
            Invocable invocable = (Invocable)jruby;
            invocable.invokeFunction("start",this);
        }
        public void test(int x) {
            System.out.println(x);
        }
        public void test(RubyArray array) {
            System.out.println(array);
        }
    }

If you run that code, JRuby will tell you that there are no methods for the parameter RubyArray, and then it will mention that there are two overloads available: (int) and (RubyArray), which doesn't make any sense.

JRuby 1.7.11 building with Java SE 6 on Eclipse Kepler for Mac.

Omegas7 avatar Apr 18 '14 00:04 Omegas7

I can reproduce this with 1.7.12 and OpenJDK 1.7.0. It also shows up with static methods -- I simplified to this Java class:

import org.jruby.RubyArray;

public class Foo {
    public static void test(int x) {
        System.out.println(x);
    }
    public static void test(RubyArray array) {
        System.out.println(array);
    }
}

I can compile that, and then open IRB, and:

Java::Foo.test 42

works as expected, but:

 Java::Foo.test [1,2,3]

fails exactly as you describe. Swapping the order of the calls or method definitions has no effect. But the RubyArray method does work, so long as it's not overloaded.

masover avatar Apr 29 '14 16:04 masover

Potential workaround: Instead of working with RubyArray directly, use the List interface:

import java.util.List;
public class Foo {
    public static void test(int x) {
        System.out.println(x);
    }
    public static void test(List<?> array) {
        System.out.println(array);
    }
}

For whatever reason, there's at least one chunk of the dispatch code that's interpreting RubyArray as a List. This makes some sense, since it's incredibly handy to be able to call Java methods that expect Lists and give them Ruby arrays instead.

masover avatar May 01 '14 05:05 masover

Thanks @masover, I will use your proposed workaround for the time being. This is fairly weird.

Omegas7 avatar May 03 '14 09:05 Omegas7

the resolution logic deal with Java types (and their mappings to Ruby types) but having the 'native' Ruby types recognized might be useful as well ... let's see how that goes.

kares avatar Jan 19 '17 19:01 kares