Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] set() capture oldvalue




Tim,

I'm sure there was a thread on either aspectj-users or aspectj-dev but I
can't find it. Bottom line is the existing field value is not a property of
the join point so you must get it directly. The simple examples below shows
you how to use reflection or direct access:

public class SomeClass {

      private int intField;

      public void setInt (int i) {
            intField = i;
      }
}

public aspect Aspect {

      pointcut fieldSet(int newValue) :
            set(* SomeClass+.*) && args(newValue)
            && withincode(* SomeClass+.set*(..))
            ;

      before (int newValue) : fieldSet (newValue) {

            Object oldValue = null;
            try {
                  Class clazz =
thisJoinPoint.getSignature().getDeclaringType();
                  String fieldName =
thisJoinPoint.getSignature().getName();
                  Field field = clazz.getDeclaredField(fieldName);
                  field.setAccessible(true);
                  oldValue = field.get(thisJoinPoint.getThis());
            }
            catch (Exception ex) {
                  ex.printStackTrace();
            }
            System.out.println("? beforeFieldSet() oldVaue=" + oldValue +
", newValue=" + newValue);
      }
}

public privileged aspect PrivilegedAspect {

      pointcut intFieldSet(SomeClass obj, int newValue) :
            set(* intField) && target(obj) && args(newValue)
            && withincode(* SomeClass+.set*(..))
            ;

      before (SomeClass obj, int newValue) : intFieldSet (obj,newValue) {
            int oldValue = obj.intField;
            System.out.println("? beforeIntFieldSet() oldVaue=" + oldValue
+ ", newValue=" + newValue);
      }

}

Matthew Webster
AOSD Project
Java Technology Centre, MP146
IBM Hursley Park, Winchester,  SO21 2JN, England
Telephone: +44 196 2816139 (external) 246139 (internal)
Email: Matthew Webster/UK/IBM @ IBMGB, matthew_webster@xxxxxxxxxx
http://w3.hursley.ibm.com/~websterm/

"Tim Schafer" <tschafer@xxxxxxxxxxx>@eclipse.org on 11/05/2005 21:47:44

Please respond to aspectj-users@xxxxxxxxxxx

Sent by:    aspectj-users-bounces@xxxxxxxxxxx


To:    aspectj-users@xxxxxxxxxxx
cc:
Subject:    [aspectj-users] set() capture oldvalue


I've used the following pointcut to capture all field modifications that
occur within a setter method.

pointcut fieldSet(Object newValue) : set(* SomeClass+.*) && args(newValue)
&& withincode(* SomeClass+.set*(..));

I can capture the new value, but I can't capture the old value without
resorting to reflection.
In theory aspectJ could provide access to the old value more statically.
But aspectJ doesn't seem to support this.
Or am I missing something?


Tim Schafer
tschafer@xxxxxxxxxxx


_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users




Back to the top