private boolean fixBranches() {
    int size = insns.size();
    boolean anyFixed = false;

    for (int i = 0; i < size; i++) {
      DalvInsn insn = insns.get(i);
      if (!(insn instanceof TargetInsn)) {
        // This loop only needs to inspect TargetInsns.
        continue;
      }

      Dop opcode = insn.getOpcode();
      TargetInsn target = (TargetInsn) insn;

      if (opcode.getFormat().branchFits(target)) {
        continue;
      }

      if (opcode.getFamily() == Opcodes.GOTO) {
        // It is a goto; widen it if possible.
        opcode = findOpcodeForInsn(insn, opcode);
        if (opcode == null) {
          /*
           * The branch is already maximally large. This should
           * only be possible if a method somehow manages to have
           * more than 2^31 code units.
           */
          throw new UnsupportedOperationException("method too long");
        }
        insns.set(i, insn.withOpcode(opcode));
      } else {
        /*
         * It is a conditional: Reverse its sense, and arrange for
         * it to branch around an absolute goto to the original
         * branch target.
         *
         * Note: An invariant of the list being processed is
         * that every TargetInsn is followed by a CodeAddress.
         * Hence, it is always safe to get the next element
         * after a TargetInsn and cast it to CodeAddress, as
         * is happening a few lines down.
         *
         * Also note: Size gets incremented by one here, as we
         * have -- in the net -- added one additional element
         * to the list, so we increment i to match. The added
         * and changed elements will be inspected by a repeat
         * call to this method after this invocation returns.
         */
        CodeAddress newTarget;
        try {
          newTarget = (CodeAddress) insns.get(i + 1);
        } catch (IndexOutOfBoundsException ex) {
          // The TargetInsn / CodeAddress invariant was violated.
          throw new IllegalStateException("unpaired TargetInsn (dangling)");
        } catch (ClassCastException ex) {
          // The TargetInsn / CodeAddress invariant was violated.
          throw new IllegalStateException("unpaired TargetInsn");
        }
        TargetInsn gotoInsn =
            new TargetInsn(
                Dops.GOTO, target.getPosition(), RegisterSpecList.EMPTY, target.getTarget());
        insns.set(i, gotoInsn);
        insns.add(i, target.withNewTargetAndReversed(newTarget));
        size++;
        i++;
      }

      anyFixed = true;
    }

    return anyFixed;
  }
Helper for {@link #assignAddressesAndFixBranches}, which checks the branch target size
requirement of each branch instruction to make sure it fits. For instructions that don't fit,
this rewrites them to use a {@code goto} of some sort. In the case of a conditional branch that
doesn't fit, the sense of the test is reversed in order to branch around a {@code goto} to the
original target.

@return whether any branches had to be fixed
android/android_framework/dex-maker/src/main/java/com/android/dx/dex/code/OutputFinisher.java