예제 #1
0
 static int appendMethodSignature(Class[] argTypes, Class returnType, StringBuffer sb) {
   sb.append('(');
   int firstLocal = 1 + argTypes.length; // includes this.
   for (int i = 0; i < argTypes.length; i++) {
     Class type = argTypes[i];
     appendTypeString(sb, type);
     if (type == Long.TYPE || type == Double.TYPE) {
       // adjust for duble slot
       ++firstLocal;
     }
   }
   sb.append(')');
   appendTypeString(sb, returnType);
   return firstLocal;
 }
예제 #2
0
 private static StringBuffer appendTypeString(StringBuffer sb, Class type) {
   while (type.isArray()) {
     sb.append('[');
     type = type.getComponentType();
   }
   if (type.isPrimitive()) {
     char typeLetter;
     if (type == Boolean.TYPE) {
       typeLetter = 'Z';
     } else if (type == Long.TYPE) {
       typeLetter = 'J';
     } else {
       String typeName = type.getName();
       typeLetter = Character.toUpperCase(typeName.charAt(0));
     }
     sb.append(typeLetter);
   } else {
     sb.append('L');
     sb.append(type.getName().replace('.', '/'));
     sb.append(';');
   }
   return sb;
 }
예제 #3
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();
 }
예제 #4
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);
  }