Can I define a Java subclass in ruby, then instant

2019-05-17 15:55发布

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?

标签: jruby
1条回答
啃猪蹄的小仙女
2楼-- · 2019-05-17 16:38

I've come up with a workaround for now, but it's not pretty.

In brief: I can't return RubySub.become_java! (which is nil, because of JRUBY-6105) or try to return RubySub and convert from a RubyClass to a Class.

Instead, I return RubySub.method(:new), which shows up as a RubyMethod. I can call this, and it creates an IRubyObject which is actually an instance of the subclass. This can be cast to the Java superclass using JavaEmbedUtils.rubyToJava(). Like I said, it's not pretty but it works.

查看更多
登录 后发表回答