Skip to content

Commit

Permalink
Copy try-catch blocks when merging static initializers
Browse files Browse the repository at this point in the history
This also causes the merged method to work as expected if the target method contains more than one RETURN instruction.
  • Loading branch information
DaMatrix committed Sep 15, 2023
1 parent 155314e commit d083c5e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -745,19 +745,31 @@ protected final void appendInsns(MixinTargetContext mixin, MethodNode method) {
MethodNode target = this.findTargetMethod(method);

if (target != null) {
AbstractInsnNode returnNode = Bytecode.findInsn(target, Opcodes.RETURN);

if (returnNode != null) {
List<AbstractInsnNode> returnNodes = Bytecode.findAllInsns(target, Opcodes.RETURN);
if (!returnNodes.isEmpty()) {
// Replace all existing return instructions with a GOTO to the start of the newly appended code
LabelNode appendedCodeStartLabel = new LabelNode();
for (AbstractInsnNode returnNode : returnNodes) {
method.instructions.set(returnNode, new JumpInsnNode(Opcodes.GOTO, appendedCodeStartLabel));
}
method.instructions.add(appendedCodeStartLabel);

// Append all the new code to the end of the target method, excluding line numbers
Iterator<AbstractInsnNode> injectIter = method.instructions.iterator();
while (injectIter.hasNext()) {
AbstractInsnNode insn = injectIter.next();
if (!(insn instanceof LineNumberNode) && insn.getOpcode() != Opcodes.RETURN) {
target.instructions.insertBefore(returnNode, insn);
if (!(insn instanceof LineNumberNode)) {
injectIter.remove();
target.instructions.add(insn);
}
}

target.maxLocals = Math.max(target.maxLocals, method.maxLocals);
target.maxStack = Math.max(target.maxStack, method.maxStack);

// Merge incoming try-catch blocks into the target method
target.tryCatchBlocks.addAll(method.tryCatchBlocks);
// We could probably copy over local variable information as well?
}

return;
Expand Down
25 changes: 22 additions & 3 deletions src/main/java/org/spongepowered/asm/util/Bytecode.java
Original file line number Diff line number Diff line change
Expand Up @@ -285,16 +285,35 @@ public static MethodNode findMethod(ClassNode classNode, String name, String des
* @return found node or null if not found
*/
public static AbstractInsnNode findInsn(MethodNode method, int opcode) {
Iterator<AbstractInsnNode> findReturnIter = method.instructions.iterator();
while (findReturnIter.hasNext()) {
AbstractInsnNode insn = findReturnIter.next();
Iterator<AbstractInsnNode> findInsnIter = method.instructions.iterator();
while (findInsnIter.hasNext()) {
AbstractInsnNode insn = findInsnIter.next();
if (insn.getOpcode() == opcode) {
return insn;
}
}
return null;
}

/**
* Find all insn nodes with a matching opcode in the specified method
*
* @param method method to search
* @param opcode opcode to search for
* @return a list containing the found nodes, may be empty if not found
*/
public static List<AbstractInsnNode> findAllInsns(MethodNode method, int opcode) {
List<AbstractInsnNode> insns = new ArrayList<AbstractInsnNode>();
Iterator<AbstractInsnNode> findInsnIter = method.instructions.iterator();
while (findInsnIter.hasNext()) {
AbstractInsnNode insn = findInsnIter.next();
if (insn.getOpcode() == opcode) {
insns.add(insn);
}
}
return insns;
}

/**
* Find the call to <tt>super()</tt> or <tt>this()</tt> in a constructor.
* This attempts to locate the first call to <tt>&lt;init&gt;</tt> which
Expand Down

0 comments on commit d083c5e

Please sign in to comment.