use of java.lang.instrument.ClassFileTransformer in project jetty.project by eclipse.
the class WebAppClassLoaderTest method testClassFileTranslations.
@Test
public void testClassFileTranslations() throws Exception {
final List<Object> results = new ArrayList<Object>();
_loader.addTransformer(new ClassFileTransformer() {
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
results.add(loader);
byte[] b = new byte[classfileBuffer.length];
for (int i = 0; i < classfileBuffer.length; i++) b[i] = (byte) (classfileBuffer[i] ^ 0xff);
return b;
}
});
_loader.addTransformer(new ClassFileTransformer() {
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
results.add(className);
byte[] b = new byte[classfileBuffer.length];
for (int i = 0; i < classfileBuffer.length; i++) b[i] = (byte) (classfileBuffer[i] ^ 0xff);
return b;
}
});
_context.setParentLoaderPriority(false);
assertCanLoadClass("org.acme.webapp.ClassInJarA");
assertCanLoadClass("org.acme.webapp.ClassInJarB");
assertCanLoadClass("org.acme.other.ClassInClassesC");
assertCanLoadClass("java.lang.String");
assertCantLoadClass("org.eclipse.jetty.webapp.Configuration");
assertThat("Classname Results", results, contains(_loader, "org.acme.webapp.ClassInJarA", _loader, "org.acme.webapp.ClassInJarB", _loader, "org.acme.other.ClassInClassesC"));
}
use of java.lang.instrument.ClassFileTransformer in project jersey by jersey.
the class PerfTestAgent method premain.
public static void premain(String agentArgs, Instrumentation instrumentation) {
final String handlerClassName = (agentArgs != null && !agentArgs.isEmpty()) ? agentArgs.substring(0, agentArgs.lastIndexOf('.')) : HANDLER_CLASS_NAME;
final String handlerMethodName = (agentArgs != null && !agentArgs.isEmpty()) ? agentArgs.substring(agentArgs.lastIndexOf('.') + 1) : HANDLER_METHOD_NAME;
instrumentation.addTransformer(new ClassFileTransformer() {
@Override
public byte[] transform(ClassLoader loader, String className, Class<?> aClass, ProtectionDomain protectionDomain, byte[] bytes) throws IllegalClassFormatException {
if (handlerClassName.replaceAll("\\.", "/").equals(className)) {
try {
ClassPool cp = ClassPool.getDefault();
cp.appendSystemPath();
CtClass cc = cp.makeClass(new java.io.ByteArrayInputStream(bytes));
final CtField ctxField = CtField.make("public static final agent.metrics.Timer.Context agentTimerCtx;", cc);
final CtField registryField = CtField.make("public static final agent.metrics.MetricRegistry agentREG = new agent.metrics.MetricRegistry();", cc);
final CtField reporterField = CtField.make("public static final agent.metrics.JmxReporter agentReporter = agent.metrics.JmxReporter.forRegistry(agentREG).build();", cc);
final CtField timerField = CtField.make("public static final agent.metrics.Timer agentTimer = " + "agentREG.timer(agent.metrics.MetricRegistry.name(\"" + handlerClassName + "\", new String[] {\"" + handlerMethodName + "\"}));", cc);
cc.addField(registryField);
cc.addField(reporterField);
cc.addField(timerField);
cc.makeClassInitializer().insertAfter("agentReporter.start();");
CtMethod m = cc.getDeclaredMethod(handlerMethodName);
m.addLocalVariable("agentCtx", ctxField.getType());
m.insertBefore("agentCtx = agentTimer.time();");
m.insertAfter("agentCtx.stop();", true);
byte[] byteCode = cc.toBytecode();
cc.detach();
System.out.printf("Jersey Perf Agent Instrumentation Done! (instrumented method: %s)\n", m.getLongName());
return byteCode;
} catch (Exception ex) {
ex.printStackTrace();
}
}
return null;
}
});
}
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 pinpoint by naver.
the class DefaultClassFileTransformerDispatcher method transform.
@Override
public byte[] transform(ClassLoader classLoader, String classInternalName, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws IllegalClassFormatException {
if (!pinpointClassFilter.accept(classLoader, classInternalName, classBeingRedefined, protectionDomain, classFileBuffer)) {
return null;
}
final ClassFileTransformer dynamicTransformer = dynamicTransformerRegistry.getTransformer(classLoader, classInternalName);
if (dynamicTransformer != null) {
return transform0(classLoader, classInternalName, classBeingRedefined, protectionDomain, classFileBuffer, dynamicTransformer);
}
if (!unmodifiableFilter.accept(classLoader, classInternalName, classBeingRedefined, protectionDomain, classFileBuffer)) {
return null;
}
ClassFileTransformer transformer = this.transformerRegistry.findTransformer(classInternalName);
if (transformer == null) {
// For debug
// TODO What if a modifier is duplicated?
transformer = this.debugTransformerRegistry.findTransformer(classInternalName);
if (transformer == null) {
return null;
}
}
return transform0(classLoader, classInternalName, classBeingRedefined, protectionDomain, classFileBuffer, transformer);
}
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;
}
Aggregations