Can I define a Java subclass in ruby, then instantiate it in Java?

耗尽温柔 提交于 2019-12-22 11:13:25

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/11579542/can-i-define-a-java-subclass-in-ruby-then-instantiate-it-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!