从 Ruby 调用 Java 方法,该方法采用带有 Ruby BigDecimal 的 Java 基元双精度型
当 Java 方法的签名具有原始双精度值并且我传递的参数是 Ruby BigDecimal 时,尝试从 JRuby 调用 Java 对象上的方法时出现错误。是否存在可能的隐式转换,或者是否必须显式处理。如果有什么不同的话,我将使用 send 调用 Java 对象的方法。
java_object.send :some_method, a_big_decimal
(这实际上可以调用 Java 对象上具有不同类型签名的方法,因此我不仅需要担心 BigDecimals,这就是为什么我想尽可能避免显式处理它)
我也尝试过
java_object.send :some_method, a_big_decimal.to_java
但这会产生一个不同的错误:
TypeError: cannot convert instance of class org.jruby.java.proxies.ConcreteJavaProxy to double
I'm getting an error when attempting to call a method on a Java object from JRuby when the Java method's signature has a primitive double and the parameter I pass is a Ruby BigDecimal. Is there any implicit conversion that's possible there or does it have to be handled explicitly. I'm invoking the Java object's method with send if that makes any difference.
java_object.send :some_method, a_big_decimal
(This could actually invoke methods with different type signatures on the Java object so I don't only have to worry about BigDecimals here which is why I'd like to avoid explicitly handling it if possible)
I've also tried
java_object.send :some_method, a_big_decimal.to_java
But that produces a different error:
TypeError: cannot convert instance of class org.jruby.java.proxies.ConcreteJavaProxy to double
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我遇到这个问题时,我只是在谷歌上搜索类似问题的解决方案(在你发布它后仅 3 小时......该死的谷歌很快!)。
我最终能够通过执行(相当于)以下操作来解决我的问题:
希望这对您有用吗?
I was just Googling for a solution to a similar problem, when I came across this question (only 3 hours after you posted it ... damn Google is fast!).
I was eventually able to solve my problem by doing (the equivalent of) this:
Hopefully that will work for you?