Is it possible to write a effective pointcut that matches a method that changes a class variable of a specific class type?
The point of doing this is that my classes have a lastModificationDate that I want to update to the latest date whenever a class variable is changed.
Example of method:
public void stupidMethod() {
...
for (int i = 0; i < 100; i++) this.var = whatever;
...
} <--- I want to match here
Currently I have this, but it is not very optimal:
after(SimpleEntity entity) : set(* *.*) && target(entity) && !within(SimpleEntityAspect)
No.
getandsetpointcuts exist for members only, but not for local variables. Consequently, your example will only match member variable assignments forSimpleEntityobjects. If this is what you want to do, please rephrase your question’s heading and content so as to make clear what you really want to achieve. Please also provide some more code context, such as e.g. the relevant part of the type declaration forSimpleEntity.My best guess for now is that you want to match the exit point of methods in which a certain type’s members are changed. If you also tell us what exactly you want to do in your advice (e.g. print the method name or the assigned value etc.), we might be able to better help you.
Update: Okay, I have found a solution which does what you want, using
pertargetaspect instantiation plus ITD (inter-type declaration). I have not tested performance or memory consumption, I am leaving that up to you.Sample entity class:
Sample application class creating and using entities:
Aspect doing what was requested by Piotr Blasiak:
Sample console output: