Bytecode manipulation to intercept setting the value of a field

感情迁移 提交于 2019-12-19 08:12:42

问题


Using a library like ASM or cglib, is there a way to add bytecode instructions to a class to execute code whenever the value of a class field is set?

For example, let’s say I have this class:


   public class Person
   {  
       bool dirty;
       public String name;
       public Date birthDate;
       public double salary;
   }

Let’s say a section of code contains this line:

person.name = "Joe";

I want this instruction to be intercepted so the dirty flag is set to true. I know this is possible for setter methods -- person.setName (“Joe”) -- as class methods can be modified by bytecode manipulation, but I want to do the same thing for a field.

Is this possible, and if so, how?

EDIT

I want to avoid modifying the code section that accesses the class, I'm looking for a way to keep the interception code as part of the Person class. Are there a pseudo-methods for field access, similar to properties in Python classes?


回答1:


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.




回答2:


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.


来源:https://stackoverflow.com/questions/4198164/bytecode-manipulation-to-intercept-setting-the-value-of-a-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!