I'm trying to implement a java interface on a ruby class, then pass the Java class (via become_java!) into a java method that will create it with Class.newInstance and invoke the interfaces method.
I had no luck with jruby 1.6.7 so tried 1.7preview1 which also has a number of issues.
First, if I require 'jruby/core_ext' before defining the class I get an exception:
require 'jruby/core_ext'
class Foo
include java.lang.Readable
java_signature "int read(java.nio.CharBuffer)"
def read(cb); 3; end
end
If I move 'jruby/core_ext' to after the class definition, it compiles but the method signature is not correct.
require 'java'
class Foo
include java.lang.Readable
java_signature "int read(java.nio.CharBuffer)"
def read(cb); 3; end
end
require 'jruby/core_ext'
Foo.become_java!.declared_methods.each {|m| puts m.toGenericString }
s = java.util.Scanner.new(Foo.become_java!.newInstance)
s.nextByte
The method signature is:
and an instance doesn't satisfy the interface:
If I try to use add_method_signature from jruby/core_ext instead of java_signature, it fails because it doesn't handle symbols for non java class types (e.g. :int) - the documentation implies it does:
require 'jruby/core_ext'
class Foo
include java.lang.Readable
def read(cb); 3; end
add_method_signature :read, [:int, java.nio.CharBuffer]
end
The error is:
If I declare the method to return java.lang.Number, then the java method signature does change - but of course this doesn't satisfy this interface:
require 'jruby/core_ext'
class Foo
include java.lang.Readable
def read(cb); 3; end
add_method_signature :read, [java.lang.Integer, java.nio.CharBuffer]
end
Foo.become_java!.declared_methods.each {|m| puts m.toGenericString }
In this case the method signature is:
public java.lang.Integer rubyobj.Foo.read(java.nio.CharBuffer)
|