Search in sources :

Example 16 with Instrumentation

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

the class ClassReloadingStrategyTest method testAnonymousType.

@Test
@JavaVersionRule.Enforce(8)
@AgentAttachmentRule.Enforce(retransformsClasses = true)
public void testAnonymousType() throws Exception {
    ClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassFileExtraction.of(Class.forName(LAMBDA_SAMPLE_FACTORY)), ByteArrayClassLoader.PersistenceHandler.MANIFEST);
    Instrumentation instrumentation = ByteBuddyAgent.install();
    Class<?> factory = classLoader.loadClass(LAMBDA_SAMPLE_FACTORY);
    @SuppressWarnings("unchecked") Callable<String> instance = (Callable<String>) factory.getDeclaredMethod("nonCapturing").invoke(factory.getDeclaredConstructor().newInstance());
    // Anonymous types can only be reset to their original format, if a retransformation is applied.
    ClassReloadingStrategy classReloadingStrategy = new ClassReloadingStrategy(instrumentation, ClassReloadingStrategy.Strategy.RETRANSFORMATION).preregistered(instance.getClass());
    ClassFileLocator classFileLocator = ClassFileLocator.AgentBased.of(instrumentation, instance.getClass());
    try {
        assertThat(instance.call(), is(FOO));
        new ByteBuddy().redefine(instance.getClass(), classFileLocator).method(named("call")).intercept(FixedValue.value(BAR)).make().load(instance.getClass().getClassLoader(), classReloadingStrategy);
        assertThat(instance.call(), is(BAR));
    } finally {
        classReloadingStrategy.reset(classFileLocator, instance.getClass());
        assertThat(instance.call(), is(FOO));
    }
}
Also used : ClassFileLocator(net.bytebuddy.dynamic.ClassFileLocator) Instrumentation(java.lang.instrument.Instrumentation) ByteBuddy(net.bytebuddy.ByteBuddy) RandomString(net.bytebuddy.utility.RandomString) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 17 with Instrumentation

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

the class ClassReloadingStrategyTest method testRetransformationDiscovery.

@Test
public void testRetransformationDiscovery() throws Exception {
    Instrumentation instrumentation = mock(Instrumentation.class);
    when(instrumentation.isRetransformClassesSupported()).thenReturn(true);
    assertThat(ClassReloadingStrategy.of(instrumentation), notNullValue(ClassReloadingStrategy.class));
}
Also used : Instrumentation(java.lang.instrument.Instrumentation) Test(org.junit.Test)

Example 18 with Instrumentation

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

the class ClassReloadingStrategyTest method testResetNotSupported.

@Test
public void testResetNotSupported() throws Exception {
    Instrumentation instrumentation = mock(Instrumentation.class);
    when(instrumentation.isRetransformClassesSupported()).thenReturn(true);
    new ClassReloadingStrategy(instrumentation, ClassReloadingStrategy.Strategy.RETRANSFORMATION).reset();
}
Also used : Instrumentation(java.lang.instrument.Instrumentation) Test(org.junit.Test)

Example 19 with Instrumentation

use of java.lang.instrument.Instrumentation 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 20 with Instrumentation

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

the class AgentBuilderRedefinitionStrategyTest method testRedefinitionStrategyIsChecked.

@Test
public void testRedefinitionStrategyIsChecked() throws Exception {
    Instrumentation instrumentation = mock(Instrumentation.class);
    when(instrumentation.isRedefineClassesSupported()).thenReturn(true);
    AgentBuilder.RedefinitionStrategy.REDEFINITION.check(instrumentation);
}
Also used : Instrumentation(java.lang.instrument.Instrumentation) Test(org.junit.Test)

Aggregations

Instrumentation (java.lang.instrument.Instrumentation)31 Test (org.junit.Test)21 TypeDescription (net.bytebuddy.description.type.TypeDescription)8 DynamicType (net.bytebuddy.dynamic.DynamicType)8 JavaModule (net.bytebuddy.utility.JavaModule)6 ClassFileTransformer (java.lang.instrument.ClassFileTransformer)4 InterceptorRegistryBinder (com.navercorp.pinpoint.profiler.interceptor.registry.InterceptorRegistryBinder)3 TestInterceptorRegistryBinder (com.navercorp.pinpoint.profiler.util.TestInterceptorRegistryBinder)3 ByteBuddy (net.bytebuddy.ByteBuddy)3 AgentOption (com.navercorp.pinpoint.bootstrap.AgentOption)2 DefaultAgentOption (com.navercorp.pinpoint.bootstrap.DefaultAgentOption)2 DefaultProfilerConfig (com.navercorp.pinpoint.bootstrap.config.DefaultProfilerConfig)2 ProfilerConfig (com.navercorp.pinpoint.bootstrap.config.ProfilerConfig)2 DefaultAnnotationKeyRegistryService (com.navercorp.pinpoint.common.service.DefaultAnnotationKeyRegistryService)2 DefaultServiceTypeRegistryService (com.navercorp.pinpoint.common.service.DefaultServiceTypeRegistryService)2 ApiMetaDataService (com.navercorp.pinpoint.profiler.metadata.ApiMetaDataService)2 ObjectBinderFactory (com.navercorp.pinpoint.profiler.objectfactory.ObjectBinderFactory)2 Properties (java.util.Properties)2 Callable (java.util.concurrent.Callable)2 JarFile (java.util.jar.JarFile)2