Can I modify the byte code of a Java method in the runtime?

Cal*_*wok 4 java proxy hook bytecode javassist

I am writing a plugin of another large java program .

I want to modify some byte code of some java method of the java program during runtime, so that I can intercept the method calls (namely, inject some hooking code into the method).

Any way can achieve this?

PS:

I've checked the following approaches:

1.change the classloader of the java program. (we CANNOT change it)
2.use java proxy. (We CANNOT use java proxy, because java proxy would create a new proxy object. We DON'T use the proxy object. We need to hook the java program's object, and Use that object)
3. use -javaagent option ( we CANNOT add the commandline option for the java program.)

PS more [Edited again]:
My classes was loaded by ext class loader (I put my jar files in JAVA_HOME\lib\ext folder). The large java program is an applet program loaded by Browser. When the browser start the applet, it also loads my classes.

PS更多[再次编辑]:
虽然它在Applet中运行.我可以获得完全许可.因为我可以修改java.policy和java.security文件.

谢谢你,
凯文

Ani*_*ony 6

只需使用-javaagent opiton,它用于在运行时修改字节码.您可以从此链接此链接中找到有关-javaagent的更多信息


Jim*_*myB 2

有几个库可供您使用。参见此处的示例。不过,一旦虚拟机加载/初始化了一个类,就无法对其进行操作。

顺便说一句,原则上您也可以用您自己的代理类文件替换要“挂钩”的类。只要类的可见接口不改变,这就可以工作。(不过,该类的子类在运行时可能会严重失败。)这种替换可以像更改类路径一样简单,以便在原始类之前先找到同名的类。在这种情况下,委托给同名的原始类可能会稍微复杂一些。