« Return to Thread: Java subclasses in Ruby

Re: Java subclasses in Ruby

by backspaces :: Rate this Message:

Reply to Author | View in Thread

Interesting you suggest that, indeed in the initial tests, "callSetup()" took an argument, the instance on which setup was to be called.  I changed to the simpler approach where the Java callSetup just calls the currently visible setup method directly.  This worked for testing groovy so I presumed it'd be OK for JRuby.

So when I call: rubyClass.callSetup() I presume this causes the Java class to call the currently visible setup() method, which in the Ruby case is the Ruby subclass of the Java superclass.  The only way I think that might not test correctly is if JRuby "flattens" the classes, building a Ruby class with callSetup() promoted into Ruby.

I could try the old method where the instance gets passed around if you think it'd make a  difference.

Owen

Nick Sieger-2 wrote:
On 3/7/07, backspaces <owen@backspaces.net> wrote:
> First, I build a trivial java class with a setup method and a util method
> which simply prints a string.  In addition, I add a method that simply calls
> this instance's setup method:
> public class JavaClass {
>   public void setup() {
>     util("JavaClass: setup called.");
>   }
>   public void util(String s) {
>     System.out.println(s);
>   }
>   public void callSetup() {
>     this.setup();
>   }
> }
>
> I then build a java subclass overriding setup(), and include a main which
> lets me test it.  The test creates an instance of JavaClass and an instance
> of the subclass.  It then calls setup directly, and indirectly via
> callSetup:
> public class JavaSubClass extends JavaClass {
>     public void setup() {
>       util("JavaSubClass: setup called.");
>     }
>     public static void main(String[] args) {
>       JavaClass jc = new JavaClass();
>       jc.setup();
>       jc.callSetup();
>       System.out.println("");
>       JavaSubClass jsc = new JavaSubClass();
>       jsc.setup();
>       jsc.callSetup();
>     }
> }
>
> When I run it, I get what you'd expect:
> owen|~/src/jruby[1110]: java JavaSubClass
> JavaClass: setup called.
> JavaClass: setup called.
>
> JavaSubClass: setup called.
> JavaSubClass: setup called.
>
> Next for the JRuby test:
> require 'java'
> include_class "JavaClass"
>
> javaClass = JavaClass.new
> javaClass.setup()
> javaClass.callSetup()
>
> class RubyClass < JavaClass
>   def setup()
>     util('RubyClass: setup called')
>   end
> end
>
> puts
> rubyClass = RubyClass.new
> rubyClass.setup()
> rubyClass.callSetup()
>
> .. which works just fine:
> owen|~/src/jruby[1111]: jruby testjava.rb
> JavaClass: setup called.
> JavaClass: setup called.
>
> RubyClass: setup called
> RubyClass: setup called

I'm not surprised that this works, calling the Ruby subclass from the
Ruby side.  What happens if you pass that object back to Java and try
to call the setup method from there?

/Nick

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

 « Return to Thread: Java subclasses in Ruby