@@ -745,19 +745,31 @@ protected final void appendInsns(MixinTargetContext mixin, MethodNode method) {
745745 MethodNode target = this .findTargetMethod (method );
746746
747747 if (target != null ) {
748- AbstractInsnNode returnNode = Bytecode .findInsn (target , Opcodes .RETURN );
749-
750- if (returnNode != null ) {
748+ List <AbstractInsnNode > returnNodes = Bytecode .findAllInsns (target , Opcodes .RETURN );
749+ if (!returnNodes .isEmpty ()) {
750+ // Replace all existing return instructions with a GOTO to the start of the newly appended code
751+ LabelNode appendedCodeStartLabel = new LabelNode ();
752+ for (AbstractInsnNode returnNode : returnNodes ) {
753+ target .instructions .set (returnNode , new JumpInsnNode (Opcodes .GOTO , appendedCodeStartLabel ));
754+ }
755+ target .instructions .add (appendedCodeStartLabel );
756+
757+ // Append all the new code to the end of the target method, excluding line numbers
751758 Iterator <AbstractInsnNode > injectIter = method .instructions .iterator ();
752759 while (injectIter .hasNext ()) {
753760 AbstractInsnNode insn = injectIter .next ();
754- if (!(insn instanceof LineNumberNode ) && insn .getOpcode () != Opcodes .RETURN ) {
755- target .instructions .insertBefore (returnNode , insn );
761+ if (!(insn instanceof LineNumberNode )) {
762+ injectIter .remove ();
763+ target .instructions .add (insn );
756764 }
757765 }
758766
759767 target .maxLocals = Math .max (target .maxLocals , method .maxLocals );
760768 target .maxStack = Math .max (target .maxStack , method .maxStack );
769+
770+ // Merge incoming try-catch blocks into the target method
771+ target .tryCatchBlocks .addAll (method .tryCatchBlocks );
772+ // We could probably copy over local variable information as well?
761773 }
762774
763775 return ;
0 commit comments