如何检测java系统类?
我正在尝试向系统类(FileInputStream/FileOutputStream)添加自定义行为。我使用以下转换方法编写了自定义 ClassFileTransformer:
public byte[] transform(ClassLoader arg0, String arg1, Class arg2, ProtectionDomain arg3, byte[] arg4) throws IllegalClassFormatException {
System.out.println("class name: " + arg1);
return arg4;
}
当我运行示例程序时:
public static void main(String[] args) throws Exception {
new FileOutputStream("file");
}
我看到没有系统类未传递给转换。
有没有办法修改系统类?提前致谢!
I'm trying to add custom behaviour to system classes (FileInputStream/FileOutputStream). I wrote custom ClassFileTransformer with the following transform method:
public byte[] transform(ClassLoader arg0, String arg1, Class arg2, ProtectionDomain arg3, byte[] arg4) throws IllegalClassFormatException {
System.out.println("class name: " + arg1);
return arg4;
}
When I run sample program:
public static void main(String[] args) throws Exception {
new FileOutputStream("file");
}
I see that no system classes are not passed to transform.
Is there any way to modify system classes? Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在调用 pre-main 方法并添加 ClassFileTransformer 之前,一些(不是全部)系统类已经加载。如果您也想转换这些类,则可以在添加 ClassFileTransformer 后调用类似 Instrumentation#retransformClasses(Instrumentation#getAllLoadedClasses()) 的内容。请注意,您必须使用 Instrumentation#addTransformer(ClassFileTransformer, true) 来指示您的转换器支持类的重新转换。
Some (not all) system classes are already loaded before the pre-main method is invoked and your ClassFileTransformer is added. If you want to transform these classes as well, you can invoke something like Instrumentation#retransformClasses(Instrumentation#getAllLoadedClasses()) after adding your ClassFileTransformer. Note, you have to use Instrumentation#addTransformer(ClassFileTransformer, true) to indicate that your transformer supports retransformation of classes.