You can use before advice with reflection
to achieve your goal. Here's a simple example where the field is public and is
not a static member. Generalizing to handling private fields and static members
is left as an exercise for the reader :-)
before(int newVal, Object container) :
set(int ModelClass.*) && args(newVal) && this(container) {
Class inClass =
thisJoinPoint.getSignature().getDeclaringType();
String fieldName = thisJoinPoint.getSignature().getName();
Field field = inClass.getField(fieldName);
int oldVal = field.getInt(container);
//System.out.println("change "+oldVal+" to
"+newVal);
if (oldVal != newVal)
{
} catch (Exception e) {
e.printStackTrace(); } // handle properly
}
Hello,
I
have the following Problem:
If
have some classes representing a data structure. I want to write an aspect,
that adds a dirty flag to a class, that turns true, if one of the values
changed. So far the adding of the Flag is not the problem. My Problem is to
find out, if any of the classvariables is set with a new value. New means in
that case that it is not the old value written again (what my Gui does). To get
the new Value is no problem with a combination of set und args. My Problem is
how to find out the previous value of the variable witch is written. Target
returns the target instance and not the Variable. Are there any solutions to
this problem?
Best
regards,
Jan
Kerssenfischer