/**
   * Generates the Java bytecode corresponding to this list of bytecodes. This just calls {@code
   * bytecode.NonBranchingBytecode.generateJavaBytecode(JavaClassGenerator)} on each non-branching
   * bytecode in the list and appends the results.
   *
   * @param classGen the Java class generator to be used for this generation
   * @return the Java bytecode corresponding to this list of bytecodes
   */
  public InstructionList generateJavaBytecode(JavaClassGenerator classGen) {
    InstructionList result;

    if (head instanceof NonBranchingBytecode)

      // generiamo il Java bytecode dal primo bytecode, se non è una condizione di un ramo
      result = ((NonBranchingBytecode) head).generateJavaBytecode(classGen);
    else result = new InstructionList();

    // e per quelli successivi, se esistono
    if (tail != null) result.append(tail.generateJavaBytecode(classGen));

    // se non sono state aggiunte istruzioni, ne aggiungiamo una fittizia cosi da ritornare sempre
    // una lista non vuota
    if (result.isEmpty()) result.append(new org.apache.bcel.generic.NOP());

    return result;
  }
Exemplo n.º 2
0
  /**
   * Replace instruction ih in list il with the instructions in new_il. If new_il is null, do
   * nothing
   */
  protected static void replace_instructions(
      InstructionList il, InstructionHandle ih, InstructionList new_il) {

    if ((new_il == null) || new_il.isEmpty()) return;

    // If there is only one new instruction, just replace it in the handle
    if (new_il.getLength() == 1) {
      ih.setInstruction(new_il.getEnd().getInstruction());
      return;
    }

    // Get the start and end instruction of the new instructions
    InstructionHandle new_end = new_il.getEnd();
    InstructionHandle new_start = il.insert(ih, new_il);

    // Move all of the branches from the old instruction to the new start
    il.redirectBranches(ih, new_start);

    // Move other targets to the new instuctions.
    if (ih.hasTargeters()) {
      for (InstructionTargeter it : ih.getTargeters()) {
        if (it instanceof LineNumberGen) {
          it.updateTarget(ih, new_start);
        } else if (it instanceof LocalVariableGen) {
          it.updateTarget(ih, new_end);
        } else if (it instanceof CodeExceptionGen) {
          CodeExceptionGen exc = (CodeExceptionGen) it;
          if (exc.getStartPC() == ih) exc.updateTarget(ih, new_start);
          else if (exc.getEndPC() == ih) exc.updateTarget(ih, new_end);
          else if (exc.getHandlerPC() == ih) exc.setHandlerPC(new_start);
          else System.out.printf("Malformed CodeException: %s%n", exc);
        } else {
          System.out.printf("unexpected target %s%n", it);
        }
      }
    }

    // Remove the old handle.  There should be no targeters left to it.
    try {
      il.delete(ih);
    } catch (Exception e) {
      throw new Error("Can't delete instruction", e);
    }
  }