use of java.lang.instrument.ClassFileTransformer in project byte-buddy by raphw.
the class AgentBuilderDefaultApplicationSuperTypeLoadingTest method testAsynchronousSuperTypeLoading.
@Test
@AgentAttachmentRule.Enforce
@IntegrationRule.Enforce
public void testAsynchronousSuperTypeLoading() throws Exception {
assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
ClassFileTransformer classFileTransformer = new AgentBuilder.Default().with(AgentBuilder.DescriptionStrategy.Default.POOL_ONLY.withSuperTypeLoading(executorService)).ignore(none()).with(AgentBuilder.InitializationStrategy.NoOp.INSTANCE).type(ElementMatchers.isSubTypeOf(Foo.class), ElementMatchers.is(classLoader)).transform(new ConstantTransformer()).installOnByteBuddyAgent();
try {
Class<?> type = classLoader.loadClass(Bar.class.getName());
assertThat(type.getDeclaredMethod(BAR).invoke(type.getDeclaredConstructor().newInstance()), is((Object) BAR));
assertThat(type.getSuperclass().getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) FOO));
} finally {
ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer);
}
}
use of java.lang.instrument.ClassFileTransformer in project byte-buddy by raphw.
the class AgentBuilderDefaultApplicationTest method testSignatureTypesAreAvailableAfterLoad.
@Test
@AgentAttachmentRule.Enforce
@IntegrationRule.Enforce
public void testSignatureTypesAreAvailableAfterLoad() throws Exception {
assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
ClassFileTransformer classFileTransformer = new AgentBuilder.Default().with(poolStrategy).ignore(none()).type(ElementMatchers.is(Foo.class), ElementMatchers.is(classLoader)).transform(new ConstructorTransformer()).installOnByteBuddyAgent();
try {
Class<?> type = classLoader.loadClass(Foo.class.getName());
assertThat(type.getDeclaredConstructors().length, is(2));
assertThat(type.getDeclaredConstructor().newInstance(), notNullValue(Object.class));
} finally {
ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer);
}
}
use of java.lang.instrument.ClassFileTransformer in project byte-buddy by raphw.
the class AgentBuilderDefaultApplicationTest method testArgumentCapturingLambdaIsNotConstant.
@Test
@JavaVersionRule.Enforce(8)
@AgentAttachmentRule.Enforce(redefinesClasses = true)
@IntegrationRule.Enforce
public void testArgumentCapturingLambdaIsNotConstant() 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(Callable.class)).transform(new SingleMethodReplacer("call")).installOn(ByteBuddyAgent.getInstrumentation());
try {
Class<?> sampleFactory = classLoader.loadClass(LAMBDA_SAMPLE_FACTORY);
assertThat(sampleFactory.getDeclaredMethod("argumentCapturing", String.class).invoke(sampleFactory.getDeclaredConstructor().newInstance(), FOO), not(sameInstance(sampleFactory.getDeclaredMethod("argumentCapturing", String.class).invoke(sampleFactory.getDeclaredConstructor().newInstance(), FOO))));
} 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 testNonCapturingLambdaIsConstant.
@Test
@JavaVersionRule.Enforce(8)
@AgentAttachmentRule.Enforce(redefinesClasses = true)
@IntegrationRule.Enforce
public void testNonCapturingLambdaIsConstant() 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(Callable.class)).transform(new SingleMethodReplacer("call")).installOn(ByteBuddyAgent.getInstrumentation());
try {
Class<?> sampleFactory = classLoader.loadClass(LAMBDA_SAMPLE_FACTORY);
assertThat(sampleFactory.getDeclaredMethod("nonCapturing").invoke(sampleFactory.getDeclaredConstructor().newInstance()), sameInstance(sampleFactory.getDeclaredMethod("nonCapturing").invoke(sampleFactory.getDeclaredConstructor().newInstance())));
} 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 testRedefinition.
@Test
@AgentAttachmentRule.Enforce(redefinesClasses = true)
@IntegrationRule.Enforce
public void testRedefinition() 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.REDEFINITION).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);
}
}
Aggregations