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?