Skip to content

Commit

Permalink
Removed ASM advice adapter and implemented cheaper, less intrusive so…
Browse files Browse the repository at this point in the history
…lution.
  • Loading branch information
raphw committed Mar 4, 2016
1 parent cfe758c commit f75748e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
28 changes: 13 additions & 15 deletions byte-buddy-dep/src/main/java/net/bytebuddy/asm/advice/Advice.java
Expand Up @@ -8,7 +8,6 @@
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.ClassFileLocator;
import org.objectweb.asm.*;
import org.objectweb.asm.commons.AdviceAdapter;

import java.io.IOException;
import java.lang.annotation.*;
Expand Down Expand Up @@ -71,7 +70,7 @@ public MethodVisitor wrap(TypeDescription instrumentedType, MethodDescription.In
return new AsmAdvice(methodVisitor, methodDescription);
}

protected class AsmAdvice extends AdviceAdapter {
protected class AsmAdvice extends MethodVisitor {

private static final int NO_VALUE = -1;

Expand All @@ -84,37 +83,36 @@ protected class AsmAdvice extends AdviceAdapter {
private int maxLocals = NO_VALUE;

protected AsmAdvice(MethodVisitor methodVisitor, MethodDescription methodDescription) {
super(Opcodes.ASM5, methodVisitor, methodDescription.getActualModifiers(), methodDescription.getInternalName(), methodDescription.getDescriptor());
super(Opcodes.ASM5, methodVisitor);
classReader = new ClassReader(binaryRepresentation);
this.instrumentedMethod = methodDescription;
}

@Override
protected void onMethodEnter() {
public void visitCode() {
super.visitCode();
classReader.accept(new CodeCopier(methodEnter), ClassReader.SKIP_DEBUG);
}

@Override
protected void onMethodExit(int opcode) {
int stackIncrement;
public void visitInsn(int opcode) {
switch (opcode) {
case Opcodes.RETURN:
stackIncrement = 0;
classReader.accept(new CodeCopier(methodExit), ClassReader.SKIP_DEBUG);
break;
case Opcodes.ARETURN:
case Opcodes.ATHROW:
case Opcodes.IRETURN:
case Opcodes.FRETURN:
stackIncrement = 1;
case Opcodes.ARETURN:
case Opcodes.ATHROW:
classReader.accept(new CodeCopier(methodExit, 1), ClassReader.SKIP_DEBUG);
break;
case Opcodes.DRETURN:
case Opcodes.LRETURN:
stackIncrement = 2;
classReader.accept(new CodeCopier(methodExit, 2), ClassReader.SKIP_DEBUG);
break;
default:
throw new IllegalStateException("Unexpected termination opcode: " + opcode);
}
classReader.accept(new CodeCopier(methodExit, stackIncrement), ClassReader.SKIP_DEBUG);
// TODO: Adapt stack map frames to include additional frame!
super.visitInsn(opcode);
}

@Override
Expand Down Expand Up @@ -224,6 +222,7 @@ public void visitInsn(int opcode) {
break;
case Opcodes.IRETURN:
case Opcodes.FRETURN:
case Opcodes.ARETURN:
super.visitInsn(Opcodes.POP);
super.visitJumpInsn(Opcodes.GOTO, endOfMethod);
break;
Expand All @@ -232,7 +231,6 @@ public void visitInsn(int opcode) {
super.visitInsn(Opcodes.POP2);
super.visitJumpInsn(Opcodes.GOTO, endOfMethod);
break;
case Opcodes.ARETURN:
default:
super.visitInsn(opcode);
}
Expand Down
Expand Up @@ -102,4 +102,4 @@ private static void exit(@Advice.Argument(1) String argument) {
System.out.println(argument);
}
}
}
}

0 comments on commit f75748e

Please sign in to comment.