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