Ejemplo n.º 1
0
 /** Returns a fully qualified method name concatenated with its signature. */
 private static String getMethodSignature(Method method, Class[] argTypes) {
   StringBuffer sb = new StringBuffer();
   appendMethodSignature(argTypes, method.getReturnType(), sb);
   return sb.toString();
 }
Ejemplo n.º 2
0
  private static void generateMethod(
      ClassFileWriter cfw, String genName, String methodName, Class[] parms, Class returnType) {
    StringBuffer sb = new StringBuffer();
    int paramsEnd = appendMethodSignature(parms, returnType, sb);
    String methodSignature = sb.toString();
    cfw.startMethod(methodName, methodSignature, ClassFileWriter.ACC_PUBLIC);

    // Prepare stack to call method

    // push factory
    cfw.add(ByteCode.ALOAD_0);
    cfw.add(ByteCode.GETFIELD, genName, "factory", "Lorg/mozilla/javascript/ContextFactory;");

    // push self
    cfw.add(ByteCode.ALOAD_0);
    cfw.add(ByteCode.GETFIELD, genName, "self", "Lorg/mozilla/javascript/Scriptable;");

    // push function
    cfw.add(ByteCode.ALOAD_0);
    cfw.add(ByteCode.GETFIELD, genName, "delegee", "Lorg/mozilla/javascript/Scriptable;");
    cfw.addPush(methodName);
    cfw.addInvoke(
        ByteCode.INVOKESTATIC,
        "org/mozilla/javascript/JavaAdapter",
        "getFunction",
        "(Lorg/mozilla/javascript/Scriptable;"
            + "Ljava/lang/String;"
            + ")Lorg/mozilla/javascript/Function;");

    // push arguments
    generatePushWrappedArgs(cfw, parms, parms.length);

    // push bits to indicate which parameters should be wrapped
    if (parms.length > 64) {
      // If it will be an issue, then passing a static boolean array
      // can be an option, but for now using simple bitmask
      throw Context.reportRuntimeError0(
          "JavaAdapter can not subclass methods with more then" + " 64 arguments.");
    }
    long convertionMask = 0;
    for (int i = 0; i != parms.length; ++i) {
      if (!parms[i].isPrimitive()) {
        convertionMask |= (1 << i);
      }
    }
    cfw.addPush(convertionMask);

    // go through utility method, which creates a Context to run the
    // method in.
    cfw.addInvoke(
        ByteCode.INVOKESTATIC,
        "org/mozilla/javascript/JavaAdapter",
        "callMethod",
        "(Lorg/mozilla/javascript/ContextFactory;"
            + "Lorg/mozilla/javascript/Scriptable;"
            + "Lorg/mozilla/javascript/Function;"
            + "[Ljava/lang/Object;"
            + "J"
            + ")Ljava/lang/Object;");

    generateReturnResult(cfw, returnType, true);

    cfw.stopMethod((short) paramsEnd);
  }