2010-06-18 56 views
3

我覺得這是我應該知道的,但是java代理(用-javaagent指定)在單獨的線程中運行?我讀過a Java agent is a pluggable library that runs embedded in a JVM and intercepts the classloading process,但我想確認:是否真的攔截了它們(聽起來像是在另一個線程中運行並監視類加載),還是由JVM通知(JVM調用它來解析類加載;它沒有一個單獨的線程)?java代理是否在單獨的線程中運行?

再次,我得到我應該能夠弄清楚,但對我很好的感覺,我的工作太辛苦,我的大腦有點炸:P

謝謝!

回答

5

ClassFileTransformer被ClassLoader調用。從你鏈接的文章,變壓器的稍加修飾,看起來像這樣:

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) 
+0

很好,謝謝。非常聰明的方式來找出,順便說一句:)。感謝您的嘗試! – Alix 2010-06-18 16:02:34

相關問題