Search in sources :

Example 1 with ClassFileTransformer

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"));
}
Also used : ProtectionDomain(java.security.ProtectionDomain) ClassFileTransformer(java.lang.instrument.ClassFileTransformer) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) ArrayList(java.util.ArrayList) URLClassLoader(java.net.URLClassLoader) Test(org.junit.Test)

Example 2 with ClassFileTransformer

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;
        }
    });
}
Also used : ProtectionDomain(java.security.ProtectionDomain) ClassFileTransformer(java.lang.instrument.ClassFileTransformer) ClassPool(javassist.ClassPool) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) CtClass(javassist.CtClass) CtField(javassist.CtField) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) CtMethod(javassist.CtMethod)

Example 3 with ClassFileTransformer

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);
}
Also used : ClassFileTransformer(java.lang.instrument.ClassFileTransformer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) UnmodifiableClassException(java.lang.instrument.UnmodifiableClassException) Instrumentation(java.lang.instrument.Instrumentation) UnmodifiableClassException(java.lang.instrument.UnmodifiableClassException) Test(org.junit.Test)

Example 4 with ClassFileTransformer

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);
}
Also used : MatchableClassFileTransformer(com.navercorp.pinpoint.profiler.plugin.MatchableClassFileTransformer) ClassFileTransformer(java.lang.instrument.ClassFileTransformer)

Example 5 with ClassFileTransformer

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;
}
Also used : ClassFileTransformer(java.lang.instrument.ClassFileTransformer)

Aggregations

ClassFileTransformer (java.lang.instrument.ClassFileTransformer)80 Test (org.junit.Test)40 Instrumentation (java.lang.instrument.Instrumentation)37 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)17 ProtectionDomain (java.security.ProtectionDomain)11 ByteArrayClassLoader (net.bytebuddy.dynamic.loading.ByteArrayClassLoader)9 Callable (java.util.concurrent.Callable)7 MatchableClassFileTransformer (com.navercorp.pinpoint.profiler.plugin.MatchableClassFileTransformer)6 SimpleType (net.bytebuddy.test.packaging.SimpleType)6 ArrayList (java.util.ArrayList)5 ByteBuddy (net.bytebuddy.ByteBuddy)5 IOException (java.io.IOException)4 URLClassLoader (java.net.URLClassLoader)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 InputStream (java.io.InputStream)3 URL (java.net.URL)3 CodeSource (java.security.CodeSource)3 UnmodifiableClassException (java.lang.instrument.UnmodifiableClassException)2 Certificate (java.security.cert.Certificate)2