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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With