private IRubyObject callEachType(
     MethodType type, IRubyObject rubyReceiver, String methodName, Block block, Object... args) {
   Ruby runtime = rubyReceiver.getRuntime();
   IRubyObject[] rubyArgs = null;
   if (args != null && args.length > 0) {
     rubyArgs = JavaUtil.convertJavaArrayToRuby(runtime, args);
     for (int i = 0; i < rubyArgs.length; i++) {
       IRubyObject obj = rubyArgs[i];
       if (obj instanceof JavaObject) {
         rubyArgs[i] = Java.wrap(runtime, obj);
       }
     }
   }
   ThreadContext context = runtime.getCurrentContext();
   switch (type) {
     case CALLMETHOD_NOARG:
       return RuntimeHelpers.invoke(context, rubyReceiver, methodName);
     case CALLMETHOD:
       return RuntimeHelpers.invoke(context, rubyReceiver, methodName, rubyArgs);
     case CALLMETHOD_WITHBLOCK:
       return RuntimeHelpers.invoke(context, rubyReceiver, methodName, rubyArgs, block);
     case CALLSUPER:
       return RuntimeHelpers.invokeSuper(context, rubyReceiver, rubyArgs, Block.NULL_BLOCK);
     case CALLSUPER_WITHBLOCK:
       return RuntimeHelpers.invokeSuper(context, rubyReceiver, rubyArgs, block);
     default:
       break;
   }
   return null;
 }
Beispiel #2
0
 @JRubyMethod(frame = true, meta = true)
 public static IRubyObject inherited(
     ThreadContext context, IRubyObject recv, IRubyObject subclass) {
   IRubyObject subJavaClass = RuntimeHelpers.invoke(context, subclass, "java_class");
   if (subJavaClass.isNil()) {
     subJavaClass = RuntimeHelpers.invoke(context, recv, "java_class");
     RuntimeHelpers.invoke(context, subclass, "java_class=", subJavaClass);
   }
   return RuntimeHelpers.invokeSuper(context, recv, subclass, Block.NULL_BLOCK);
 }
Beispiel #3
0
 @JRubyMethod(name = "[]", meta = true, rest = true)
 public static IRubyObject op_aref(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
   IRubyObject javaClass = RuntimeHelpers.invoke(context, recv, "java_class");
   if (args.length > 0) {
     // construct new array proxy (ArrayJavaProxy)
     ArrayJavaProxyCreator ajpc = new ArrayJavaProxyCreator(context.runtime);
     ajpc.setup(context, javaClass, args);
     return ajpc;
   } else {
     return Java.get_proxy_class(
         javaClass, RuntimeHelpers.invoke(context, javaClass, "array_class"));
   }
 }
Beispiel #4
0
  private static Class<?> getJavaClass(ThreadContext context, RubyModule module) {
    try {
      IRubyObject jClass = RuntimeHelpers.invoke(context, module, "java_class");

      return !(jClass instanceof JavaClass) ? null : ((JavaClass) jClass).javaClass();
    } catch (Exception e) {
      return null;
    }
  }
Beispiel #5
0
 @JRubyMethod(name = "open", required = 1, frame = true, meta = true)
 public static IRubyObject open(
     final ThreadContext context, IRubyObject recv, IRubyObject filename, Block block)
     throws IOException {
   Ruby runtime = recv.getRuntime();
   IRubyObject io =
       RuntimeHelpers.invoke(
           context, runtime.getFile(), "open", filename, runtime.newString("rb"));
   RubyGzipReader gzio = newInstance(recv, new IRubyObject[] {io}, block);
   return RubyGzipFile.wrapBlock(context, gzio, block);
 }
