我觉得这是我应该知道的,但是java代理(用-javaagent指定)是否在一个单独的线程中运行?我已经阅读了a Java agent is a pluggable library that runs embedded in a JVM and intercepts the classloading process,但我想确定:它是否真的拦截它们(这听起来像是在另一个线程中运行并监视类加载)或是否由JVM(JVM调用它来解析类加载;它没有单独的线程)?
再次,我觉得我应该能够弄明白,但对我好,我工作太辛苦了,我的大脑有点油炸:P
谢谢!
答案 0 :(得分:5)
ClassFoader调用ClassFileTransformer。从链接的文章中对变换器进行一点修改,如下所示:
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;
}
产生此输出:
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)