Search in sources :

Example 31 with ClassFileTransformer

use of java.lang.instrument.ClassFileTransformer in project jetty.project by eclipse.

the class WebAppClassLoaderTest method testNullClassFileTransformer.

@Test
public void testNullClassFileTransformer() throws Exception {
    _loader.addTransformer(new ClassFileTransformer() {

        public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
            return null;
        }
    });
    assertCanLoadClass("org.acme.webapp.ClassInJarA");
}
Also used : ProtectionDomain(java.security.ProtectionDomain) ClassFileTransformer(java.lang.instrument.ClassFileTransformer) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) URLClassLoader(java.net.URLClassLoader) Test(org.junit.Test)

Example 32 with ClassFileTransformer

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

the class AgentBuilderLambdaInstrumentationStrategyTest method testEnabledStrategyNeverThrowsException.

@Test
public void testEnabledStrategyNeverThrowsException() throws Exception {
    ClassFileTransformer initialClassFileTransformer = mock(ClassFileTransformer.class);
    assertThat(LambdaFactory.register(initialClassFileTransformer, mock(AgentBuilder.Default.LambdaInstrumentationStrategy.LambdaInstanceFactory.class)), is(true));
    try {
        ByteBuddy byteBuddy = mock(ByteBuddy.class);
        Instrumentation instrumentation = mock(Instrumentation.class);
        ClassFileTransformer classFileTransformer = mock(ClassFileTransformer.class);
        try {
            AgentBuilder.Default.LambdaInstrumentationStrategy.ENABLED.apply(byteBuddy, instrumentation, classFileTransformer);
        } finally {
            assertThat(LambdaFactory.release(classFileTransformer), is(false));
        }
    } finally {
        assertThat(LambdaFactory.release(initialClassFileTransformer), is(true));
    }
}
Also used : ClassFileTransformer(java.lang.instrument.ClassFileTransformer) ByteBuddy(net.bytebuddy.ByteBuddy) Instrumentation(java.lang.instrument.Instrumentation) Test(org.junit.Test)

Example 33 with ClassFileTransformer

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

the class AgentBuilderDefaultApplicationRedefineTest method testRetransformation.

@Test
@AgentAttachmentRule.Enforce(retransformsClasses = true)
@IntegrationRule.Enforce
public void testRetransformation() 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 34 with ClassFileTransformer

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

the class AgentBuilderDefaultApplicationRedefineTest method testRetransformationOptionalType.

@Test
@AgentAttachmentRule.Enforce(retransformsClasses = true)
@IntegrationRule.Enforce
public void testRetransformationOptionalType() 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(optionalTypeLoader.loadClass(SimpleOptionalType.class.getName()).getName(), is(SimpleOptionalType.class.getName()));
    ClassFileTransformer classFileTransformer = new AgentBuilder.Default(new ByteBuddy().with(TypeValidation.DISABLED)).ignore(none()).disableClassFormatChanges().with(AgentBuilder.RedefinitionStrategy.REDEFINITION).with(descriptionStrategy).type(ElementMatchers.is(SimpleOptionalType.class), ElementMatchers.is(optionalTypeLoader)).transform(new FooTransformer()).installOnByteBuddyAgent();
    try {
        Class<?> type = optionalTypeLoader.loadClass(SimpleOptionalType.class.getName());
        // The hybrid strategy cannot transform optional types.
        assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) BAR));
    } finally {
        ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer);
    }
}
Also used : SimpleOptionalType(net.bytebuddy.test.packaging.SimpleOptionalType) ClassFileTransformer(java.lang.instrument.ClassFileTransformer) Instrumentation(java.lang.instrument.Instrumentation) ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 35 with ClassFileTransformer

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

the class AgentBuilderDefaultApplicationResubmissionTest method testResubmission.

@Test
@AgentAttachmentRule.Enforce(retransformsClasses = true)
@IntegrationRule.Enforce
public void testResubmission() 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.
    final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
    try {
        assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
        ClassFileTransformer classFileTransformer = new AgentBuilder.Default(new ByteBuddy().with(TypeValidation.DISABLED)).ignore(none()).disableClassFormatChanges().with(AgentBuilder.LocationStrategy.NoOp.INSTANCE).with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION).withResubmission(new AgentBuilder.RedefinitionStrategy.ResubmissionScheduler() {

            @Override
            public boolean isAlive() {
                return true;
            }

            @Override
            public Cancelable schedule(final Runnable job) {
                return new Cancelable.ForFuture(scheduledExecutorService.scheduleWithFixedDelay(job, TIMEOUT, TIMEOUT, TimeUnit.SECONDS));
            }
        }).type(ElementMatchers.is(Foo.class), ElementMatchers.is(classLoader)).transform(new FooTransformer()).installOnByteBuddyAgent();
        try {
            Class<?> type = classLoader.loadClass(Foo.class.getName());
            Thread.sleep(TimeUnit.SECONDS.toMillis(TIMEOUT * 3));
            assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) FOO));
        } finally {
            ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer);
        }
    } finally {
        scheduledExecutorService.shutdown();
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ClassFileTransformer(java.lang.instrument.ClassFileTransformer) Instrumentation(java.lang.instrument.Instrumentation) ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Aggregations

ClassFileTransformer (java.lang.instrument.ClassFileTransformer)56 Test (org.junit.Test)38 Instrumentation (java.lang.instrument.Instrumentation)35 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)9 ByteArrayClassLoader (net.bytebuddy.dynamic.loading.ByteArrayClassLoader)9 Callable (java.util.concurrent.Callable)7 ProtectionDomain (java.security.ProtectionDomain)6 SimpleType (net.bytebuddy.test.packaging.SimpleType)6 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