Beispiel #6
0
    @Override
    public IRubyObject op_aset(ThreadContext context, IRubyObject key, IRubyObject value) {
      if (!key.respondsTo("to_str")) {
        throw getRuntime().newTypeError("can't convert " + key.getMetaClass() + " into String");
      }
      if (!value.respondsTo("to_str") && !value.isNil()) {
        throw getRuntime().newTypeError("can't convert " + value.getMetaClass() + " into String");
      }

      RubyString actualKey = getCorrectKey(key, context);

      if (value.isNil()) {
        return super.delete(context, actualKey, org.jruby.runtime.Block.NULL_BLOCK);
      }

      // return super.aset(getRuntime().newString("sadfasdF"), getRuntime().newString("sadfasdF"));
      return super.op_aset(
          context,
          RuntimeHelpers.invoke(context, actualKey, "to_str"),
          value.isNil() ? getRuntime().getNil() : RuntimeHelpers.invoke(context, value, "to_str"));
    }
 public void onSignalStrengthsChanged(android.telephony.SignalStrength signalStrength) {
   if (callbackProcs[CB_SIGNAL_STRENGTHS_CHANGED] != null) {
     try {
       RuntimeHelpers.invoke(
           getRuby().getCurrentContext(),
           callbackProcs[CB_SIGNAL_STRENGTHS_CHANGED],
           "call",
           JavaUtil.convertJavaToRuby(getRuby(), signalStrength));
     } catch (RaiseException re) {
       re.printStackTrace();
     }
   }
 }
 public void onDrawFrame(javax.microedition.khronos.opengles.GL10 gl) {
   if (callbackProcs[CB_DRAW_FRAME] != null) {
     try {
       RuntimeHelpers.invoke(
           getRuby().getCurrentContext(),
           callbackProcs[CB_DRAW_FRAME],
           "call",
           JavaUtil.convertJavaToRuby(getRuby(), gl));
     } catch (RaiseException re) {
       re.printStackTrace();
     }
   }
 }
Beispiel #9
0
  /*
   * call-seq:
   *  new(version = default)
   *
   * Create a new document with +version+ (defaults to "1.0")
   */
  @JRubyMethod(name = "new", meta = true, rest = true, required = 0)
  public static IRubyObject rbNew(ThreadContext context, IRubyObject cls, IRubyObject[] args) {
    XmlDocument doc = null;
    try {
      Document docNode = createNewDocument();
      doc = new XmlDocument(context.getRuntime(), (RubyClass) cls, docNode);
    } catch (Exception ex) {
      throw context.getRuntime().newRuntimeError("couldn't create document: " + ex.toString());
    }

    RuntimeHelpers.invoke(context, doc, "initialize", args);

    return doc;
  }
 public void onClick(android.content.DialogInterface dialog, int which) {
   if (callbackProcs[CB_CLICK] != null) {
     try {
       RuntimeHelpers.invoke(
           getRuby().getCurrentContext(),
           callbackProcs[CB_CLICK],
           "call",
           JavaUtil.convertJavaToRuby(getRuby(), dialog),
           JavaUtil.convertJavaToRuby(getRuby(), which));
     } catch (RaiseException re) {
       re.printStackTrace();
     }
   }
 }
