Search in sources :

Example 6 with Instrumentation

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

the class AgentBuilderListenerTest method testReadEdgeAddingListenerCanRead.

@Test
public void testReadEdgeAddingListenerCanRead() throws Exception {
    Instrumentation instrumentation = mock(Instrumentation.class);
    JavaModule source = mock(JavaModule.class), target = mock(JavaModule.class);
    when(source.isNamed()).thenReturn(true);
    when(source.canRead(target)).thenReturn(true);
    AgentBuilder.Listener listener = new AgentBuilder.Listener.ModuleReadEdgeCompleting(instrumentation, false, Collections.singleton(target));
    listener.onTransformation(mock(TypeDescription.class), mock(ClassLoader.class), source, LOADED, mock(DynamicType.class));
    verify(source).isNamed();
    verify(source).canRead(target);
    verifyNoMoreInteractions(source);
    verifyZeroInteractions(target);
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) Instrumentation(java.lang.instrument.Instrumentation) TypeDescription(net.bytebuddy.description.type.TypeDescription) JavaModule(net.bytebuddy.utility.JavaModule) Test(org.junit.Test)

Example 7 with Instrumentation

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

the class AgentBuilderListenerTest method testReadEdgeAddingListenerUnnamed.

@Test
public void testReadEdgeAddingListenerUnnamed() throws Exception {
    Instrumentation instrumentation = mock(Instrumentation.class);
    JavaModule source = mock(JavaModule.class), target = mock(JavaModule.class);
    AgentBuilder.Listener listener = new AgentBuilder.Listener.ModuleReadEdgeCompleting(instrumentation, false, Collections.singleton(target));
    listener.onTransformation(mock(TypeDescription.class), mock(ClassLoader.class), source, LOADED, mock(DynamicType.class));
    verify(source).isNamed();
    verifyNoMoreInteractions(source);
    verifyZeroInteractions(target);
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) Instrumentation(java.lang.instrument.Instrumentation) TypeDescription(net.bytebuddy.description.type.TypeDescription) JavaModule(net.bytebuddy.utility.JavaModule) Test(org.junit.Test)

Example 8 with Instrumentation

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

the class AgentBuilderDefaultNativeMethodStrategyTest method testEnabledStrategyIsEnabled.

@Test
public void testEnabledStrategyIsEnabled() throws Exception {
    Instrumentation instrumentation = mock(Instrumentation.class);
    when(instrumentation.isNativeMethodPrefixSupported()).thenReturn(true);
    assertThat(new AgentBuilder.Default.NativeMethodStrategy.ForPrefix(FOO).isEnabled(instrumentation), is(true));
}
Also used : Instrumentation(java.lang.instrument.Instrumentation) Test(org.junit.Test)

Example 9 with Instrumentation

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

the class Installer method getInstrumentation.

/**
     * <p>
     * Returns the instrumentation that was loaded by the Byte Buddy agent. When a security manager is active,
     * the {@link RuntimePermission} for {@code getInstrumentation} is required by the caller.
     * </p>
     * <p>
     * <b>Important</b>: This method must only be invoked via the {@link ClassLoader#getSystemClassLoader()} where any
     * Java agent is loaded. It is possible that two versions of this class exist for different class loaders.
     * </p>
     *
     * @return The instrumentation instance of the Byte Buddy agent.
     */
public static Instrumentation getInstrumentation() {
    SecurityManager securityManager = System.getSecurityManager();
    if (securityManager != null) {
        securityManager.checkPermission(new RuntimePermission("getInstrumentation"));
    }
    Instrumentation instrumentation = Installer.instrumentation;
    if (instrumentation == null) {
        throw new IllegalStateException("The Byte Buddy agent is not loaded or this method is not called via the system class loader");
    }
    return instrumentation;
}
Also used : Instrumentation(java.lang.instrument.Instrumentation)

Example 10 with Instrumentation

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

the class ClassReloadingStrategyTest method testPreregisteredType.

@Test
public void testPreregisteredType() throws Exception {
    Instrumentation instrumentation = mock(Instrumentation.class);
    ClassLoader classLoader = mock(ClassLoader.class);
    when(instrumentation.isRedefineClassesSupported()).thenReturn(true);
    when(instrumentation.getInitiatedClasses(classLoader)).thenReturn(new Class<?>[0]);
    ClassReloadingStrategy classReloadingStrategy = ClassReloadingStrategy.of(instrumentation).preregistered(Object.class);
    ArgumentCaptor<ClassDefinition> classDefinition = ArgumentCaptor.forClass(ClassDefinition.class);
    classReloadingStrategy.load(classLoader, Collections.singletonMap(TypeDescription.OBJECT, new byte[] { 1, 2, 3 }));
    verify(instrumentation).redefineClasses(classDefinition.capture());
    assertEquals(Object.class, classDefinition.getValue().getDefinitionClass());
    assertThat(classDefinition.getValue().getDefinitionClassFile(), is(new byte[] { 1, 2, 3 }));
}
Also used : Instrumentation(java.lang.instrument.Instrumentation) ClassDefinition(java.lang.instrument.ClassDefinition) 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