views:

50

answers:

1

I feel that this is something I should know, but does a java agent (specified with -javaagent) run in a separate thread? I've read that a Java agent is a pluggable library that runs embedded in a JVM and intercepts the classloading process, but I want to make sure: does it really intercept them (which sounds like it's running in another thread and monitoring class loads) or is it notified by the JVM (the JVM invokes it to resolve the class load; there isn't a separate thread for it)?

Again, I get the feeling that I should be able to figure it out, but be nice to me, I'm working too hard and my brain is kinda fried :P

Thanks!

+2  A: 

The ClassFileTransformer gets invoked by the ClassLoader. A little modification of the transformer from the article you linked, to look like this:

public byte[] transform(ClassLoader loader, String className,
        Class redefiningClass, ProtectionDomain domain, byte[] bytes)
        throws IllegalClassFormatException {
    new RuntimeException("Transformer to Transform Class: " + className)
            .printStackTrace(System.out);
    return bytes;
}

yields this output:

java.lang.RuntimeException: Transformer to Transform Class: MyMain
    at com.javalobby.tnt.instrument.SimpleTransformer.transform(SimpleTransformer.java:14)
    at sun.instrument.TransformerManager.transform(Unknown Source)
    at sun.instrument.InstrumentationImpl.transform(Unknown Source)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$000(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
Christian Semrau
Great, thanks. Very clever way to find out, btw :). Thanks for trying it out!
Alix