How I write a JRuby class which implements a Java interface?

Hello List,

I want to write a JRuby class which implements a Java interface.

For example, in Java I might write this:

public class MyJavaClass implements MyJavaInterface {

// Java code goes here.

}

Google pointed me in this direction:

http://www.google.com/search?q=Can+a+JRuby+class+implement+an+interface

Then here:

http://kenai.com/projects/jruby/pages/CallingJavaFromJRuby#Interfaces

And I see this:

Implementing Java Interfaces in JRuby

JRuby classes can now implement more than one Java interface. Since
Java interfaces are mapped to modules in JRuby, you implement them not
by subclassing, but by mixing them in.

class SomeJRubyObject
include java.lang.Runnable
include java.lang.Comparable
end

So, I wrote this:

#!/usr/bin/env jruby

sjo.rb

require ‘java’

class SomeJRubyObject
include java.lang.Runnable
include java.lang.Comparable
end

I transformed it into a Java class:

jrubyc --javac some_jruby_object.rb

I inspected the resulting Java class:

import org.jruby.Ruby;
import org.jruby.RubyObject;
import org.jruby.javasupport.util.RuntimeHelpers;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.javasupport.JavaUtil;
import org.jruby.RubyClass;

public class SomeJRubyObject extends RubyObject {
private static final Ruby ruby = Ruby.getGlobalRuntime();
private static final RubyClass metaclass;

Q1: I was expecting/hoping/wanting to see this:

public class SomeJRubyObject extends RubyObject implements Runnable,
Comparable {

Why is the class generated by jrubyc missing the “implements”
declaration?

Q2: How do I write JRuby syntax which would result in an “implements”
declaration appearing in my .java file should I choose to generate the
.java file with jrubyc?

The reason I want to do this is that I want to use an API written in
Java.

But I really want to avoid writing Java.

One thing this API expects me to do is write Java classes which
implement an interface in the API.

If I can write a class which implements this interface, then the API
can copy data into my objects via callback methods.

This will make me happy because I want that data.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

I wrote a blog about a jruby class implementing a java interface with
sample code
http://tommy.chheng.com/index.php/2010/06/call-a-jruby-method-from-java/

@tommychheng
Programmer and UC Irvine Graduate Student
Find a great grad school based on research interests:
http://gradschoolnow.com

On 8/10/10 11:30 AM, Audrey L. wrote:

}

I transformed it into a Java class:
import org.jruby.javasupport.JavaUtil;
public class SomeJRubyObject extends RubyObject implements Runnable,
But I really want to avoid writing Java.
To unsubscribe from this list, please visit:

 http://xircles.codehaus.org/manage_email

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Tommy,

your blog entry is a golden piece of information.

Thanks for taking time to write it up!

Also I found this syntax which works well:

sjo.rb

require ‘java’

class SomeJRubyObject
java_implements java.lang.Runnable
java_signature ‘void run()’
def run
p “running now.”
p “done running.”
end
end

On 8/10/10, Tommy C. [email protected] wrote:

On 8/10/10 11:30 AM, Audrey L. wrote:

}
Implementing Java Interfaces in JRuby
So, I wrote this:

import org.jruby.runtime.builtin.IRubyObject;



To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email