Beispiel #11
0
 @Override
 public void setNode(ThreadContext context, Node node) {
   this.node = node;
   if (node != null) {
     resetCache();
     if (node.getNodeType() != Node.DOCUMENT_NODE) {
       doc = document(context);
       setInstanceVariable("@document", doc);
       if (doc != null) {
         RuntimeHelpers.invoke(context, doc, "decorate", this);
       }
     }
   }
 }
 public void onDataConnectionStateChanged(int state, int networkType) {
   if (callbackProcs[CB_DATA_CONNECTION_STATE_CHANGED] != null) {
     try {
       RuntimeHelpers.invoke(
           getRuby().getCurrentContext(),
           callbackProcs[CB_DATA_CONNECTION_STATE_CHANGED],
           "call",
           JavaUtil.convertJavaToRuby(getRuby(), state),
           JavaUtil.convertJavaToRuby(getRuby(), networkType));
     } catch (RaiseException re) {
       re.printStackTrace();
     }
   }
 }
 public void onSurfaceChanged(javax.microedition.khronos.opengles.GL10 gl, int width, int height) {
   if (callbackProcs[CB_SURFACE_CHANGED] != null) {
     try {
       RuntimeHelpers.invoke(
           getRuby().getCurrentContext(),
           callbackProcs[CB_SURFACE_CHANGED],
           "call",
           JavaUtil.convertJavaToRuby(getRuby(), gl),
           JavaUtil.convertJavaToRuby(getRuby(), width),
           JavaUtil.convertJavaToRuby(getRuby(), height));
     } catch (RaiseException re) {
       re.printStackTrace();
     }
   }
 }
 public void onSurfaceCreated(
     javax.microedition.khronos.opengles.GL10 gl,
     javax.microedition.khronos.egl.EGLConfig config) {
   if (callbackProcs[CB_SURFACE_CREATED] != null) {
     try {
       RuntimeHelpers.invoke(
           getRuby().getCurrentContext(),
           callbackProcs[CB_SURFACE_CREATED],
           "call",
           JavaUtil.convertJavaToRuby(getRuby(), gl),
           JavaUtil.convertJavaToRuby(getRuby(), config));
     } catch (RaiseException re) {
       re.printStackTrace();
     }
   }
 }
 public void onDataActivity(int direction) {
   if (callbackProcs[CB_DATA_ACTIVITY] != null) {
     super.onDataActivity(direction);
     try {
       RuntimeHelpers.invoke(
           getRuby().getCurrentContext(),
           callbackProcs[CB_DATA_ACTIVITY],
           "call",
           JavaUtil.convertJavaToRuby(getRuby(), direction));
     } catch (RaiseException re) {
       re.printStackTrace();
     }
   } else {
     super.onDataActivity(direction);
   }
 }
 public void onCellLocationChanged(android.telephony.CellLocation location) {
   if (callbackProcs[CB_CELL_LOCATION_CHANGED] != null) {
     super.onCellLocationChanged(location);
     try {
       RuntimeHelpers.invoke(
           getRuby().getCurrentContext(),
           callbackProcs[CB_CELL_LOCATION_CHANGED],
           "call",
           JavaUtil.convertJavaToRuby(getRuby(), location));
     } catch (RaiseException re) {
       re.printStackTrace();
     }
   } else {
     super.onCellLocationChanged(location);
   }
 }
 public void onCallForwardingIndicatorChanged(boolean cfi) {
   if (callbackProcs[CB_CALL_FORWARDING_INDICATOR_CHANGED] != null) {
     super.onCallForwardingIndicatorChanged(cfi);
     try {
       RuntimeHelpers.invoke(
           getRuby().getCurrentContext(),
           callbackProcs[CB_CALL_FORWARDING_INDICATOR_CHANGED],
           "call",
           JavaUtil.convertJavaToRuby(getRuby(), cfi));
     } catch (RaiseException re) {
       re.printStackTrace();
     }
   } else {
     super.onCallForwardingIndicatorChanged(cfi);
   }
 }
 public void onSignalStrengthChanged(int asu) {
   if (callbackProcs[CB_SIGNAL_STRENGTH_CHANGED] != null) {
     super.onSignalStrengthChanged(asu);
     try {
       RuntimeHelpers.invoke(
           getRuby().getCurrentContext(),
           callbackProcs[CB_SIGNAL_STRENGTH_CHANGED],
           "call",
           JavaUtil.convertJavaToRuby(getRuby(), asu));
     } catch (RaiseException re) {
       re.printStackTrace();
     }
   } else {
     super.onSignalStrengthChanged(asu);
   }
 }
 public void onServiceStateChanged(android.telephony.ServiceState serviceState) {
   if (callbackProcs[CB_SERVICE_STATE_CHANGED] != null) {
     super.onServiceStateChanged(serviceState);
     try {
       RuntimeHelpers.invoke(
           getRuby().getCurrentContext(),
           callbackProcs[CB_SERVICE_STATE_CHANGED],
           "call",
           JavaUtil.convertJavaToRuby(getRuby(), serviceState));
     } catch (RaiseException re) {
       re.printStackTrace();
     }
   } else {
     super.onServiceStateChanged(serviceState);
   }
 }
 public void onMessageWaitingIndicatorChanged(boolean mwi) {
   if (callbackProcs[CB_MESSAGE_WAITING_INDICATOR_CHANGED] != null) {
     super.onMessageWaitingIndicatorChanged(mwi);
     try {
       RuntimeHelpers.invoke(
           getRuby().getCurrentContext(),
           callbackProcs[CB_MESSAGE_WAITING_INDICATOR_CHANGED],
           "call",
           JavaUtil.convertJavaToRuby(getRuby(), mwi));
     } catch (RaiseException re) {
       re.printStackTrace();
     }
   } else {
     super.onMessageWaitingIndicatorChanged(mwi);
   }
 }
 public void onCallStateChanged(int state, java.lang.String incomingNumber) {
   if (callbackProcs[CB_CALL_STATE_CHANGED] != null) {
     super.onCallStateChanged(state, incomingNumber);
     try {
       RuntimeHelpers.invoke(
           getRuby().getCurrentContext(),
           callbackProcs[CB_CALL_STATE_CHANGED],
           "call",
           JavaUtil.convertJavaToRuby(getRuby(), state),
           JavaUtil.convertJavaToRuby(getRuby(), incomingNumber));
     } catch (RaiseException re) {
       re.printStackTrace();
     }
   } else {
     super.onCallStateChanged(state, incomingNumber);
   }
 }
Beispiel #22
0
    @JRubyMethod(name = "open", required = 1, optional = 2, frame = true, meta = true)
    public static IRubyObject open(
        final ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block)
        throws IOException {
      Ruby runtime = recv.getRuntime();
      IRubyObject level = runtime.getNil();
      IRubyObject strategy = runtime.getNil();

      if (args.length > 1) {
        level = args[1];
        if (args.length > 2) strategy = args[2];
      }

      IRubyObject io =
          RuntimeHelpers.invoke(
              context, runtime.getFile(), "open", args[0], runtime.newString("wb"));
      RubyGzipWriter gzio = newGzipWriter(recv, new IRubyObject[] {io, level, strategy}, block);
      return RubyGzipFile.wrapBlock(context, gzio, block);
    }
Beispiel #23
0
  @JRubyMethod
  public IRubyObject parse(ThreadContext context, IRubyObject target) {
    Ruby runtime = context.runtime;

    // FIXME? only supports Unicode, since we have to produces strings...
    StreamReader reader;
    if (target.respondsTo("read")) {
      reader = new StreamReader(new InputStreamReader(new IOInputStream(target)));
    } else {
      reader = new StreamReader(new StringReader(target.convertToString().asJavaString()));
    }
    Parser parser = new ParserImpl(reader);
    IRubyObject handler = getInstanceVariable("@handler");
    Event event;

    while (true) {
      try {
        event = parser.getEvent();

        // FIXME: Event should expose a getID, so it can be switched
        if (event.is(ID.StreamStart)) {
          invoke(context, handler, "start_stream", runtime.newFixnum(YAML_ANY_ENCODING));
        } else if (event.is(ID.DocumentStart)) {
          DocumentStartEvent dse = (DocumentStartEvent) event;

          Integer[] versionInts = dse.getVersion();
          IRubyObject version =
              versionInts == null
                  ? runtime.getNil()
                  : RubyArray.newArray(
                      runtime,
                      runtime.newFixnum(versionInts[0]),
                      runtime.newFixnum(versionInts[1]));

          Map<String, String> tagsMap = dse.getTags();
          RubyArray tags = RubyArray.newArray(runtime);
          if (tags.size() > 0) {
            for (Map.Entry<String, String> tag : tagsMap.entrySet()) {
              tags.append(
                  RubyArray.newArray(
                      runtime,
                      RubyString.newString(runtime, tag.getKey()),
                      RubyString.newString(runtime, tag.getValue())));
            }
          }

          invoke(
              context,
              handler,
              "start_document",
              version,
              tags,
              runtime.newBoolean(dse.getExplicit()));
        } else if (event.is(ID.DocumentEnd)) {
          DocumentEndEvent dee = (DocumentEndEvent) event;
          invoke(context, handler, "end_document", runtime.newBoolean(dee.getExplicit()));
        } else if (event.is(ID.Alias)) {
          AliasEvent ae = (AliasEvent) event;
          IRubyObject alias = runtime.getNil();
          if (ae.getAnchor() != null) {
            alias = RubyString.newString(runtime, ae.getAnchor());
          }

          invoke(context, handler, "alias", alias);
        } else if (event.is(ID.Scalar)) {
          ScalarEvent se = (ScalarEvent) event;
          IRubyObject anchor =
              se.getAnchor() == null
                  ? runtime.getNil()
                  : RubyString.newString(runtime, se.getAnchor());
          IRubyObject tag =
              se.getTag() == null ? runtime.getNil() : RubyString.newString(runtime, se.getTag());
          IRubyObject plain_implicit = runtime.newBoolean(se.getImplicit().isFirst());
          IRubyObject quoted_implicit = runtime.newBoolean(se.getImplicit().isSecond());
          IRubyObject style = runtime.newFixnum(se.getStyle());
          IRubyObject val = RubyString.newString(runtime, se.getValue());

          invoke(
              context, handler, "scalar", val, anchor, tag, plain_implicit, quoted_implicit, style);
        } else if (event.is(ID.SequenceStart)) {
          SequenceStartEvent sse = (SequenceStartEvent) event;
          IRubyObject anchor =
              sse.getAnchor() == null
                  ? runtime.getNil()
                  : RubyString.newString(runtime, sse.getAnchor());
          IRubyObject tag =
              sse.getTag() == null ? runtime.getNil() : RubyString.newString(runtime, sse.getTag());
          IRubyObject implicit = runtime.newBoolean(sse.getImplicit());
          IRubyObject style = runtime.newFixnum(sse.getFlowStyle() ? 1 : 0);

          invoke(context, handler, "start_sequence", anchor, tag, implicit, style);
        } else if (event.is(ID.SequenceEnd)) {
          invoke(context, handler, "end_sequence");
        } else if (event.is(ID.MappingStart)) {
          MappingStartEvent mse = (MappingStartEvent) event;
          IRubyObject anchor =
              mse.getAnchor() == null
                  ? runtime.getNil()
                  : RubyString.newString(runtime, mse.getAnchor());
          IRubyObject tag =
              mse.getTag() == null ? runtime.getNil() : RubyString.newString(runtime, mse.getTag());
          IRubyObject implicit = runtime.newBoolean(mse.getImplicit());
          IRubyObject style = runtime.newFixnum(mse.getFlowStyle() ? 1 : 0);

          invoke(context, handler, "start_mapping", anchor, tag, implicit, style);
        } else if (event.is(ID.MappingEnd)) {
          invoke(context, handler, "end_mapping");
        } else if (event.is(ID.StreamEnd)) {
          invoke(context, handler, "end_stream");
          break;
        }
      } catch (ParserException pe) {
        parser = null;
        RubyKernel.raise(
            context,
            runtime.getModule("Psych").getConstant("SyntaxError"),
            new IRubyObject[] {runtime.newString(pe.getLocalizedMessage())},
            Block.NULL_BLOCK);
      }
    }

    return this;
  }