Exemple #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);
  }
  private void read(MethodInfo src, String methodname, Map classnames) throws BadBytecode {
    ConstPool destCp = constPool;
    accessFlags = src.accessFlags;
    name = destCp.addUtf8Info(methodname);
    cachedName = methodname;
    ConstPool srcCp = src.constPool;
    String desc = srcCp.getUtf8Info(src.descriptor);
    String desc2 = Descriptor.rename(desc, classnames);
    descriptor = destCp.addUtf8Info(desc2);

    attribute = new LinkedList();
    ExceptionsAttribute eattr = src.getExceptionsAttribute();
    if (eattr != null) attribute.add(eattr.copy(destCp, classnames));

    CodeAttribute cattr = src.getCodeAttribute();
    if (cattr != null) attribute.add(cattr.copy(destCp, classnames));
  }