Skip to content

Commit

Permalink
Add test.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Oct 31, 2024
1 parent 9e919eb commit 810fd97
Showing 1 changed file with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.dynamic.TargetType;
import net.bytebuddy.dynamic.loading.ByteArrayClassLoader;
import net.bytebuddy.dynamic.loading.ClassInjector;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.implementation.FixedValue;
import net.bytebuddy.implementation.MethodDelegation;
Expand Down Expand Up @@ -838,6 +839,33 @@ public void testAdviceTransformer() throws Exception {
}
}

@Test
@IntegrationRule.Enforce
public void testAdviceTransformerWithInjection() throws Exception {
Instrumentation instrumentation = ByteBuddyAgent.install();
assertThat(instrumentation, instanceOf(Instrumentation.class));
ClassFileTransformer classFileTransformer = new AgentBuilder.Default()
.with(poolStrategy)
.with(new AgentBuilder.InjectionStrategy.UsingUnsafe.OfFactory(ClassInjector.UsingUnsafe.Factory.resolve(instrumentation)))
.ignore(none())
.with(AgentBuilder.InitializationStrategy.NoOp.INSTANCE)
.type(ElementMatchers.is(Foo.class), ElementMatchers.is(classLoader)).transform(new AgentBuilder.Transformer.ForAdvice()
.with(poolStrategy)
.with(AgentBuilder.LocationStrategy.ForClassLoader.STRONG)
.include(BazAdvice.class.getClassLoader())
.with(Assigner.DEFAULT)
.withExceptionHandler(new Advice.ExceptionHandler.Simple(Removal.SINGLE))
.advice(named(FOO), BazAdvice.class.getName())
.auxiliary(BazAdviceAuxiliary.class.getName()))
.installOnByteBuddyAgent();
try {
Class<?> type = classLoader.loadClass(Foo.class.getName());
assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) (FOO + BAR)));
} finally {
assertThat(ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer), is(true));
}
}

private static class FooTransformer implements AgentBuilder.Transformer {

public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder,
Expand Down Expand Up @@ -1016,4 +1044,19 @@ private static void exit(@Advice.Return(readOnly = false) String value) {
value += QUX;
}
}

private static class BazAdvice {

@Advice.OnMethodExit
private static void exit(@Advice.Return(readOnly = false) String value) {
value += BazAdviceAuxiliary.value();
}
}

public static class BazAdviceAuxiliary {

public static String value() {
return BAR;
}
}
}

0 comments on commit 810fd97

Please sign in to comment.