Bytecode manipulation to intercept setting the value of a field

痴心易碎 提交于 2019-12-01 05:49:25

There are two bytecodes for updating fields: putfield and putstatic (see http://java.sun.com/docs/books/jvms/second_edition/html/Instructions2.doc11.html). These will be found in the code for the using class, so there's no way to simply modify Person.

In short, you need to inject bytecode that does the following in the method of interest :

if (person.name.equals("Joe") { 
   dirty = true;
}

You cannot evaluate the field at instrumentation time - it has to be at runtime when the method is executing.

Regarding your question of how, try the following:

  • Write the code in a test class and generate an ascii version of the bytecode to see what was generated. You can do this easily with javap.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!