Does the java agent work in a separate thread?

I feel that this is what I should know, but does the java agent (specified with -javaagent) work in a separate thread? I read that the Java agent is a plug-in library that runs in the JVM and intercepts the process of loading classes , but I want to make sure whether it really intercepts them (which sounds like it is working in another thread and loading class classes) or is it notified by the JVM (does the JVM call it to allow class loading, there is no separate thread for it)?

Again, I get the feeling that I should be able to understand this, but be kind to me, I work too much, and my brain is fried: P

Thank!

+3
source share
1 answer

ClassFileTransformer is called by ClassLoader. A small modification of the transformer from the article you linked is as follows:

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;
}

outputs this result:

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)
+5
source

Source: https://habr.com/ru/post/1750659/


All Articles