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.description.type.TypeDescription;
import net.bytebuddy.dynamic.ClassFileLocator; import net.bytebuddy.dynamic.ClassFileLocator;
import org.objectweb.asm.*; import org.objectweb.asm.*;
import org.objectweb.asm.commons.AdviceAdapter;


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


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


private static final int NO_VALUE = -1; private static final int NO_VALUE = -1;


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


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


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


@Override @Override
protected void onMethodExit(int opcode) { public void visitInsn(int opcode) {
int stackIncrement;
switch (opcode) { switch (opcode) {
case Opcodes.RETURN: case Opcodes.RETURN:
stackIncrement = 0; classReader.accept(new CodeCopier(methodExit), ClassReader.SKIP_DEBUG);
break; break;
case Opcodes.ARETURN:
case Opcodes.ATHROW:
case Opcodes.IRETURN: case Opcodes.IRETURN:
case Opcodes.FRETURN: case Opcodes.FRETURN:
stackIncrement = 1; case Opcodes.ARETURN:
case Opcodes.ATHROW:
classReader.accept(new CodeCopier(methodExit, 1), ClassReader.SKIP_DEBUG);
break; break;
case Opcodes.DRETURN: case Opcodes.DRETURN:
case Opcodes.LRETURN: case Opcodes.LRETURN:
stackIncrement = 2; classReader.accept(new CodeCopier(methodExit, 2), ClassReader.SKIP_DEBUG);
break; 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 @Override
Expand Down Expand Up @@ -224,6 +222,7 @@ public void visitInsn(int opcode) {
break; break;
case Opcodes.IRETURN: case Opcodes.IRETURN:
case Opcodes.FRETURN: case Opcodes.FRETURN:
case Opcodes.ARETURN:
super.visitInsn(Opcodes.POP); super.visitInsn(Opcodes.POP);
super.visitJumpInsn(Opcodes.GOTO, endOfMethod); super.visitJumpInsn(Opcodes.GOTO, endOfMethod);
break; break;
Expand All @@ -232,7 +231,6 @@ public void visitInsn(int opcode) {
super.visitInsn(Opcodes.POP2); super.visitInsn(Opcodes.POP2);
super.visitJumpInsn(Opcodes.GOTO, endOfMethod); super.visitJumpInsn(Opcodes.GOTO, endOfMethod);
break; break;
case Opcodes.ARETURN:
default: default:
super.visitInsn(opcode); super.visitInsn(opcode);
} }
Expand Down
Expand Up @@ -102,4 +102,4 @@ private static void exit(@Advice.Argument(1) String argument) {
System.out.println(argument); System.out.println(argument);
} }
} }
} }

0 comments on commit f75748e

Please sign in to comment.