Jruby rb for java generated class inheritance support or workarounds

I am experimenting with JRuby - generating java from ruby ​​files. I have an abstract class in ruby ​​that implements a Java interface, and child classes extend it. also in ruby.

I ran into a problem described at http://jira.codehaus.org/browse/JRUBY-6342 , where all generated java files only extend RubyObject.

I am wondering if anyone else has come across this and a workaround? Right now, I used the java_implement interface in every child class, since they do not extend the abstract class.

I included a snippet from JRUBY-6342 describing the problem:

The Java code created by jrubyc --java does not seem to support Ruby class inheritance. Given the following simple example:

class A def my_class; self.class.name end end

class B <end

The generated class in B.java inherits from RubyObject, not A, which makes class B completely broken in Java. According to some related notes, the inclusion of the module does not work either. A class with an M inclusion does not receive M methods in the generated Java code.

Am I missing something in my understanding of Ruby or JRuby?

+4
source share
1 answer

This is still a problem, since the jruby compiler is still creating RubyObject for classes.

The only workaround I know is to use JRuby ScriptEngine from Java to evaluate your JRuby code. For example, here is some JRuby code:

 require 'java' java_import 'javax.swing.JFrame' java_import 'javax.swing.JButton' class MyFrame < JFrame def initialize super('Test') content_pane.add(JButton.new("Hello")) pack() end end 

Then this code can be called from the Java class as follows:

 import javax.swing.JFrame; import javax.script.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("jruby"); Reader reader = new FileReader("myframe.rb"); engine.eval(reader); // Instantiate the JRuby class, and cast the result of eval. JFrame frame = (JFrame) engine.eval("MyFrame.new"); frame.setVisible(true); } } 

Here, the object returned by eval can be placed in a JFrame , as you would expect. See also this question for this problem.

+2
source

Source: https://habr.com/ru/post/1432603/


All Articles