简体   繁体   English

JRuby 调用了错误的方法

[英]JRuby calls the wrong method

I got a strange problem with a call to a Java method from JRuby .我从JRuby调用 Java 方法时遇到了一个奇怪的问题。

In my Java class these methods are defined twice, and it appears JRuby calls the wrong one.在我的 Java class 中,这些方法被定义了两次,看起来 JRuby 调用了错误的方法。

So I tried to use java_method , but I always got a:所以我尝试使用java_method ,但我总是得到:

TypeError: cannot convert instance of class org.jruby.RubyModule to class java.lang.Class类型错误:无法将 class org.jruby.RubyModule 的实例转换为 class java.lang.Z9BD81329FEDBF6EFE2

Here's my Java code:这是我的 Java 代码:

public class Renderer {

    ...
    public void addRenderer(IElementRenderer r) {
        System.out.println("Added element render: " + r.getClass().toString());

        basicRenderers.add(r);
        rendererMap.put(r.elementClass(), r);
    }

    public void addRenderer(IBasicRenderer r) {
        System.out.println("SHOULD NOT GO THERE !!");
        basicRenderers.add(r);
    }
}

and my JRuby code:和我的 JRuby 代码:

add_renderer = renderer.java_method :add_renderer, [Java::dragon.render.IElementRenderer]
add_renderer.call TextRenderer.new

I also tried with java_send but I got the same error:我也尝试了java_send但我得到了同样的错误:

renderer.java_send(:add_renderer, [Java::dragon.render.IElementRenderer], TextRenderer.new) renderer.java_send(:add_renderer, [Java::dragon.render.IElementRenderer], TextRenderer.new)

Next, I tried with:接下来,我尝试了:

renderer.add_renderer(TextRenderer.new.to_java(IElementRenderer)) renderer.add_renderer(TextRenderer.new.to_java(IElementRenderer))

This time no errors but the wrong method is called...这次没有错误但调用了错误的方法......

How can I fix this problem?我该如何解决这个问题?

You can fix that cannot convert instance of class org.jruby.RubyModule to class java.lang.Class using java.lang.Class.for_name You can fix that cannot convert instance of class org.jruby.RubyModule to class java.lang.Class using java.lang.Class.for_name

In your case, it is在你的情况下,它是

add_renderer = renderer.java_method :add_renderer, [java.lang.Class.for_name("dragon.render.IElementRenderer")]

This is because java interfaces become Ruby Modules by default and the second argument to:java_method expects an array of Class objects.这是因为 java 接口默认成为 Ruby 模块,并且第二个参数:java_method 需要一个 Class 对象数组。

You can print the matched method to see it is matching the intended method.您可以打印匹配的方法以查看它是否与预期的方法匹配。 For example, I see below code is matching the println(String) on System.out.例如,我看到下面的代码匹配 System.out 上的 println(String)。

>>java.lang.System.out.java_method "println", [java.lang.Class.for_name("java.lang.String")]
#<Method: Java::JavaIo::PrintStream#(java.lang.String)>

I've had problems like this before.我以前也遇到过这样的问题。 It was many versions ago and I think JRuby's method matching algorithm has improvedd over time.这是很多版本之前的事了,我认为 JRuby 的方法匹配算法随着时间的推移而改进了。 Are you using the latest JRuby?您使用的是最新的 JRuby 吗?

If nothing else works, you may need to add another method, or a wrapper class.如果没有其他方法,您可能需要添加另一种方法或包装器 class。 Something that distinguishes your methods by name or number of parameters, not just parameter type.通过名称或参数数量来区分您的方法的东西,而不仅仅是参数类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM