Skip to content

Instantly share code, notes, and snippets.

@cheptsov
Last active October 18, 2016 16:48
Show Gist options
  • Save cheptsov/99cc6ce08a7bbbe24076a7825cb7f8ea to your computer and use it in GitHub Desktop.
Save cheptsov/99cc6ce08a7bbbe24076a7825cb7f8ea to your computer and use it in GitHub Desktop.
package org.jetbrains.fluid.java;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.commons.AdviceAdapter;
public class ReplaceMonitorWithTryCatchVisitor extends AdviceAdapter implements Opcodes {
private Label endTry;
private Label startCatch;
private Label endCatch;
public ReplaceMonitorWithTryCatchVisitor(MethodVisitor mv, int access,
String name, String desc, String className) {
super(Opcodes.ASM5, mv, access, name, desc);
}
public void visitInsn(int opcode) {
switch (opcode) {
case MONITORENTER:
mv.visitInsn(POP);
Label startTry = new Label();
endTry = new Label();
startCatch = new Label();
endCatch = new Label();
mv.visitTryCatchBlock(startTry, endTry, startCatch, null);
mv.visitLabel(startTry);
break;
case MONITOREXIT:
mv.visitInsn(POP);
// onFinally();
mv.visitLabel(endTry);
mv.visitJumpInsn(GOTO, endCatch);
mv.visitLabel(startCatch);
// onFinally();
mv.visitInsn(ATHROW);
mv.visitLabel(endCatch);
break;
default:
mv.visitInsn(opcode);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment