I'd like to define a superclass in Java, then define a subclass in Ruby, then make instances of that subclass in Java. A little something like this:
// Java superclass
package myPkg;
public class Sup{
public Sup(){}
public abstract void foo(String a);
}
--
# Ruby code
include Java
require 'jruby/core_ext'
include_class 'myPkg.Sup'
class RubySub < Java::myPkg.Sup
def foo( a )
puts a;
end
end
RubySub.become_java!
RubySub
--
// Back in Java land
Ruby runtime = Ruby.newInstance();
IRubyObect ro = runtime.evalScriptlet(theRubyCodeAbove);
Class<Sup> clz = (Class<Sup>) JavaEmbedUtils.rubyToJava(runtime, ro, Class.class);
clz.newInstance().foo("Hey!");
I've tried the above code, but I get an InstantiationException from the newInstance()
call. I want to have the class declared in Ruby at runtime, so e.g. running jrubyc to compile the Ruby code to a Java class definition ahead of time is out. What am I doing wrong here?
I've run through the bottom code segment in the debugger, and I'm definitely getting a RubyClass object returned from the evalScriptlet()
call. Maybe there's an extra step involved to turn a RubyClass
into a Class
?
UPDATE: I have a lead -- apparently become_java!
doesn't work if the class is a subclass of a Java class. Is there another way I can do this, or am I just boned?
I've come up with a workaround for now, but it's not pretty.
In brief: I can't return
RubySub.become_java!
(which isnil
, because of JRUBY-6105) or try to returnRubySub
and convert from aRubyClass
to aClass
.Instead, I return
RubySub.method(:new)
, which shows up as aRubyMethod
. I can call this, and it creates anIRubyObject
which is actually an instance of the subclass. This can be cast to the Java superclass usingJavaEmbedUtils.rubyToJava()
. Like I said, it's not pretty but it works.