Skip to content

Commit

Permalink
Fixed obsolete jump on suppression handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Apr 5, 2016
1 parent fc3589b commit be2e0ae
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
21 changes: 11 additions & 10 deletions byte-buddy-dep/src/main/java/net/bytebuddy/asm/Advice.java
Expand Up @@ -251,7 +251,7 @@ protected static class FrameTranslator {
protected FrameTranslator(MethodDescription.InDefinedShape instrumentedMethod, TypeList intermediateTypes) {
this.instrumentedMethod = instrumentedMethod;
this.intermediateTypes = intermediateTypes;
stackSize = 1; // Minimum for pushing exceptions onto the stack.
stackSize = 2; // Minimum for pushing exceptions or default values. Could be more accurate.
}

/**
Expand Down Expand Up @@ -332,6 +332,10 @@ protected int compoundLocalVariableSize(int localVariableLength) {
+ intermediateTypes.getStackSize());
}

protected void injectEntranceFrame(MethodVisitor methodVisitor) {
injectFrame(methodVisitor, intermediateTypes, false);
}

/**
* Injects a frame that describes the method when entering its surrounding exception handler.
*
Expand Down Expand Up @@ -645,6 +649,8 @@ protected AdviceVisitor(MethodVisitor methodVisitor,
public void visitCode() {
super.visitCode();
append(methodEnter);
// TODO: Optional for above "true"
// frameTranslator.injectEntranceFrame(mv);
onUserStart();
}

Expand All @@ -664,7 +670,9 @@ public void visitVarInsn(int opcode, int offset) {
public void visitInsn(int opcode) {
switch (opcode) {
case Opcodes.RETURN:
onMethodExit();
mv.visitInsn(Opcodes.ACONST_NULL);
variable(Opcodes.ASTORE);
mv.visitJumpInsn(Opcodes.GOTO, endOfMethod);
return;
case Opcodes.IRETURN:
onMethodExit(Opcodes.ISTORE);
Expand Down Expand Up @@ -698,12 +706,6 @@ private void onMethodExit(int store) {
mv.visitJumpInsn(Opcodes.GOTO, endOfMethod);
}

private void onMethodExit() {
mv.visitInsn(Opcodes.ACONST_NULL);
variable(Opcodes.ASTORE);
mv.visitJumpInsn(Opcodes.GOTO, endOfMethod);
}

/**
* Access the first variable after the instrumented variables and return type are stored.
*
Expand Down Expand Up @@ -2912,9 +2914,8 @@ public void onStart(MethodVisitor methodVisitor, FrameTranslator.Bound frameTran
@Override
public void onEnd(MethodVisitor methodVisitor, FrameTranslator.Bound frameTranslator, ReturnValueProducer returnValueProducer) {
Label endOfHandler = new Label();
methodVisitor.visitJumpInsn(Opcodes.GOTO, endOfHandler);
frameTranslator.injectHandlerFrame(methodVisitor);
methodVisitor.visitLabel(handler);
frameTranslator.injectHandlerFrame(methodVisitor);
methodVisitor.visitInsn(Opcodes.POP);
returnValueProducer.makeDefault(methodVisitor);
methodVisitor.visitLabel(endOfHandler);
Expand Down
Expand Up @@ -2,6 +2,7 @@

import net.bytebuddy.ByteBuddy;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.test.utility.DebuggingWrapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
Expand Down Expand Up @@ -43,6 +44,7 @@ public AdviceSuppressionTest(Class<?> type) {
public void testIllegalAssignment() throws Exception {
Class<?> dynamicType = new ByteBuddy()
.redefine(Sample.class)
.visit(DebuggingWrapper.makeDefault())
.visit(Advice.to(type).on(named(FOO)))
.make()
.load(null, ClassLoadingStrategy.Default.WRAPPER)
Expand Down

0 comments on commit be2e0ae

Please sign in to comment.