private static InsnList extractSuperCall()

in TransformCore/src/main/java/com/facebook/ads/injkit/benchmark/BenchmarkInjector.java [491:533]


  private static InsnList extractSuperCall(InsnList instructions, String superIName) {
    AbstractInsnNode loadNode = null;
    AbstractInsnNode superNode = null;

    // Super call goes all the way from the first ALOAD0 to the actual INVOKESPECIAL.

    for (int i = 0; i < instructions.size(); i++) {
      AbstractInsnNode n = instructions.get(i);
      if (n.getOpcode() == Opcodes.ALOAD && loadNode == null && ((VarInsnNode) n).var == 0) {
        loadNode = n;
        continue;
      }

      if (loadNode != null
          && n.getOpcode() == Opcodes.INVOKESPECIAL
          && AsmMethodUtils.isConstructorName(((MethodInsnNode) n).name)
          && ((MethodInsnNode) n).owner.equals(superIName)) {
        if (superNode == null) {
          superNode = n;
        } else {
          // Ooops, more than one invokespecial of the superclass found.
          return null;
        }
      }
    }

    if (superNode == null) {
      // Ooops, super constructor not found.
      return null;
    }

    int loadIdx = instructions.indexOf(loadNode);
    int superIdx = instructions.indexOf(superNode);

    InsnList newInstructions = new InsnList();
    for (int i = loadIdx; i <= superIdx; i++) {
      AbstractInsnNode n = instructions.get(loadIdx);
      instructions.remove(n);
      newInstructions.add(n);
    }

    return newInstructions;
  }