Search in sources :

Example 11 with ClassFileTransformer

use of java.lang.instrument.ClassFileTransformer in project byte-buddy by raphw.

the class AgentBuilderDefaultApplicationTest method testChunkedRedefinition.

@Test
@AgentAttachmentRule.Enforce(redefinesClasses = true)
@IntegrationRule.Enforce
public void testChunkedRedefinition() 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).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);
    }
}
Also used : SimpleType(net.bytebuddy.test.packaging.SimpleType) ClassFileTransformer(java.lang.instrument.ClassFileTransformer) Instrumentation(java.lang.instrument.Instrumentation) Test(org.junit.Test)

Example 12 with ClassFileTransformer

use of java.lang.instrument.ClassFileTransformer in project byte-buddy by raphw.

the class AgentBuilderDefaultApplicationTest method testLambdaFactoryIsReset.

@Test
@JavaVersionRule.Enforce(8)
@AgentAttachmentRule.Enforce(redefinesClasses = true)
@IntegrationRule.Enforce
public void testLambdaFactoryIsReset() throws Exception {
    assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
    ClassLoader classLoader = lambdaSamples();
    ClassFileTransformer classFileTransformer = new AgentBuilder.Default().with(poolStrategy).ignore(none()).with(AgentBuilder.LambdaInstrumentationStrategy.ENABLED).installOn(ByteBuddyAgent.getInstrumentation());
    ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer);
    AgentBuilder.LambdaInstrumentationStrategy.release(classFileTransformer, ByteBuddyAgent.getInstrumentation());
    Class<?> sampleFactory = classLoader.loadClass(LAMBDA_SAMPLE_FACTORY);
    @SuppressWarnings("unchecked") Callable<String> instance = (Callable<String>) sampleFactory.getDeclaredMethod("nonCapturing").invoke(sampleFactory.getDeclaredConstructor().newInstance());
    assertThat(instance.call(), is(FOO));
}
Also used : ClassFileTransformer(java.lang.instrument.ClassFileTransformer) Instrumentation(java.lang.instrument.Instrumentation) ByteArrayClassLoader(net.bytebuddy.dynamic.loading.ByteArrayClassLoader) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 13 with ClassFileTransformer

use of java.lang.instrument.ClassFileTransformer in project byte-buddy by raphw.

the class AgentBuilderDefaultApplicationTest method testNonCapturingLambda.

@Test
@JavaVersionRule.Enforce(8)
@AgentAttachmentRule.Enforce(redefinesClasses = true)
@IntegrationRule.Enforce
public void testNonCapturingLambda() 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);
        @SuppressWarnings("unchecked") Callable<String> instance = (Callable<String>) sampleFactory.getDeclaredMethod("nonCapturing").invoke(sampleFactory.getDeclaredConstructor().newInstance());
        assertThat(instance.call(), is(BAR));
    } finally {
        ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer);
        AgentBuilder.LambdaInstrumentationStrategy.release(classFileTransformer, ByteBuddyAgent.getInstrumentation());
    }
}
Also used : ClassFileTransformer(java.lang.instrument.ClassFileTransformer) Instrumentation(java.lang.instrument.Instrumentation) ByteArrayClassLoader(net.bytebuddy.dynamic.loading.ByteArrayClassLoader) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 14 with ClassFileTransformer

use of java.lang.instrument.ClassFileTransformer in project byte-buddy by raphw.

the class AgentBuilderDefaultApplicationRedefineTest 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(simpleTypeLoader.loadClass(SimpleType.class.getName()).getName(), is(SimpleType.class.getName()));
    ClassFileTransformer classFileTransformer = new AgentBuilder.Default().ignore(none()).disableClassFormatChanges().with(AgentBuilder.RedefinitionStrategy.REDEFINITION).with(descriptionStrategy).type(ElementMatchers.is(SimpleType.class), ElementMatchers.is(simpleTypeLoader)).transform(new FooTransformer()).installOnByteBuddyAgent();
    try {
        Class<?> type = simpleTypeLoader.loadClass(SimpleType.class.getName());
        assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) BAR));
    } finally {
        ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer);
    }
}
Also used : SimpleType(net.bytebuddy.test.packaging.SimpleType) ClassFileTransformer(java.lang.instrument.ClassFileTransformer) Instrumentation(java.lang.instrument.Instrumentation) Test(org.junit.Test)

Example 15 with ClassFileTransformer

use of java.lang.instrument.ClassFileTransformer in project byte-buddy by raphw.

the class AgentBuilderDefaultApplicationSuperTypeLoadingTest method testSynchronousSuperTypeLoading.

@Test
@AgentAttachmentRule.Enforce
@IntegrationRule.Enforce
public void testSynchronousSuperTypeLoading() throws Exception {
    assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
    ClassFileTransformer classFileTransformer = new AgentBuilder.Default().with(AgentBuilder.DescriptionStrategy.Default.POOL_ONLY.withSuperTypeLoading()).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()), nullValue(Object.class));
    } finally {
        ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer);
    }
}
Also used : ClassFileTransformer(java.lang.instrument.ClassFileTransformer) Instrumentation(java.lang.instrument.Instrumentation) Test(org.junit.Test)

Aggregations

ClassFileTransformer (java.lang.instrument.ClassFileTransformer)54 Test (org.junit.Test)38 Instrumentation (java.lang.instrument.Instrumentation)35 ByteArrayClassLoader (net.bytebuddy.dynamic.loading.ByteArrayClassLoader)9 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)7 Callable (java.util.concurrent.Callable)7 SimpleType (net.bytebuddy.test.packaging.SimpleType)6 ProtectionDomain (java.security.ProtectionDomain)5 ByteBuddy (net.bytebuddy.ByteBuddy)5 URLClassLoader (java.net.URLClassLoader)3 MatchableClassFileTransformer (com.navercorp.pinpoint.profiler.plugin.MatchableClassFileTransformer)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Properties (java.util.Properties)2 PersistenceProvider (javax.persistence.spi.PersistenceProvider)2 TypeDescription (net.bytebuddy.description.type.TypeDescription)2 SimpleOptionalType (net.bytebuddy.test.packaging.SimpleOptionalType)2 TempClassLoader (org.apache.openejb.core.TempClassLoader)2 PersistenceClassLoaderHandler (org.apache.openejb.persistence.PersistenceClassLoaderHandler)2 PersistenceUnitInfoImpl (org.apache.openejb.persistence.PersistenceUnitInfoImpl)2