Beispiel #1
0
  /**
   * Appends a gap at the end of the bytecode sequence.
   *
   * @param gapLength gap length
   */
  public void appendGap(int gapLength) {
    byte[] code = bytecode;
    int codeLength = code.length;
    byte[] newcode = new byte[codeLength + gapLength];

    int i;
    for (i = 0; i < codeLength; ++i) newcode[i] = code[i];

    for (i = codeLength; i < codeLength + gapLength; ++i) newcode[i] = NOP;

    codeAttr.setCode(newcode);
    bytecode = newcode;
    endPos = getCodeLength();
  }
Beispiel #2
0
  /**
   * Inserts an inclusive or exclusive gap in front of the instruction at the given index <code>pos
   * </code>. Branch offsets and the exception table in the method body are also updated. The
   * inserted gap is filled with NOP. The gap length may be extended to a multiple of 4.
   *
   * <p>Suppose that the instruction at the given index is at the beginning of a block statement. If
   * the gap is inclusive, then it is included within that block. If the gap is exclusive, then it
   * is excluded from that block.
   *
   * <p>The index at which the gap is actually inserted might be different from pos since some other
   * bytes might be inserted at other positions (e.g. to change <code>GOTO</code> to <code>GOTO_W
   * </code>). The index is available from the <code>Gap</code> object returned by this method.
   *
   * <p>Suppose that the gap is inserted at the position of the next instruction that would be
   * returned by <code>next()</code> (not the last instruction returned by the last call to <code>
   * next()</code>). The next instruction returned by <code>next()</code> after the gap is inserted
   * is still the same instruction. It is not <code>NOP</code> at the first byte of the inserted
   * gap.
   *
   * @param pos the index at which a gap is inserted.
   * @param length gap length.
   * @param exclusive true if exclusive, otherwise false.
   * @return the position and the length of the inserted gap.
   * @since 3.11
   */
  public Gap insertGapAt(int pos, int length, boolean exclusive) throws BadBytecode {
    /** cursorPos indicates the next bytecode whichever exclusive is true or false. */
    Gap gap = new Gap();
    if (length <= 0) {
      gap.position = pos;
      gap.length = 0;
      return gap;
    }

    byte[] c;
    int length2;
    if (bytecode.length + length > Short.MAX_VALUE) {
      // currentPos might change after calling insertGapCore0w().
      c =
          insertGapCore0w(
              bytecode, pos, length, exclusive, get().getExceptionTable(), codeAttr, gap);
      pos = gap.position;
      length2 = length; // == gap.length
    } else {
      int cur = currentPos;
      c = insertGapCore0(bytecode, pos, length, exclusive, get().getExceptionTable(), codeAttr);
      // insertGapCore0() never changes pos.
      length2 = c.length - bytecode.length;
      gap.position = pos;
      gap.length = length2;
      if (cur >= pos) currentPos = cur + length2;

      if (mark > pos || (mark == pos && exclusive)) mark += length2;
    }

    codeAttr.setCode(c);
    bytecode = c;
    endPos = getCodeLength();
    updateCursors(pos, length2);
    return gap;
  }