public static void renameMethodNode(
      String originalParentName,
      String originalMethodName,
      String originalMethodDesc,
      String newParent,
      String newName,
      String newDesc) {
    for (ClassNode c : BytecodeViewer.getLoadedClasses()) {
      for (Object o : c.methods.toArray()) {
        MethodNode m = (MethodNode) o;
        for (AbstractInsnNode i : m.instructions.toArray()) {
          if (i instanceof MethodInsnNode) {
            MethodInsnNode mi = (MethodInsnNode) i;
            if (mi.owner.equals(originalParentName)
                && mi.name.equals(originalMethodName)
                && mi.desc.equals(originalMethodDesc)) {
              if (newParent != null) mi.owner = newParent;
              if (newName != null) mi.name = newName;
              if (newDesc != null) mi.desc = newDesc;
            }
          } else {
            // System.out.println(i.getOpcode()+":"+c.name+":"+m.name);
          }
        }

        if (m.signature != null) {
          if (newName != null) m.signature = m.signature.replace(originalMethodName, newName);
          if (newParent != null) m.signature = m.signature.replace(originalParentName, newParent);
        }

        if (m.name.equals(originalMethodName)
            && m.desc.equals(originalMethodDesc)
            && c.name.equals(originalParentName)) {
          if (newName != null) m.name = newName;
          if (newDesc != null) m.desc = newDesc;
        }
      }
    }
  }
  private void replaceObfuscatedMethodNames(AsmagicDiffVisitor dv) {
    for (MethodNode mn : dv.newMethods) {
      if (mn.visibleAnnotations != null) {
        for (Object oan : mn.visibleAnnotations) {
          AnnotationNode an = (AnnotationNode) oan;
          if (an.desc.contains("AsmagicMethodReplace")) {
            List<Object> vals = an.values;

            String alias = null;
            if (vals != null || vals.size() > 1) {
              alias = (String) vals.get(1);
              dv.methodsToReplace.put(alias, 1);
              mn.name = alias;
            }
          }
        }
      }
    }
  }
  void writeHeader() {
    int access = 0;
    if (statement != null) {
      if (compiler.getScope().isDebugMode()) {
        statement.setDynamicLocal(true);
      }

      switch (statement.getModifier()) {
        case PRIVATE:
          access += Opcodes.ACC_PRIVATE;
          break;
        case PROTECTED:
          access += Opcodes.ACC_PROTECTED;
          break;
        case PUBLIC:
          access += Opcodes.ACC_PUBLIC;
          break;
      }

      if (statement.isStatic()) access += Opcodes.ACC_STATIC;
      // if (statement.isAbstract()) access += Opcodes.ACC_ABSTRACT;
      if (statement.isFinal()) access += Opcodes.ACC_FINAL;

      node.access = access;
      node.name =
          clazz.isSystem() || entity == null
              ? statement.getName().getName()
              : entity.getInternalName();
      node.desc =
          Type.getMethodDescriptor(
              Type.getType(Memory.class),
              Type.getType(Environment.class),
              Type.getType(Memory[].class));

      if (external) {
        node.desc =
            Type.getMethodDescriptor(
                Type.getType(Memory.class),
                Type.getType(Environment.class),
                Type.getType(Memory[].class),
                Type.getType(ArrayMemory.class));
      }
    }

    if (statement != null) {
      LabelNode label = labelStart = writeLabel(node, statement.getMeta().getStartLine());

      if (!statement.isStatic()) addLocalVariable("~this", label, Object.class);

      ExpressionStmtCompiler expressionCompiler = new ExpressionStmtCompiler(this, null);

      addLocalVariable("~env", label, Environment.class); // Environment env
      LocalVariable args = addLocalVariable("~args", label, Memory[].class); // Memory[] arguments

      if (statement.isDynamicLocal()) {
        if (external) addLocalVariable("~passedLocal", label, ArrayMemory.class);

        LocalVariable local = addLocalVariable("~local", label, ArrayMemory.class);

        if (external) {
          expressionCompiler.writeVarLoad("~passedLocal");
          expressionCompiler.writeSysStaticCall(
              ArrayMemory.class, "valueOfRef", ArrayMemory.class, ArrayMemory.class);
          expressionCompiler.setStackPeekAsImmutable();
          expressionCompiler.writeVarStore(local, false, true);
        } else {
          expressionCompiler.writePushConstNull();
          expressionCompiler.writeSysStaticCall(
              ArrayMemory.class, "valueOfRef", ArrayMemory.class, ArrayMemory.class);
          expressionCompiler.setStackPeekAsImmutable();
          expressionCompiler.writeVarStore(local, false, true);
        }
      }

      if (statement.getUses() != null && !statement.getUses().isEmpty()) {
        int i = 0;
        expressionCompiler.writeVarLoad("~this");
        expressionCompiler.writeGetDynamic("uses", Memory[].class);

        for (ArgumentStmtToken argument : statement.getUses()) {
          LocalVariable local;
          if (statement.isDynamicLocal()) {
            expressionCompiler.writeDefineVariable(argument.getName());
            local = getLocalVariable(argument.getName().getName());
          } else {
            local = addLocalVariable(argument.getName().getName(), label, Memory.class);
          }

          if (argument.isReference()) {
            local.setReference(true);
            statement.variable(argument.getName()).setUnstable(true);
          }

          expressionCompiler.writePushDup();
          expressionCompiler.writePushGetFromArray(i, Memory.class);

          if (statement.isDynamicLocal()) {
            expressionCompiler.writeVarAssign(local, argument.getName(), false, false);
          } else {
            expressionCompiler.writeVarStore(local, false, false);
          }

          local.pushLevel();
          i++;
        }
        expressionCompiler.writePopAll(1);
      }

      int i = 0;

      for (ArgumentStmtToken argument : statement.getArguments()) {
        if (argument.isReference()) {
          statement.variable(argument.getName()).setReference(true).setUnstable(true);
        }

        LabelNode next = new LabelNode();

        expressionCompiler.writeDefineVariable(argument.getName());
        LocalVariable local = getLocalVariable(argument.getName().getName());

        if (local != null) {
          expressionCompiler.writeVarLoad(args);
          expressionCompiler.writePushGetFromArray(i, Memory.class);
          expressionCompiler.writeVarAssign(local, argument.getName(), true, false);

          // if length <= i then undefined
          node.instructions.add(new JumpInsnNode(Opcodes.IFNONNULL, next));
          expressionCompiler.stackPop();

          if (argument.getValue() == null) expressionCompiler.writePushNull();
          else expressionCompiler.writeExpression(argument.getValue(), true, false);

          expressionCompiler.writeVarAssign(local, argument.getName(), false, false);
          node.instructions.add(next);

          local.pushLevel();
        }

        i++;
      }
    } else {
      LabelNode label = labelStart = writeLabel(node, clazz.statement.getMeta().getStartLine());
    }
  }