@@ -745,19 +745,31 @@ protected final void appendInsns(MixinTargetContext mixin, MethodNode method) {
745
745
MethodNode target = this .findTargetMethod (method );
746
746
747
747
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
+ method .instructions .set (returnNode , new JumpInsnNode (Opcodes .GOTO , appendedCodeStartLabel ));
754
+ }
755
+ method .instructions .add (appendedCodeStartLabel );
756
+
757
+ // Append all the new code to the end of the target method, excluding line numbers
751
758
Iterator <AbstractInsnNode > injectIter = method .instructions .iterator ();
752
759
while (injectIter .hasNext ()) {
753
760
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 );
756
764
}
757
765
}
758
766
759
767
target .maxLocals = Math .max (target .maxLocals , method .maxLocals );
760
768
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?
761
773
}
762
774
763
775
return ;
0 commit comments