1 package org.jruby.javasupport; 2 3 30 31 import java.util.List ; 32 33 import org.jruby.Ruby; 34 import org.jruby.runtime.Block; 35 import org.jruby.runtime.ThreadContext; 36 import org.jruby.runtime.builtin.IRubyObject; 37 38 45 public class JavaEmbedUtils { 46 52 public static Ruby initialize(List loadPaths) { 53 Ruby runtime = Ruby.getDefaultInstance(); 54 runtime.getLoadService().init(loadPaths); 55 runtime.getLoadService().require("java"); 56 57 return runtime; 58 } 59 60 65 public static void terminate(Ruby runtime) { 66 runtime.tearDown(); 67 runtime.getThreadService().disposeCurrentThread(); 68 } 69 70 80 public static Object invokeMethod(Ruby runtime, Object receiver, String method, Object [] args, 81 Class returnType) { 82 IRubyObject rubyReceiver = receiver != null ? 83 JavaUtil.convertJavaToRuby(runtime, receiver) : runtime.getTopSelf(); 84 85 IRubyObject[] rubyArgs = JavaUtil.convertJavaArrayToRuby(runtime, args); 86 87 IRubyObject javaUtilities = runtime.getObject().getConstant("JavaUtilities"); 89 ThreadContext context = runtime.getCurrentContext(); 90 for (int i = 0; i < rubyArgs.length; i++) { 91 IRubyObject obj = rubyArgs[i]; 92 93 if (obj instanceof JavaObject) { 94 rubyArgs[i] = javaUtilities.callMethod(context, "wrap", obj); 95 } 96 } 97 98 IRubyObject result = rubyReceiver.callMethod(context, method, rubyArgs); 99 100 return rubyToJava(runtime, result, returnType); 101 } 102 103 107 public static Object rubyToJava(Ruby runtime, IRubyObject value, Class type) { 108 return JavaUtil.convertArgument(Java.ruby_to_java(runtime.getObject(), value, Block.NULL_BLOCK), type); 109 } 110 111 114 public static IRubyObject javaToRuby(Ruby runtime, Object value) { 115 if (value instanceof IRubyObject) { 116 return (IRubyObject) value; 117 } 118 IRubyObject result = JavaUtil.convertJavaToRuby(runtime, value); 119 if (result instanceof JavaObject) { 120 return runtime.getModule("JavaUtilities").callMethod(runtime.getCurrentContext(), "wrap", result); 121 } 122 return result; 123 } 124 125 public static IRubyObject javaToRuby(Ruby runtime, boolean value) { 126 return javaToRuby(runtime, value ? Boolean.TRUE : Boolean.FALSE); 127 } 128 public static IRubyObject javaToRuby(Ruby runtime, byte value) { 129 return javaToRuby(runtime, new Byte (value)); 130 } 131 public static IRubyObject javaToRuby(Ruby runtime, char value) { 132 return javaToRuby(runtime, new Character (value)); 133 } 134 public static IRubyObject javaToRuby(Ruby runtime, double value) { 135 return javaToRuby(runtime, new Double (value)); 136 } 137 public static IRubyObject javaToRuby(Ruby runtime, float value) { 138 return javaToRuby(runtime, new Float (value)); 139 } 140 public static IRubyObject javaToRuby(Ruby runtime, int value) { 141 return javaToRuby(runtime, new Integer (value)); 142 } 143 public static IRubyObject javaToRuby(Ruby runtime, long value) { 144 return javaToRuby(runtime, new Long (value)); 145 } 146 public static IRubyObject javaToRuby(Ruby runtime, short value) { 147 return javaToRuby(runtime, new Short (value)); 148 } 149 } 150 | Popular Tags |