Issue Type: Bug Bug
Affects Versions: JRuby 1.7.0.pre1
Assignee: Unassigned
Components: Java Integration
Created: 29/Jun/12 2:59 PM
Description:

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
NoMethodError: undefined method `start_with?' for #<Java::OrgJrubyAstJava_signature::ReferenceTypeNode:0x5631db04>
    as_java_type at /Users/aw/.rvm/rubies/jruby-1.7.0.preview1/lib/ruby/shared/jruby/compiler/java_signature.rb:20
      parameters at /Users/aw/.rvm/rubies/jruby-1.7.0.preview1/lib/ruby/shared/jruby/compiler/java_signature.rb:48
            each at file:/Users/aw/.rvm/rubies/jruby-1.7.0.preview1/lib/jruby.jar!/jruby/java/java_ext/java.util.rb:7
             map at org/jruby/RubyEnumerable.java:705
      parameters at /Users/aw/.rvm/rubies/jruby-1.7.0.preview1/lib/ruby/shared/jruby/compiler/java_signature.rb:48
           types at /Users/aw/.rvm/rubies/jruby-1.7.0.preview1/lib/ruby/shared/jruby/compiler/java_signature.rb:54
  java_signature at /Users/aw/.rvm/rubies/jruby-1.7.0.preview1/lib/ruby/shared/jruby/core_ext/class.rb:44
             Foo at foo.rb:5
          (root) at foo.rb:3

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:

public org.jruby.runtime.builtin.IRubyObject rubyobj.Foo.read(org.jruby.runtime.builtin.IRubyObject[])

and an instance doesn't satisfy the interface:

Scanner.java:780:in `readInput': java.lang.AbstractMethodError: rubyobj.Foo.read(Ljava/nio/CharBuffer;)I
	from Scanner.java:1459:in `next'
	from Scanner.java:1855:in `nextByte'
        [...]

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:

TypeError: expected a Java class, got int
           _anno_class at /Users/aw/.rvm/rubies/jruby-1.7.0.preview1/lib/ruby/shared/jruby/core_ext/class.rb:107
  add_method_signature at /Users/aw/.rvm/rubies/jruby-1.7.0.preview1/lib/ruby/shared/jruby/core_ext/class.rb:179
                  each at org/jruby/RubyArray.java:1611
                inject at org/jruby/RubyEnumerable.java:807
  add_method_signature at /Users/aw/.rvm/rubies/jruby-1.7.0.preview1/lib/ruby/shared/jruby/core_ext/class.rb:179
                   Foo at foo.rb:5
                (root) at foo.rb:2

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)
Project: JRuby
Priority: Major Major
Reporter: Andrew Wason
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators.
For more information on JIRA, see: http://www.atlassian.com/software/jira
--------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email

Reply via email to