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