Ejemplo n.º 1
0
 public void accept(
     Clazz clazz,
     Method method,
     CodeAttribute codeAttribute,
     int offset,
     InstructionVisitor instructionVisitor) {
   instructionVisitor.visitVariableInstruction(clazz, method, codeAttribute, offset, this);
 }
Ejemplo n.º 2
0
  public void visitBranchInstruction(
      Clazz clazz,
      Method method,
      CodeAttribute codeAttribute,
      int offset,
      BranchInstruction branchInstruction) {
    // Check if the instruction is an unconditional goto instruction.
    byte opcode = branchInstruction.opcode;
    if (opcode == InstructionConstants.OP_GOTO || opcode == InstructionConstants.OP_GOTO_W) {
      // Check if the goto instruction points to another simple goto
      // instruction.
      int branchOffset = branchInstruction.branchOffset;
      int targetOffset = offset + branchOffset;

      if (branchOffset != branchInstruction.length(offset)
          && !codeAttributeEditor.isModified(offset)
          && !codeAttributeEditor.isModified(targetOffset)) {
        Instruction targetInstruction = InstructionFactory.create(codeAttribute.code, targetOffset);

        if (targetInstruction.opcode == InstructionConstants.OP_GOTO) {
          // Simplify the goto instruction.
          int targetBranchOffset = ((BranchInstruction) targetInstruction).branchOffset;

          Instruction newBranchInstruction =
              new BranchInstruction(opcode, (branchOffset + targetBranchOffset));
          codeAttributeEditor.replaceInstruction(offset, newBranchInstruction);

          // Visit the instruction, if required.
          if (extraInstructionVisitor != null) {
            extraInstructionVisitor.visitBranchInstruction(
                clazz, method, codeAttribute, offset, branchInstruction);
          }
        }
      }
    }
  }