Forum: JRuby java_send with a CharSequence not working

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3b8ad006781b1a3ab36dc76e2a05ce9c?d=identicon&s=25 Cris Shupp (cshupp)
on 2015-04-28 00:21
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 :-(
2c0c4cf3ccc8da22f7c3b9586ce1cd70?d=identicon&s=25 Christian MICHON (Guest)
on 2015-04-28 23:49
(Received via mailing list)
I read this line
  a.java_send :append, [java.lang.String], "I am Here\n"
as equivalent to
  a.append(java.lang.String.new("I am Here\n"))

Why use java_send in the first place?

So using [java.lang.String] means more or less a creation of a new java
String or casting from ruby String into java String.

But java.lang.CharSequence is an interface... not a java type. I doubt
you
can cast into it... Verification below

java.lang.CharSequence.new('boo')
        from org/jruby/java/proxies/JavaInterfaceTemplate.java:393:in
`new'
        from (irb):16:in `evaluate'
        from org/jruby/RubyKernel.java:1112:in `eval'
        from C:/dev/rb/lib/ruby/1.8/irb.rb:158:in `eval_input'
        from C:/dev/rb/lib/ruby/1.8/irb.rb:271:in `signal_status'
        from C:/dev/rb/lib/ruby/1.8/irb.rb:155:in `eval_input'
        from org/jruby/RubyKernel.java:1439:in `loop'
        from org/jruby/RubyKernel.java:1212:in `catch'
        from C:/dev/rb/lib/ruby/1.8/irb.rb:154:in `eval_input'
        from C:/dev/rb/lib/ruby/1.8/irb.rb:71:in `start'
        from org/jruby/RubyKernel.java:1212:in `catch'
        from C:/dev/rb/lib/ruby/1.8/irb.rb:70:in `start'
        from C:\dev\rb\bin\jirb:13:in `(root)'

Could you please show in plain java what you're trying to achieve with
CharSequence?
3b8ad006781b1a3ab36dc76e2a05ce9c?d=identicon&s=25 Cris Shupp (cshupp)
on 2015-04-28 23:57
I opened this as a bug here:

https://github.com/jruby/jruby/issues/2881

They already submitted a patch in an upcoming release (4 hours after
submission -- fast!!).

To answer your question...  Why use java_send in the first place?

To force JRuby to use the overloaded method of *my* choosing.  I
understand that I do not have to, but if I had called a preexisting
library that returned a CharSequence I wanted to be able to avoid the
intermediate object creation.

Cris
This topic is locked and can not be replied to.