Forum: JRuby java_send and TypeError for Java Interfaces

Posted by Andreas Ronge (Guest)
on 2012-12-18 22:13
(Received via mailing list)
Hi

How can I use the java_send method where the arguments are Java 
interfaces ?
In the example in
https://github.com/jruby/jruby/wiki/CallingJavaFromJRubythey only use
classes.
When I specify an interface in the argument list like this:

  traversal_description.java_send(:expand,
[org.neo4j.graphdb.PathExpander], pe)

I get the following stack trace:

  TypeError: cannot convert instance of class org.jruby.RubyModule to 
class
java.lang.Class
  org/jruby/java/proxies/JavaProxy.java:332:in `java_send'

I found that you should use Class.for_name for interfaces since the java
interfaces become Ruby Modules by default, see (
http://jr.runcode.us/q/jruby-calls-the-wrong-method). But that does not
work either.

The reason why I'm doing this is because I wan't to avoid a warning 
about
"ambiguous Java methods found"

I'm trying to call the evaluator method in the Neo4j API
TraversalDescription,
http://api.neo4j.org/1.8.1/org/neo4j/graphdb/trave...


The first evaluator method takes an Evaluator Interface as argument.
The second evaluator method takes a PathEvaluator interface which 
extends
the Evaluator interface and uses generics.

I'm using JRuby 1.7.1 on Java version "1.7.0_10"

Cheers
Andreas
Posted by Keith B. (keith_b)
on 2012-12-19 03:04
(Received via mailing list)
I'm pretty sure there's a better answer, but as a workaround, could you 
create a minimal object that includes the module, and call java_send on 
that object instead?:

module M
  def foo
    puts "foo!"
  end
end

class MWrapper
  include M
end

puts MWrapper.new.foo

# output: foo!

- Keith
Posted by Andreas Ronge (Guest)
on 2012-12-19 09:28
(Received via mailing list)
I've already tried that (if I understand you correctly) together with
java_class which gives me a NullPointerException.
I use the java_class method on my own Ruby object that implements the 
java
interface, but then I get an NullPointer.

Example:

jruby-1.7.1 :021 > class Foo
jruby-1.7.1 :022?>   include java.lang.Readable
jruby-1.7.1 :023?>   end
 => Foo
jruby-1.7.1 :024 > f = Foo.new
 => #<Foo:0x5f7f8483>
jruby-1.7.1 :025 > f.java_class
Java::JavaLang::NullPointerException:
from
org.jruby.java.proxies.JavaInterfaceTemplate$5.call(JavaInterfaceTemplate.java:191)
from
org.jruby.runtime.callsite.CachingCallSite.cacheAndCall(CachingCallSite.java:306)
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.