本文介绍了如何检测java系统类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向系统类添加自定义行为(FileInputStream / FileOutputStream)。我使用以下转换方法编写了自定义ClassFileTransformer:

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

当我运行示例程序时:

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!

推荐答案

在调用pre-main方法和ClassFileTransformer之前,已经加载了一些(并非所有)系统类被添加。如果你想要转换这些类,你可以在添加ClassFileTransformer之后调用Instrumentation #reversformClasses(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.

这篇关于如何检测java系统类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 04:50