use of java.lang.instrument.ClassFileTransformer in project byte-buddy by raphw.
the class AgentBuilderDefaultApplicationTest method testChainedAgent.
@Test
@AgentAttachmentRule.Enforce
@IntegrationRule.Enforce
public void testChainedAgent() throws Exception {
assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
AgentBuilder agentBuilder = new AgentBuilder.Default().with(poolStrategy).ignore(none()).type(ElementMatchers.is(Qux.class), ElementMatchers.is(classLoader)).transform(new QuxTransformer());
ClassFileTransformer firstTransformer = agentBuilder.installOnByteBuddyAgent();
ClassFileTransformer secondTransformer = agentBuilder.installOnByteBuddyAgent();
try {
Class<?> type = classLoader.loadClass(Qux.class.getName());
assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) (FOO + BAR + BAR)));
} finally {
ByteBuddyAgent.getInstrumentation().removeTransformer(firstTransformer);
ByteBuddyAgent.getInstrumentation().removeTransformer(secondTransformer);
}
}
use of java.lang.instrument.ClassFileTransformer in project byte-buddy by raphw.
the class AgentBuilderDefaultApplicationTest method testChunkedRetransformation.
@Test
@AgentAttachmentRule.Enforce(retransformsClasses = true)
@IntegrationRule.Enforce
public void testChunkedRetransformation() throws Exception {
// A redefinition reflects on loaded types which are eagerly validated types (Java 7- for redefinition).
// This causes type equality for outer/inner classes to fail which is why an external class is used.
assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
// ensure that class is loaded
assertThat(classLoader.loadClass(SimpleType.class.getName()).getName(), is(SimpleType.class.getName()));
ClassFileTransformer classFileTransformer = new AgentBuilder.Default().with(poolStrategy).ignore(none()).disableClassFormatChanges().with(AgentBuilder.TypeStrategy.Default.REDEFINE).with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION).with(AgentBuilder.RedefinitionStrategy.BatchAllocator.ForFixedSize.ofSize(1)).type(ElementMatchers.is(SimpleType.class), ElementMatchers.is(classLoader)).transform(new FooTransformer()).installOnByteBuddyAgent();
try {
Class<?> type = classLoader.loadClass(SimpleType.class.getName());
assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) BAR));
} finally {
ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer);
}
}
use of java.lang.instrument.ClassFileTransformer in project byte-buddy by raphw.
the class AgentBuilderDefaultApplicationTest method testAgentWithoutSelfInitializationWithNativeMethodPrefix.
@Test
@AgentAttachmentRule.Enforce
@IntegrationRule.Enforce
public void testAgentWithoutSelfInitializationWithNativeMethodPrefix() throws Exception {
assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
ClassFileTransformer classFileTransformer = new AgentBuilder.Default().with(poolStrategy).ignore(none()).enableNativeMethodPrefix(QUX).type(ElementMatchers.is(Baz.class), ElementMatchers.is(classLoader)).transform(new FooTransformer()).installOnByteBuddyAgent();
try {
Class<?> type = classLoader.loadClass(Baz.class.getName());
assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) BAR));
assertThat(type.getDeclaredMethod(QUX + FOO), notNullValue(Method.class));
} finally {
ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer);
}
}
use of java.lang.instrument.ClassFileTransformer in project byte-buddy by raphw.
the class AgentBuilderDefaultApplicationTest method testNonCapturingLambdaWithArguments.
@Test
@JavaVersionRule.Enforce(8)
@AgentAttachmentRule.Enforce(redefinesClasses = true)
@IntegrationRule.Enforce
public void testNonCapturingLambdaWithArguments() throws Exception {
assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
ClassLoader classLoader = lambdaSamples();
ClassFileTransformer classFileTransformer = new AgentBuilder.Default().with(poolStrategy).ignore(none()).with(AgentBuilder.LambdaInstrumentationStrategy.ENABLED).type(isSubTypeOf(Class.forName("java.util.function.Function"))).transform(new SingleMethodReplacer("apply")).installOn(ByteBuddyAgent.getInstrumentation());
try {
Class<?> sampleFactory = classLoader.loadClass(LAMBDA_SAMPLE_FACTORY);
Object instance = sampleFactory.getDeclaredMethod("nonCapturingWithArguments").invoke(sampleFactory.getDeclaredConstructor().newInstance());
assertThat(instance.getClass().getMethod("apply", Object.class).invoke(instance, FOO), is((Object) BAR));
} finally {
ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer);
AgentBuilder.LambdaInstrumentationStrategy.release(classFileTransformer, ByteBuddyAgent.getInstrumentation());
}
}
use of java.lang.instrument.ClassFileTransformer in project byte-buddy by raphw.
the class AgentBuilderDefaultApplicationTest method testAgentWithoutSelfInitialization.
@Test
@AgentAttachmentRule.Enforce
@IntegrationRule.Enforce
public void testAgentWithoutSelfInitialization() throws Exception {
assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
ClassFileTransformer classFileTransformer = new AgentBuilder.Default().with(poolStrategy).ignore(none()).with(AgentBuilder.InitializationStrategy.NoOp.INSTANCE).type(ElementMatchers.is(Foo.class), ElementMatchers.is(classLoader)).transform(new FooTransformer()).installOnByteBuddyAgent();
try {
Class<?> type = classLoader.loadClass(Foo.class.getName());
assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) BAR));
} finally {
ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer);
}
}
Aggregations