From be2e0ae99b07c436dd85d9205c1ba85acfb7157a Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Tue, 5 Apr 2016 13:42:24 +0200 Subject: [PATCH] Fixed obsolete jump on suppression handler. --- .../main/java/net/bytebuddy/asm/Advice.java | 21 ++++++++++--------- .../bytebuddy/asm/AdviceSuppressionTest.java | 2 ++ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/byte-buddy-dep/src/main/java/net/bytebuddy/asm/Advice.java b/byte-buddy-dep/src/main/java/net/bytebuddy/asm/Advice.java index c0e20f66bed..de860a0df64 100644 --- a/byte-buddy-dep/src/main/java/net/bytebuddy/asm/Advice.java +++ b/byte-buddy-dep/src/main/java/net/bytebuddy/asm/Advice.java @@ -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. } /** @@ -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. * @@ -645,6 +649,8 @@ protected AdviceVisitor(MethodVisitor methodVisitor, public void visitCode() { super.visitCode(); append(methodEnter); + // TODO: Optional for above "true" +// frameTranslator.injectEntranceFrame(mv); onUserStart(); } @@ -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); @@ -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. * @@ -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); diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/asm/AdviceSuppressionTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/asm/AdviceSuppressionTest.java index 103354375fa..9786fb95ced 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/asm/AdviceSuppressionTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/asm/AdviceSuppressionTest.java @@ -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; @@ -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)