Beispiel #1
0
  private static CtMethod delegator0(CtMethod delegate, CtClass declaring)
      throws CannotCompileException, NotFoundException {
    MethodInfo deleInfo = delegate.getMethodInfo2();
    String methodName = deleInfo.getName();
    String desc = deleInfo.getDescriptor();
    ConstPool cp = declaring.getClassFile2().getConstPool();
    MethodInfo minfo = new MethodInfo(cp, methodName, desc);
    minfo.setAccessFlags(deleInfo.getAccessFlags());

    ExceptionsAttribute eattr = deleInfo.getExceptionsAttribute();
    if (eattr != null) minfo.setExceptionsAttribute((ExceptionsAttribute) eattr.copy(cp, null));

    Bytecode code = new Bytecode(cp, 0, 0);
    boolean isStatic = Modifier.isStatic(delegate.getModifiers());
    CtClass deleClass = delegate.getDeclaringClass();
    CtClass[] params = delegate.getParameterTypes();
    int s;
    if (isStatic) {
      s = code.addLoadParameters(params, 0);
      code.addInvokestatic(deleClass, methodName, desc);
    } else {
      code.addLoad(0, deleClass);
      s = code.addLoadParameters(params, 1);
      code.addInvokespecial(deleClass, methodName, desc);
    }

    code.addReturn(delegate.getReturnType());
    code.setMaxLocals(++s);
    code.setMaxStack(s < 2 ? 2 : s); // for a 2-word return value
    minfo.setCodeAttribute(code.toCodeAttribute());
    return new CtMethod(minfo, declaring);
  }
Beispiel #2
0
  /* MemberCodeGen overrides this method.
   */
  protected void atClassObject2(String cname) throws CompileError {
    int start = bytecode.currentPc();
    bytecode.addLdc(cname);
    bytecode.addInvokestatic("java.lang.Class", "forName", "(Ljava/lang/String;)Ljava/lang/Class;");
    int end = bytecode.currentPc();
    bytecode.addOpcode(Opcode.GOTO);
    int pc = bytecode.currentPc();
    bytecode.addIndex(0); // correct later

    bytecode.addExceptionHandler(
        start, end, bytecode.currentPc(), "java.lang.ClassNotFoundException");

    /* -- the following code is for inlining a call to DotClass.fail().

    int var = getMaxLocals();
    incMaxLocals(1);
    bytecode.growStack(1);
    bytecode.addAstore(var);

    bytecode.addNew("java.lang.NoClassDefFoundError");
    bytecode.addOpcode(DUP);
    bytecode.addAload(var);
    bytecode.addInvokevirtual("java.lang.ClassNotFoundException",
                              "getMessage", "()Ljava/lang/String;");
    bytecode.addInvokespecial("java.lang.NoClassDefFoundError", "<init>",
                              "(Ljava/lang/String;)V");
    */

    bytecode.growStack(1);
    bytecode.addInvokestatic(
        "javassist.runtime.DotClass",
        "fail",
        "(Ljava/lang/ClassNotFoundException;)" + "Ljava/lang/NoClassDefFoundError;");
    bytecode.addOpcode(ATHROW);
    bytecode.write16bit(pc, bytecode.currentPc() - pc + 1);
  }
Beispiel #3
0
  private void convToString(int type, int dim) throws CompileError {
    final String method = "valueOf";

    if (isRefType(type) || dim > 0)
      bytecode.addInvokestatic(javaLangString, method, "(Ljava/lang/Object;)Ljava/lang/String;");
    else if (type == DOUBLE)
      bytecode.addInvokestatic(javaLangString, method, "(D)Ljava/lang/String;");
    else if (type == FLOAT)
      bytecode.addInvokestatic(javaLangString, method, "(F)Ljava/lang/String;");
    else if (type == LONG)
      bytecode.addInvokestatic(javaLangString, method, "(J)Ljava/lang/String;");
    else if (type == BOOLEAN)
      bytecode.addInvokestatic(javaLangString, method, "(Z)Ljava/lang/String;");
    else if (type == CHAR)
      bytecode.addInvokestatic(javaLangString, method, "(C)Ljava/lang/String;");
    else if (type == VOID) throw new CompileError("void type expression");
    else /* INT, BYTE, SHORT */
      bytecode.addInvokestatic(javaLangString, method, "(I)Ljava/lang/String;");
  }