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_send will not invoke a method taking a CharSequence #2881

Closed
cshupp1 opened this issue Apr 27, 2015 · 6 comments
Closed

java_send will not invoke a method taking a CharSequence #2881

cshupp1 opened this issue Apr 27, 2015 · 6 comments

Comments

@cshupp1
Copy link

cshupp1 commented Apr 27, 2015

Consider the following in irb:

a = java.lang.StringBuilder.new
a.java_send :append, [java.lang.String], "I am Here\n"
puts a

yields I am Here as expected.

a.java_send :append, [java.lang.CharSequence], "I am not here!\n"
a.java_send :append, [java.lang.CharSequence], "I am not here!\n".to_java

both yield

TypeError: cannot convert instance of class org.jruby.RubyModule to class java.lang.Class
        from org/jruby/java/proxies/JavaProxy.java:347:in `java_send'
        from (irb):76:in `evaluate'
        from org/jruby/RubyKernel.java:1107:in `eval'
        from org/jruby/RubyKernel.java:1507:in `loop'
        from org/jruby/RubyKernel.java:1270:in `catch'
        from org/jruby/RubyKernel.java:1270:in `catch'
        from C:/jruby-1.7.18/bin/jirb:13:in `(root)'

Lets get extreme and open Ruby's String as follows:

class String
  include java.lang.CharSequence

  def charAt(index)
    self.to_java.charAt(index)
  end

  def length
    self.to_java.length
  end

  def subSequence(start, endPoint)
    self.to_java.subSequence(start, endPoint)
  end

  def toString
    self.to_java.toString()
  end

end

The code "SomeString".toString behaves as expected now, but:

 a.java_send :append, [java.lang.CharSequence], "Some String"
TypeError: cannot convert instance of class org.jruby.RubyModule to class java.lang.Class
        from org/jruby/java/proxies/JavaProxy.java:347:in `java_send'
        from (irb):81:in `evaluate'
        from org/jruby/RubyKernel.java:1107:in `eval'
        from org/jruby/RubyKernel.java:1507:in `loop'
        from org/jruby/RubyKernel.java:1270:in `catch'
        from org/jruby/RubyKernel.java:1270:in `catch'
        from C:/jruby-1.7.18/bin/jirb:13:in `(root)'

Still fails :-(

BTW:

irb(main):083:0> RUBY_VERSION
=> "1.9.3"
irb(main):084:0> JRUBY_VERSION
=> "1.7.18"
irb(main):085:0>
@headius
Copy link
Member

headius commented Apr 29, 2015

I will be fixing this (or have fixed it) for JRuby 1.7.20, but for interfaces you need to add .java_class so we pass the class through. The modules we set up for interfaces do not automatically coerce to java.lang.Class (yet).

@headius
Copy link
Member

headius commented Apr 29, 2015

Oh, I just saw @kares took a stab at this...but perhaps it is not finished?

@kares
Copy link
Member

kares commented Apr 29, 2015

@headius yeah, was trying to get a base 🍏 travis-ci to make sure I test properly ... and it finally is!

the fix here is to add toJava code on the RubyModule ... similar as the one that exists on RubyClass so that proxy (interface) modules convert correctly. all seems well and it seems it was not left out intentionally but forgotten (no test/spec code with java_send using an iface), a similar issue #638 existed for a while

@cshupp1
Copy link
Author

cshupp1 commented Apr 29, 2015

Question for you guys...

The fix seems to imply that my "getting extreme" and opening Ruby's string class to mixin CharSequence will be required. Is this the case, or is Ruby's String going to be coercible to a CharSequence?

@kares
Copy link
Member

kares commented Apr 29, 2015

@cshupp1 it won't be required just take a look at the commit there's a piece of your problematic code

@cshupp1
Copy link
Author

cshupp1 commented Apr 29, 2015

I see it! Thanks!

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

No branches or pull requests

3 participants