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);
}
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);
}
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));
}
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;
}
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 }));
}
Aggregations