Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java method lookup for arity-1 method with inexact argument fails in the presence of arity-2 method with same name #8015

Closed
ikaronen-relex opened this issue Nov 16, 2023 · 1 comment · Fixed by #8057
Milestone

Comments

@ikaronen-relex
Copy link
Contributor

Test case:

Create the following Java class:

package foobar;

public class JRubyArityTest {
    public JRubyArityTest() {}

    public String foo(String s) {
        return "foo(" + s + ")";
    }

    public String bar(String s) {
        return "bar(" + s + ")";
    }

    public String bar(String s, boolean b) {
        return "NOT EXPECTED TO BE CALLED";
    }
}

and run the following JRuby code:

obj = Java::Foobar::JRubyArityTest.new
puts obj.foo('string')
puts obj.foo(:symbol)
puts obj.bar('string')
puts obj.bar(:symbol)

Expected output:

foo(string)
foo(symbol)
bar(string)
bar(symbol)

Observed output:

foo(string)
foo(symbol)
bar(string)
NameError: no method 'bar' for arguments (org.jruby.RubySymbol) on Java::Foobar::JRubyArityTest

Tested on JRuby 9.3.11.0 and 9.4.5.0. However, this appears to be a longstanding issue, since our codebase apparently has old workarounds for this bug dating from 2018.

@ikaronen-relex
Copy link
Contributor Author

Note that there are (at least) two possible ways to fix the specific unexpected behavior in the test case:

  1. Make RubyToJavaInvoker#findCallableArityOne (and friends) skip the argument matching logic and return immediately if only one candidate method with the correct name and arity is found.
  2. Make the argument matching logic in CallableSelector recognize RubySymbol as assignable to String.

In my opinion, while option 2 might be nice to have, option 1 fixes the real problem of non-varargs Java methods with different arities affecting each other's behavior when called from JRuby.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants