From 9282054cbf22023e1f005f03d4669522a3eeb276 Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Thu, 28 Nov 2019 20:26:24 +0100 Subject: [PATCH] Throw exception if local advice parameters are used without exit advice what is not currently supported. --- .../src/main/java/net/bytebuddy/asm/Advice.java | 3 +++ .../test/java/net/bytebuddy/asm/AdviceTest.java | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) 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 394ebfda20e..024f87cb935 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 @@ -5160,6 +5160,9 @@ protected static ForInstrumentedMethod of(TypeDescription instrumentedType, if ((writerFlags & ClassWriter.COMPUTE_FRAMES) != 0 || classFileVersion.isLessThan(ClassFileVersion.JAVA_V6)) { return NoOp.INSTANCE; } else if (!exitAdvice) { + if (!initialTypes.isEmpty()) { + throw new IllegalStateException("Local parameters are not supported if no exit advice is present"); + } return new Trivial(instrumentedType, instrumentedMethod, (readerFlags & ClassReader.EXPAND_FRAMES) != 0); } else if (copyArguments) { return new WithPreservedArguments.UsingArgumentCopy(instrumentedType, diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/asm/AdviceTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/asm/AdviceTest.java index b517d0849d8..17be921a743 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/asm/AdviceTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/asm/AdviceTest.java @@ -1746,6 +1746,14 @@ public void testInvisibleDelegationAdvice() throws Exception { .make(); } + @Test(expected = IllegalStateException.class) + public void testLocalParameterWithoutExitIsIllegal() throws Exception { + new ByteBuddy() + .redefine(Sample.class) + .visit(Advice.to(EnterLocalVariableNotAllowedAdvice.class).on(named(FOO))) + .make(); + } + @Test(expected = IllegalStateException.class) public void testNonResolvedAdvice() throws Exception { Advice.to(TypeDescription.ForLoadedType.of(TrivialAdvice.class)); @@ -3309,4 +3317,12 @@ public static void advice() { } } } + + public static class EnterLocalVariableNotAllowedAdvice { + + @Advice.OnMethodEnter + public static void advice(@Advice.Local("foo") Void argument) { + /* empty */ + } + } }