コード例 #1
0
  public Object call(
      final Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) {

    JavaObjectWrapper jcw = (JavaObjectWrapper) thisObj;
    Method method = this.getBestMethod(args);
    if (method == null) {
      throw new EvaluatorException(
          "No method matching "
              + this.className
              + " with "
              + (args == null ? 0 : args.length)
              + " arguments.");
    }
    Class[] actualArgTypes = method.getParameterTypes();
    int numParams = actualArgTypes.length;
    Object[] actualArgs = args == null ? new Object[0] : new Object[numParams];
    boolean linfo = loggableInfo;
    if (linfo) {
      Object javaObject = jcw.getJavaObject();
      logger.info(
          "call(): Calling method "
              + method.getName()
              + " on object "
              + javaObject
              + " of type "
              + this.getTypeName(javaObject));
    }
    JavaScript manager = JavaScript.getInstance();
    for (int i = 0; i < numParams; i++) {
      Object arg = args[i];
      Object actualArg = manager.getJavaObject(arg, actualArgTypes[i]);
      if (linfo) {
        logger.info(
            "call(): For method="
                + method.getName()
                + ": Converted arg="
                + arg
                + " (type="
                + this.getTypeName(arg)
                + ") into actualArg="
                + actualArg
                + ". Type expected by method is "
                + actualArgTypes[i].getName()
                + ".");
      }
      actualArgs[i] = actualArg;
    }
    try {
      Object raw = method.invoke(jcw.getJavaObject(), actualArgs);
      return manager.getJavascriptObject(raw, scope);
    } catch (IllegalAccessException iae) {
      throw new IllegalStateException("Unable to call " + this.className + ".", iae);
    } catch (InvocationTargetException ite) {
      throw new WrappedException(
          new InvocationTargetException(
              ite.getCause(),
              "Unable to call " + this.className + " on " + jcw.getJavaObject() + "."));
    } catch (IllegalArgumentException iae) {
      StringBuffer argTypes = new StringBuffer();
      for (int i = 0; i < actualArgs.length; i++) {
        if (i > 0) {
          argTypes.append(", ");
        }
        argTypes.append(actualArgs[i] == null ? "<null>" : actualArgs[i].getClass().getName());
      }
      throw new WrappedException(
          new IllegalArgumentException(
              "Unable to call " + this.className + ". Argument types: " + argTypes + ".", iae));
    }
  }