[jruby-user] java_send with a CharSequence not working

3 views
Skip to first unread message

Cris Shupp

unread,
Apr 27, 2015, 6:21:47 PM4/27/15
to us...@jruby.codehaus.org
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 :-(

--
Posted via http://www.ruby-forum.com/.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Christian MICHON

unread,
Apr 28, 2015, 5:48:58 PM4/28/15
to us...@jruby.codehaus.org
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

        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?
--
Christian

Cris Shupp

unread,
Apr 28, 2015, 5:57:36 PM4/28/15
to us...@jruby.codehaus.org
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
Reply all
Reply to author
Forward
0 new messages