Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy try-catch blocks when merging static initializers #644

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
target.instructions.set(returnNode, new JumpInsnNode(Opcodes.GOTO, appendedCodeStartLabel));
}
target.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