Skip to content

Commit

Permalink
Add missing tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Oct 5, 2021
1 parent 833afda commit 049f799
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 1 deletion.
1 change: 0 additions & 1 deletion byte-buddy-dep/src/main/java/net/bytebuddy/asm/Advice.java
Original file line number Diff line number Diff line change
Expand Up @@ -12060,7 +12060,6 @@ public List<AssignReturned.Handler> make(MethodDescription.InDefinedShape advice
}
}


/**
* <p>
* Assigns the advice method's return value to the {@code this} reference of the instrumented method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.bytebuddy.ByteBuddy;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.implementation.bytecode.assign.Assigner;
import org.junit.Test;

import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -243,6 +244,53 @@ public void testAssignReturnedToThrownSuppression() throws Exception {
assertThat(type.getMethod(FOO, String.class).invoke(type.getConstructor().newInstance(), FOO), nullValue(Object.class));
}

@Test
public void testAssignReturnedToAllArgumentsDynamicTyped() throws Exception {
Class<?> type = new ByteBuddy()
.redefine(Sample.class)
.visit(Advice.withCustomMapping()
.with(new Advice.AssignReturned.Factory())
.to(ToAllArgumentsDynamic.class)
.on(named(BAZ)))
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
assertThat(type.getMethod(BAZ, String.class, String.class).invoke(type.getConstructor().newInstance(), FOO, BAR), is((Object) (BAR + FOO)));
}

@Test
public void testAssignReturnedToFieldStaticFromNonStatic() throws Exception {
Class<?> type = new ByteBuddy()
.redefine(Sample.class)
.visit(Advice.withCustomMapping()
.with(new Advice.AssignReturned.Factory())
.to(ToFieldStatic.class)
.on(named(FOO)))
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
Object instance = type.getConstructor().newInstance();
type.getField(BAR).set(null, FOO);
assertThat(type.getMethod(FOO, String.class).invoke(instance, FOO), is((Object) FOO));
assertThat(type.getField(BAR).get(null), is((Object) QUX));
}

@Test
public void testAssignReturnedToFieldStaticFromStatic() throws Exception {
Class<?> type = new ByteBuddy()
.redefine(Sample.class)
.visit(Advice.withCustomMapping()
.with(new Advice.AssignReturned.Factory())
.to(ToFieldStatic.class)
.on(named(BAR)))
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
type.getField(BAR).set(null, FOO);
assertThat(type.getMethod(BAR, String.class).invoke(null, FOO), is((Object) FOO));
assertThat(type.getField(BAR).get(null), is((Object) QUX));
}

@Test(expected = IllegalStateException.class)
public void testArgumentTooFewParameters() {
new ByteBuddy()
Expand Down Expand Up @@ -320,10 +368,56 @@ public void testThisNotAssignable() throws Exception {
.make();
}

@Test(expected = IllegalStateException.class)
public void testAllArgumentsNotAssignable() throws Exception {
new ByteBuddy()
.redefine(Sample.class)
.visit(Advice.withCustomMapping()
.with(new Advice.AssignReturned.Factory())
.to(ToAllArgumentsNotAssignable.class)
.on(named(BAZ)))
.make();
}

@Test(expected = IllegalStateException.class)
public void testFieldValueNotAssignable() throws Exception {
new ByteBuddy()
.redefine(Sample.class)
.visit(Advice.withCustomMapping()
.with(new Advice.AssignReturned.Factory())
.to(ToFieldNotAssignable.class)
.on(named(FOO)))
.make();
}

@Test(expected = IllegalStateException.class)
public void testFieldValueNotFound() throws Exception {
new ByteBuddy()
.redefine(Sample.class)
.visit(Advice.withCustomMapping()
.with(new Advice.AssignReturned.Factory())
.to(ToFieldNotKnown.class)
.on(named(FOO)))
.make();
}

@Test(expected = IllegalStateException.class)
public void testFieldValueStatic() throws Exception {
new ByteBuddy()
.redefine(Sample.class)
.visit(Advice.withCustomMapping()
.with(new Advice.AssignReturned.Factory())
.to(ToFieldScalar.class)
.on(named(BAR)))
.make();
}

public static class Sample {

public String foo;

public static String bar;

public String foo(String value) {
return value;
}
Expand Down Expand Up @@ -650,4 +744,58 @@ public static Object enter(@Advice.This Sample sample) {
return replacement;
}
}

public static class ToAllArgumentsDynamic {

@Advice.OnMethodEnter
@Advice.AssignReturned.ToAllArguments(typing = Assigner.Typing.DYNAMIC)
public static Object enter(@Advice.AllArguments String[] argument) {
if (argument.length != 2 || !FOO.equals(argument[0]) || !BAR.equals(argument[1])) {
throw new AssertionError();
}
return new String[] {BAR, FOO};
}
}

public static class ToAllArgumentsNotAssignable {

@Advice.OnMethodEnter
@Advice.AssignReturned.ToAllArguments
public static Object enter(@Advice.AllArguments String[] argument) {
if (argument.length != 2 || !FOO.equals(argument[0]) || !BAR.equals(argument[1])) {
throw new AssertionError();
}
return new String[] {BAR, FOO};
}
}

public static class ToFieldNotAssignable {

@Advice.OnMethodEnter
@Advice.AssignReturned.ToFields(@Advice.AssignReturned.ToFields.ToField(FOO))
public static Void enter() {
throw new AssertionError();
}
}

public static class ToFieldNotKnown {

@Advice.OnMethodEnter
@Advice.AssignReturned.ToFields(@Advice.AssignReturned.ToFields.ToField(QUX))
public static String enter() {
throw new AssertionError();
}
}

public static class ToFieldStatic {

@Advice.OnMethodEnter
@Advice.AssignReturned.ToFields(@Advice.AssignReturned.ToFields.ToField(BAR))
public static String enter(@Advice.FieldValue(BAR) String field) {
if (!FOO.equals(field)) {
throw new AssertionError();
}
return QUX;
}
}
}

0 comments on commit 049f799

Please sign in to comment.