InstanceMethodInvoker.java

  1. package org.jruby.java.invokers;

  2. import java.lang.reflect.Method;
  3. import java.util.List;

  4. import org.jruby.Ruby;
  5. import org.jruby.RubyModule;
  6. import org.jruby.RubyProc;
  7. import org.jruby.java.proxies.JavaProxy;
  8. import org.jruby.javasupport.JavaMethod;
  9. import org.jruby.runtime.Block;
  10. import org.jruby.runtime.ThreadContext;
  11. import org.jruby.runtime.builtin.IRubyObject;

  12. public class InstanceMethodInvoker extends MethodInvoker {
  13.     public InstanceMethodInvoker(RubyModule host, List<Method> methods) {
  14.         super(host, methods);
  15.     }

  16.     public InstanceMethodInvoker(RubyModule host, Method method) {
  17.         super(host, method);
  18.     }

  19.     @Override
  20.     public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args) {
  21.         JavaProxy proxy = castJavaProxy(self);

  22.         int len = args.length;
  23.         Object[] convertedArgs = new Object[len];
  24.         JavaMethod method = (JavaMethod)findCallable(self, name, args, len);
  25.         if (method.isVarArgs()) {
  26.             len = method.getParameterTypes().length - 1;
  27.             convertedArgs = new Object[len + 1];
  28.             for (int i = 0; i < len && i < args.length; i++) {
  29.                 convertedArgs[i] = convertArg(args[i], method, i);
  30.             }
  31.             convertedArgs[len] = convertVarargs(args, method);
  32.         } else {
  33.             convertedArgs = new Object[len];
  34.             for (int i = 0; i < len && i < args.length; i++) {
  35.                 convertedArgs[i] = convertArg(args[i], method, i);
  36.             }
  37.         }
  38.         return method.invokeDirect(context, proxy.getObject(), convertedArgs);
  39.     }

  40.     @Override
  41.     public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name) {
  42.         if (javaVarargsCallables != null) return call(context, self, clazz, name, IRubyObject.NULL_ARRAY);
  43.         JavaProxy proxy = castJavaProxy(self);
  44.         JavaMethod method = (JavaMethod)findCallableArityZero(self, name);
  45.         return method.invokeDirect(context, proxy.getObject());
  46.     }

  47.     @Override
  48.     public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0) {
  49.         if (javaVarargsCallables != null) return call(context, self, clazz, name, new IRubyObject[] {arg0});
  50.         JavaProxy proxy = castJavaProxy(self);
  51.         JavaMethod method = (JavaMethod)findCallableArityOne(self, name, arg0);
  52.         Object cArg0 = convertArg(arg0, method, 0);
  53.         return method.invokeDirect(context, proxy.getObject(), cArg0);
  54.     }

  55.     @Override
  56.     public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0, IRubyObject arg1) {
  57.         if (javaVarargsCallables != null) return call(context, self, clazz, name, new IRubyObject[] {arg0, arg1});
  58.         JavaProxy proxy = castJavaProxy(self);
  59.         JavaMethod method = (JavaMethod)findCallableArityTwo(self, name, arg0, arg1);
  60.         Object cArg0 = convertArg(arg0, method, 0);
  61.         Object cArg1 = convertArg(arg1, method, 1);
  62.         return method.invokeDirect(context, proxy.getObject(), cArg0, cArg1);
  63.     }

  64.     @Override
  65.     public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2) {
  66.         if (javaVarargsCallables != null) return call(context, self, clazz, name, new IRubyObject[] {arg0, arg1, arg2});
  67.         JavaProxy proxy = castJavaProxy(self);
  68.         JavaMethod method = (JavaMethod)findCallableArityThree(self, name, arg0, arg1, arg2);
  69.         Object cArg0 = convertArg(arg0, method, 0);
  70.         Object cArg1 = convertArg(arg1, method, 1);
  71.         Object cArg2 = convertArg(arg2, method, 2);
  72.         return method.invokeDirect(context, proxy.getObject(), cArg0, cArg1, cArg2);
  73.     }

  74.     public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args, Block block) {
  75.         if (block.isGiven()) {
  76.             JavaProxy proxy = castJavaProxy(self);
  77.             int len = args.length;
  78.             // these extra arrays are really unfortunate; split some of these paths out to eliminate?
  79.             Object[] convertedArgs = new Object[len + 1];
  80.             IRubyObject[] intermediate = new IRubyObject[len + 1];
  81.             System.arraycopy(args, 0, intermediate, 0, len);
  82.             intermediate[len] = RubyProc.newProc(context.runtime, block, block.type);
  83.             JavaMethod method = (JavaMethod)findCallable(self, name, intermediate, len + 1);
  84.             for (int i = 0; i < len + 1; i++) {
  85.                 convertedArgs[i] = convertArg(intermediate[i], method, i);
  86.             }
  87.             return method.invokeDirect(context, proxy.getObject(), convertedArgs);
  88.         } else {
  89.             return call(context, self, clazz, name, args);
  90.         }
  91.     }

  92.     @Override
  93.     public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, Block block) {
  94.         if (block.isGiven()) {
  95.             JavaProxy proxy = castJavaProxy(self);
  96.             RubyProc proc = RubyProc.newProc(context.runtime, block, block.type);
  97.             JavaMethod method = (JavaMethod)findCallableArityOne(self, name, proc);
  98.             Object cArg0 = convertArg(proc, method, 0);
  99.             return method.invokeDirect(context, proxy.getObject(), cArg0);
  100.         } else {
  101.             return call(context, self, clazz, name);
  102.         }
  103.     }

  104.     @Override
  105.     public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0, Block block) {
  106.         if (block.isGiven()) {
  107.             JavaProxy proxy = castJavaProxy(self);
  108.             RubyProc proc = RubyProc.newProc(context.runtime, block, block.type);
  109.             JavaMethod method = (JavaMethod)findCallableArityTwo(self, name, arg0, proc);
  110.             Object cArg0 = convertArg(arg0, method, 0);
  111.             Object cArg1 = convertArg(proc, method, 1);
  112.             return method.invokeDirect(context, proxy.getObject(), cArg0, cArg1);
  113.         } else {
  114.             return call(context, self, clazz, name, arg0);
  115.         }
  116.     }

  117.     @Override
  118.     public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0, IRubyObject arg1, Block block) {
  119.         if (block.isGiven()) {
  120.             JavaProxy proxy = castJavaProxy(self);
  121.             RubyProc proc = RubyProc.newProc(context.runtime, block, block.type);
  122.             JavaMethod method = (JavaMethod)findCallableArityThree(self, name, arg0, arg1, proc);
  123.             Object cArg0 = convertArg(arg0, method, 0);
  124.             Object cArg1 = convertArg(arg1, method, 1);
  125.             Object cArg2 = convertArg(proc, method, 2);
  126.             return method.invokeDirect(context, proxy.getObject(), cArg0, cArg1, cArg2);
  127.         } else {
  128.             return call(context, self, clazz, name, arg0, arg1);
  129.         }
  130.     }

  131.     @Override
  132.     public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2, Block block) {
  133.         if (block.isGiven()) {
  134.             JavaProxy proxy = castJavaProxy(self);
  135.             RubyProc proc = RubyProc.newProc(context.runtime, block, block.type);
  136.             JavaMethod method = (JavaMethod)findCallableArityFour(self, name, arg0, arg1, arg2, proc);
  137.             Object cArg0 = convertArg(arg0, method, 0);
  138.             Object cArg1 = convertArg(arg1, method, 1);
  139.             Object cArg2 = convertArg(arg2, method, 2);
  140.             Object cArg3 = convertArg(proc, method, 3);
  141.             return method.invokeDirect(context, proxy.getObject(), cArg0, cArg1, cArg2, cArg3);
  142.         } else {
  143.             return call(context, self, clazz, name, arg0, arg1, arg2);
  144.         }
  145.     }
  146. }