Created
November 24, 2009 21:15
-
-
Save bluescreen303/242222 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ruby_thing.rb: | |
require 'jruby/core_ext' | |
puts "hello from ruby" | |
class RubyThing | |
include org.test.RubyInit::RubyThingInterface | |
def setJt(java_thing) | |
puts "setJt" | |
java_thing.say(self) | |
end | |
end | |
RubyThing.add_method_signature 'setJt', [java.lang.Void::TYPE, org.test.JavaThing] | |
RubyThing.become_java! | |
org.test.RubyInit.rubyThing = RubyThing.java_class | |
JavaThing.java: | |
package org.test; | |
public class JavaThing { | |
public JavaThing() { | |
} | |
public void say(RubyInit.RubyThingInterface o) { | |
System.out.println("got passed: " + o); | |
} | |
} | |
RubyInit.java: | |
package org.test; | |
import java.io.FileReader; | |
import javax.script.*; | |
public class RubyInit { | |
public static interface RubyThingInterface { | |
void setJt(JavaThing jt); | |
} | |
public static Class<? extends RubyThingInterface> rubyThing; | |
public static void init() throws Exception { | |
ScriptEngineManager factory = new ScriptEngineManager(); | |
ScriptEngine engine = factory.getEngineByName("jruby"); | |
FileReader reader = new FileReader("ruby_thing.rb"); | |
try { | |
engine.eval(reader); | |
} catch (ScriptException exception) { | |
exception.printStackTrace(); | |
} | |
} | |
public static void main(String...args) throws Exception { | |
init(); | |
JavaThing javaThing = new JavaThing(); | |
RubyThingInterface rt = rubyThing.newInstance(); | |
rt.setJt(javaThing); | |
} | |
} | |
output: | |
$ java -cp .:./jruby.jar org.test.RubyInit | |
hello from ruby | |
setJt | |
got passed: null | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment