Skip to content

Commit

Permalink
Added better test.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Apr 3, 2016
1 parent f3bde32 commit c7c19be
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 39 deletions.
17 changes: 10 additions & 7 deletions byte-buddy-dep/src/main/java/net/bytebuddy/asm/Advice.java
Expand Up @@ -170,7 +170,7 @@ private static Dispatcher resolve(Class<? extends Annotation> annotation, Dispat
* @return A suitable ASM visitor wrapper with the <i>compute frames</i> option enabled.
*/
public AsmVisitorWrapper.ForDeclaredMethods on(ElementMatcher<? super MethodDescription.InDefinedShape> matcher) {
return new AsmVisitorWrapper.ForDeclaredMethods().writerFlags(ClassWriter.COMPUTE_MAXS).method(matcher, this);
return new AsmVisitorWrapper.ForDeclaredMethods().writerFlags(ClassWriter.COMPUTE_MAXS).readerFlags(ClassReader.EXPAND_FRAMES).method(matcher, this);
}

@Override
Expand Down Expand Up @@ -236,9 +236,6 @@ protected void injectHandlerFrame(MethodVisitor methodVisitor) {
}

private void injectFrame(MethodVisitor methodVisitor, TypeList intermediateTypes, boolean throwableOnStack) {
if (true) {
return;
}
if (requiresFull) {
Object[] localVariable = new Object[instrumentedMethod.getParameters().size()
+ (instrumentedMethod.isStatic() ? 0 : 1)
Expand Down Expand Up @@ -294,9 +291,6 @@ private void translateFrame(MethodVisitor methodVisitor,
Object[] localVariable,
int stackSize,
Object[] stack) {
if (true) {
return;
}
switch (type) {
case Opcodes.F_SAME:
case Opcodes.F_SAME1:
Expand Down Expand Up @@ -401,6 +395,15 @@ protected void injectFrame(MethodVisitor methodVisitor) {
public void injectHandlerFrame(MethodVisitor methodVisitor) {
FrameTranslator.this.injectFrame(methodVisitor, intermediateTypes, true);
}

@Override
public String toString() {
return "Bound{" +
"frameTranslator=" + FrameTranslator.this +
", methodDescription=" + methodDescription +
", intermediateTypes=" + intermediateTypes +
'}';
}
}
}

Expand Down
97 changes: 65 additions & 32 deletions byte-buddy-dep/src/test/java/net/bytebuddy/asm/AdviceTest.java
Expand Up @@ -9,6 +9,7 @@
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.implementation.bytecode.StackSize;
import net.bytebuddy.test.utility.DebuggingWrapper;
import net.bytebuddy.test.utility.ObjectPropertyAssertion;
import org.junit.Test;

Expand Down Expand Up @@ -501,6 +502,7 @@ public void testFrameAdvice() throws Exception {
Class<?> type = new ByteBuddy()
.redefine(FrameSample.class)
.visit(Advice.to(FrameAdvice.class).on(named(FOO)))
.visit(DebuggingWrapper.makeDefault())
.make()
.load(null, ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
Expand All @@ -509,7 +511,7 @@ public void testFrameAdvice() throws Exception {
}

@Test
public void testFrameAdviceStatic() throws Exception {
public void testFrameAdviceStaticMethod() throws Exception {
Class<?> type = new ByteBuddy()
.redefine(FrameSample.class)
.visit(Advice.to(FrameAdvice.class).on(named(BAR)))
Expand Down Expand Up @@ -1302,68 +1304,99 @@ private static void exit(@Advice.Origin("\\#\\#\\\\#m") String origin) throws Ex
}
}

@SuppressWarnings("all")
public static class FrameSample {

public static int count;

public String foo(String value) {
switch (0) {
case 0: {
long a = 1L, b = 1L, c = 1L, d = 1L, e = 1L, f = 1L;
int ignored = 0;
{
long v1 = 1L, v2 = 2L, v3 = 3L;
if (ignored == 1) {
throw new AssertionError();
} else if (ignored == 2) {
if (v1 + v2 + v3 == 0L) {
throw new AssertionError();
}
}
default: {
long a = 1L;
}
long v4 = 1L, v5 = 2L, v6 = 3L, v7 = 4L;
if (ignored == 3) {
throw new AssertionError();
} else if (ignored == 4) {
if (v4 + v5 + v6 + v7 == 0L) {
throw new AssertionError();
}
}
try {
long a = 1L;
} catch (Exception ignored) {
long a = 1L;
long v8 = 1L;
} catch (Exception exception) {
long v9 = 1L;
}
long a = 1L;
return value;
}

public static String bar(String value) {
switch (0) {
case 0: {
long a = 1L, b = 1L, c = 1L, d = 1L, e = 1L, f = 1L;
int ignored = 0;
{
long v1 = 1L, v2 = 2L, v3 = 3L;
if (ignored == 1) {
throw new AssertionError();
} else if (ignored == 2) {
if (v1 + v2 + v3 == 0L) {
throw new AssertionError();
}
}
default: {
long a = 1L;
}
long v4 = 1L, v5 = 2L, v6 = 3L, v7 = 4L;
if (ignored == 3) {
throw new AssertionError();
} else if (ignored == 4) {
if (v4 + v5 + v6 + v7 == 0L) {
throw new AssertionError();
}
}
try {
long a = 1L;
} catch (Exception ignored) {
long a = 1L;
long v8 = 1L;
} catch (Exception exception) {
long v9 = 1L;
}
long a = 1L;
return value;
}
}

@SuppressWarnings("unused")
public static class FrameAdvice {

@Advice.OnMethodEnter
@Advice.OnMethodExit
private static String enter(@Advice.Ignored int value, @Advice.Argument(0) String pseudo) {
switch (value) {
case 0: {
long a = 1L, b = 1L, c = 1L, d = 1L, e = 1L, f = 1L;
@Advice.OnMethodEnter(suppress = RuntimeException.class)
@Advice.OnMethodExit(suppress = RuntimeException.class)
private static String advice(@Advice.Ignored int ignored, @Advice.Argument(0) String value) {
{
long v1 = 1L, v2 = 2L, v3 = 3L;
if (ignored == 1) {
throw new AssertionError();
} else if (ignored == 2) {
if (v1 + v2 + v3 == 0L) {
throw new AssertionError();
}
}
default: {
long a = 1L;
}
long v4 = 1L, v5 = 2L, v6 = 3L, v7 = 4L;
if (ignored == 3) {
throw new AssertionError();
} else if (ignored == 4) {
if (v4 + v5 + v6 + v7 == 0L) {
throw new AssertionError();
}
}
try {
long a = 1L;
} catch (Exception ignored) {
long a = 1L;
long v8 = 1L;
} catch (Exception exception) {
long v9 = 1L;
}
long a = 1L;
FrameSample.count++;
return pseudo;
return value;
}
}

Expand Down

0 comments on commit c7c19be

Please sign in to comment.