Last active
August 29, 2015 14:00
-
-
Save pfmiles/1bfd57f80f7fcb012c54 to your computer and use it in GitHub Desktop.
利用instrumentation打印运行过程中实际使用到的java类
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Manifest-Version: 1.0 | |
Premain-Class: test.PrintClsAgent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PrintClsAgent { | |
public static void premain(String args, Instrumentation inst) { | |
File f = new File(args); | |
try { | |
final FileWriter fw = new FileWriter(f); | |
inst.addTransformer(new ClassFileTransformer() { | |
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, | |
ProtectionDomain protectionDomain, byte[] classfileBuffer) | |
throws IllegalClassFormatException { | |
try { | |
fw.write(className + "\n"); | |
fw.flush(); | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
return classfileBuffer; | |
} | |
}); | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
public static void agentmain(String args, Instrumentation inst) { | |
premain(args, inst); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
运行时传给vm参数:-javaagent:/tmp/prtCls.jar=/tmp/class.txt