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

后端 未结 4 943
花落未央
花落未央 2021-01-03 01:18

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

4条回答
  •  执笔经年
    2021-01-03 01:42

    There are several libraries which you can use. See for example here. Once a class was already loaded/initialized by the VM it will be impossible to manipulate, though.

    By the way, in principle you can also just replace the class to be 'hooked' with your own proxy class file. As long as the class' visible interface does not change this may work. (Sub-classes of the class may horribly fail at runtime though.) This replacement can be as easy as changing the classpath so that your class of the same name will be found first, before the original one. Delegating to the original class of the same name may be a little more complex in this case.

提交回复
热议问题