Guest User

Untitled

a guest
Jan 22nd, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1.  
  2.  
  3. import org.jruby.Ruby;
  4. import org.jruby.RubyObject;
  5. import org.jruby.javasupport.util.RuntimeHelpers;
  6. import org.jruby.runtime.builtin.IRubyObject;
  7. import org.jruby.javasupport.JavaUtil;
  8. import org.jruby.RubyClass;
  9. import org.some.Class;
  10. import org.foo.*;
  11.  
  12.  
  13. public class MyClass extends RubyObject {
  14. private static final Ruby __ruby__ = Ruby.getGlobalRuntime();
  15. private static final RubyClass __metaclass__;
  16.  
  17. static {
  18. String source = new StringBuilder("java_import 'org.some.Class'\n" +
  19. "java_import 'org.foo.*'\n" +
  20. "\n" +
  21. "class MyClass\n" +
  22. " java_signature 'void main(String[])'\n" +
  23. " def self.main(args)\n" +
  24. " end\n" +
  25. "\n" +
  26. " java_signature 'String somethingElse(int,float,Object)'\n" +
  27. " def something_else(a, b, c)\n" +
  28. " end\n" +
  29. "end\n" +
  30. "").toString();
  31. __ruby__.executeScript(source, "blah.rb");
  32. RubyClass metaclass = __ruby__.getClass("MyClass");
  33. metaclass.setRubyStaticAllocator(MyClass.class);
  34. if (metaclass == null) throw new NoClassDefFoundError("Could not load Ruby class: MyClass");
  35. __metaclass__ = metaclass;
  36. }
  37.  
  38. /**
  39. * Standard Ruby object constructor, for construction-from-Ruby purposes.
  40. * Generally not for user consumption.
  41. *
  42. * @param ruby The JRuby instance this object will belong to
  43. * @param metaclass The RubyClass representing the Ruby class of this object
  44. */
  45. private MyClass(Ruby ruby, RubyClass metaclass) {
  46. super(ruby, metaclass);
  47. }
  48.  
  49. /**
  50. * A static method used by JRuby for allocating instances of this object
  51. * from Ruby. Generally not for user comsumption.
  52. *
  53. * @param ruby The JRuby instance this object will belong to
  54. * @param metaclass The RubyClass representing the Ruby class of this object
  55. */
  56. public static IRubyObject __allocate__(Ruby ruby, RubyClass metaClass) {
  57. return new MyClass(ruby, metaClass);
  58. }
  59.  
  60. /**
  61. * Default constructor. Invokes this(Ruby, RubyClass) with the classloader-static
  62. * Ruby and RubyClass instances assocated with this class, and then invokes the
  63. * no-argument 'initialize' method in Ruby.
  64. *
  65. * @param ruby The JRuby instance this object will belong to
  66. * @param metaclass The RubyClass representing the Ruby class of this object
  67. */
  68. public MyClass() {
  69. this(__ruby__, __metaclass__);
  70. RuntimeHelpers.invoke(__ruby__.getCurrentContext(), this, "initialize");
  71. }
  72.  
  73.  
  74. public static void main(String[] args) {
  75. IRubyObject ruby_args = JavaUtil.convertJavaToRuby(__ruby__, args);
  76. IRubyObject ruby_result = RuntimeHelpers.invoke(__ruby__.getCurrentContext(), __metaclass__, "main", ruby_args);
  77. return;
  78.  
  79. }
  80.  
  81.  
  82. public String somethingElse(int a, float b, Object c) {
  83. IRubyObject ruby_a = JavaUtil.convertJavaToRuby(__ruby__, a);
  84. IRubyObject ruby_b = JavaUtil.convertJavaToRuby(__ruby__, b);
  85. IRubyObject ruby_c = JavaUtil.convertJavaToRuby(__ruby__, c);
  86. IRubyObject ruby_result = RuntimeHelpers.invoke(__ruby__.getCurrentContext(), this, "something_else", ruby_a, ruby_b, ruby_c);
  87. return (String)ruby_result.toJava(String.class);
  88.  
  89. }
  90.  
  91. }
Add Comment
Please, Sign In to add comment