Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.jruby.Ruby;
- import org.jruby.RubyObject;
- import org.jruby.javasupport.util.RuntimeHelpers;
- import org.jruby.runtime.builtin.IRubyObject;
- import org.jruby.javasupport.JavaUtil;
- import org.jruby.RubyClass;
- import org.some.Class;
- import org.foo.*;
- public class MyClass extends RubyObject {
- private static final Ruby __ruby__ = Ruby.getGlobalRuntime();
- private static final RubyClass __metaclass__;
- static {
- String source = new StringBuilder("java_import 'org.some.Class'\n" +
- "java_import 'org.foo.*'\n" +
- "\n" +
- "class MyClass\n" +
- " java_signature 'void main(String[])'\n" +
- " def self.main(args)\n" +
- " end\n" +
- "\n" +
- " java_signature 'String somethingElse(int,float,Object)'\n" +
- " def something_else(a, b, c)\n" +
- " end\n" +
- "end\n" +
- "").toString();
- __ruby__.executeScript(source, "blah.rb");
- RubyClass metaclass = __ruby__.getClass("MyClass");
- metaclass.setRubyStaticAllocator(MyClass.class);
- if (metaclass == null) throw new NoClassDefFoundError("Could not load Ruby class: MyClass");
- __metaclass__ = metaclass;
- }
- /**
- * Standard Ruby object constructor, for construction-from-Ruby purposes.
- * Generally not for user consumption.
- *
- * @param ruby The JRuby instance this object will belong to
- * @param metaclass The RubyClass representing the Ruby class of this object
- */
- private MyClass(Ruby ruby, RubyClass metaclass) {
- super(ruby, metaclass);
- }
- /**
- * A static method used by JRuby for allocating instances of this object
- * from Ruby. Generally not for user comsumption.
- *
- * @param ruby The JRuby instance this object will belong to
- * @param metaclass The RubyClass representing the Ruby class of this object
- */
- public static IRubyObject __allocate__(Ruby ruby, RubyClass metaClass) {
- return new MyClass(ruby, metaClass);
- }
- /**
- * Default constructor. Invokes this(Ruby, RubyClass) with the classloader-static
- * Ruby and RubyClass instances assocated with this class, and then invokes the
- * no-argument 'initialize' method in Ruby.
- *
- * @param ruby The JRuby instance this object will belong to
- * @param metaclass The RubyClass representing the Ruby class of this object
- */
- public MyClass() {
- this(__ruby__, __metaclass__);
- RuntimeHelpers.invoke(__ruby__.getCurrentContext(), this, "initialize");
- }
- public static void main(String[] args) {
- IRubyObject ruby_args = JavaUtil.convertJavaToRuby(__ruby__, args);
- IRubyObject ruby_result = RuntimeHelpers.invoke(__ruby__.getCurrentContext(), __metaclass__, "main", ruby_args);
- return;
- }
- public String somethingElse(int a, float b, Object c) {
- IRubyObject ruby_a = JavaUtil.convertJavaToRuby(__ruby__, a);
- IRubyObject ruby_b = JavaUtil.convertJavaToRuby(__ruby__, b);
- IRubyObject ruby_c = JavaUtil.convertJavaToRuby(__ruby__, c);
- IRubyObject ruby_result = RuntimeHelpers.invoke(__ruby__.getCurrentContext(), this, "something_else", ruby_a, ruby_b, ruby_c);
- return (String)ruby_result.toJava(String.class);
- }
- }
Add Comment
Please, Sign In to add comment