Created
May 30, 2009 09:10
-
-
Save xli/120454 to your computer and use it in GitHub Desktop.
This file contains 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
import static org.jruby.javasupport.JavaEmbedUtils.invokeMethod; | |
import org.jruby.Ruby; | |
import org.jruby.runtime.builtin.IRubyObject; | |
public class JRubyObject { | |
public static JRubyObject load(Ruby runtime, String dump) { | |
return new JRubyObject(((IRubyObject) marshalClass(runtime).send("load", dump))); | |
} | |
private static JRubyObject marshalClass(Ruby runtime) { | |
return new JRubyObject(runtime.evalScriptlet("Marshal")); | |
} | |
private final IRubyObject obj; | |
public JRubyObject(IRubyObject obj) { | |
this.obj = obj; | |
} | |
@SuppressWarnings("unchecked") | |
public <T> T send(String method, Object... args) { | |
Object[] nonAdapterArgs = new Object[args.length]; | |
for (int i = 0; i < args.length; i++) { | |
nonAdapterArgs[i] = getRidOfAdapter(args[i]); | |
} | |
return (T) invokeMethod(obj.getRuntime(), obj, method, nonAdapterArgs, Object.class); | |
} | |
@Override | |
public String toString() { | |
return this.obj.toString(); | |
} | |
private Object getRidOfAdapter(Object object) { | |
if (object instanceof JRubyObject) { | |
return ((JRubyObject) object).obj; | |
} | |
return object; | |
} | |
public String dump() { | |
return marshalClass(obj.getRuntime()).send("dump", obj); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment