use of java.lang.instrument.ClassFileTransformer in project pinpoint by naver.
the class DefaultDynamicTransformerRegistry method getTransformer.
@Override
public ClassFileTransformer getTransformer(ClassLoader classLoader, String targetClassName) {
// TODO fix classLoader null case
if (transformerMap.isEmpty()) {
return null;
}
final TransformerKey key = new TransformerKey(classLoader, targetClassName);
final ClassFileTransformer transformer = transformerMap.remove(key);
if (transformer != null) {
if (logger.isInfoEnabled()) {
logger.info("removed dynamic transformer classLoader: {}, className: {}, registry size: {}", classLoader, targetClassName, transformerMap.size());
}
}
return transformer;
}
use of java.lang.instrument.ClassFileTransformer in project pinpoint by naver.
the class DynamicTransformServiceTest method testRetransform_Fail_memoryleak_prevent.
@Test()
public void testRetransform_Fail_memoryleak_prevent() throws Exception {
final Instrumentation instrumentation = mock(Instrumentation.class);
when(instrumentation.isModifiableClass(any(Class.class))).thenReturn(true);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
throw new UnmodifiableClassException();
}
}).when(instrumentation).retransformClasses(any(Class.class));
DefaultDynamicTransformerRegistry listener = new DefaultDynamicTransformerRegistry();
final ClassFileTransformer classFileTransformer = mock(ClassFileTransformer.class);
DynamicTransformService dynamicTransformService = new DynamicTransformService(instrumentation, listener);
try {
dynamicTransformService.retransform(String.class, classFileTransformer);
Assert.fail("expected retransform fail");
} catch (Exception e) {
}
Assert.assertEquals(listener.size(), 0);
}
use of java.lang.instrument.ClassFileTransformer in project byte-buddy by raphw.
the class AgentBuilderDefaultApplicationTest method testSerializableLambda.
@Test
@JavaVersionRule.Enforce(8)
@AgentAttachmentRule.Enforce(redefinesClasses = true)
@IntegrationRule.Enforce
public void testSerializableLambda() throws Exception {
assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
ClassLoader classLoader = lambdaSamples();
ClassFileTransformer classFileTransformer = new AgentBuilder.Default().with(poolStrategy).ignore(none()).with(AgentBuilder.LambdaInstrumentationStrategy.ENABLED).installOn(ByteBuddyAgent.getInstrumentation());
try {
Class<?> sampleFactory = classLoader.loadClass(LAMBDA_SAMPLE_FACTORY);
@SuppressWarnings("unchecked") Callable<String> instance = (Callable<String>) sampleFactory.getDeclaredMethod("serializable", String.class).invoke(sampleFactory.getDeclaredConstructor().newInstance(), FOO);
assertThat(instance.call(), is(FOO));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(instance);
objectOutputStream.close();
ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(outputStream.toByteArray()));
@SuppressWarnings("unchecked") Callable<String> deserialized = (Callable<String>) objectInputStream.readObject();
assertThat(deserialized.call(), is(FOO));
objectInputStream.close();
} finally {
ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer);
AgentBuilder.LambdaInstrumentationStrategy.release(classFileTransformer, ByteBuddyAgent.getInstrumentation());
}
}
use of java.lang.instrument.ClassFileTransformer in project byte-buddy by raphw.
the class AgentBuilderDefaultApplicationTest method testAgentSelfInitializationAuxiliaryTypeLazy.
@Test
@AgentAttachmentRule.Enforce
@IntegrationRule.Enforce
public void testAgentSelfInitializationAuxiliaryTypeLazy() throws Exception {
assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
ClassFileTransformer classFileTransformer = new AgentBuilder.Default().with(poolStrategy).ignore(none()).type(ElementMatchers.is(QuxBaz.class), ElementMatchers.is(classLoader)).transform(new QuxBazTransformer()).installOnByteBuddyAgent();
try {
Class<?> type = classLoader.loadClass(QuxBaz.class.getName());
assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) (FOO + BAR)));
} finally {
ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer);
}
}
use of java.lang.instrument.ClassFileTransformer in project byte-buddy by raphw.
the class AgentBuilderDefaultApplicationTest method testDecorationBlocked.
@Test
@AgentAttachmentRule.Enforce
@IntegrationRule.Enforce
public void testDecorationBlocked() throws Exception {
assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
ClassFileTransformer classFileTransformer = new AgentBuilder.Default().with(poolStrategy).ignore(none()).type(ElementMatchers.is(Foo.class), ElementMatchers.is(classLoader)).transform(new BarAdviceTransformer()).asDecorator().type(ElementMatchers.is(Foo.class), ElementMatchers.is(classLoader)).transform(new QuxAdviceTransformer()).installOnByteBuddyAgent();
try {
Class<?> type = classLoader.loadClass(Foo.class.getName());
assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) (FOO + QUX)));
} finally {
ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer);
}
}
Aggregations