Java_send and TypeError for Java Interfaces

Hi

How can I use the java_send method where the arguments are Java
interfaces ?
In the example in
Home · jruby/jruby Wiki · GitHub 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/traversal/TraversalDescription.html

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

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

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)