Beispiel #1
0
  public void visitVariableInstruction(
      Clazz clazz,
      Method method,
      CodeAttribute codeAttribute,
      int offset,
      VariableInstruction variableInstruction) {
    int variableSize = variableInstruction.variableIndex + 1;
    if (variableInstruction.isCategory2()) {
      variableSize++;
    }

    if (codeAttribute.u2maxLocals < variableSize) {
      codeAttribute.u2maxLocals = variableSize;

      if (DEBUG) {
        System.out.println(
            "  Max locals: "
                + codeAttribute.u2maxLocals
                + " <- "
                + variableInstruction.toString(offset));
      }
    }
  }
  public void visitVariableInstruction(
      Clazz clazz,
      Method method,
      CodeAttribute codeAttribute,
      int offset,
      VariableInstruction variableInstruction) {
    byte opcode = variableInstruction.opcode;
    if (opcode == InstructionConstants.OP_RET) {
      // Is the return instruction the last instruction of the subroutine?
      if (branchTargetFinder.subroutineEnd(offset) == offset + variableInstruction.length(offset)) {
        if (DEBUG) {
          System.out.println("    Replacing subroutine return at [" + offset + "] by a label");
        }

        // Append a label at this offset instead of the subroutine return.
        codeAttributeComposer.appendLabel(offset);
      } else {
        if (DEBUG) {
          System.out.println(
              "    Replacing subroutine return at [" + offset + "] by a simple branch");
        }

        // Replace the instruction by a branch.
        Instruction replacementInstruction =
            new BranchInstruction(
                    InstructionConstants.OP_GOTO, branchTargetFinder.subroutineEnd(offset) - offset)
                .shrink();

        codeAttributeComposer.appendInstruction(offset, replacementInstruction);
      }
    } else if (branchTargetFinder.isSubroutineStart(offset)) {
      if (DEBUG) {
        System.out.println(
            "    Replacing first subroutine instruction at [" + offset + "] by a label");
      }

      // Append a label at this offset instead of saving the subroutine
      // return address.
      codeAttributeComposer.appendLabel(offset);
    } else {
      // Append the instruction.
      codeAttributeComposer.appendInstruction(offset, variableInstruction);
    }
  }