th3falc0n
th3falc0n

Reputation: 1427

Get the value of the accessed field within a get pointcut

I have a pointcut which listens to access to an field in DBRow and all subclasses

before(DBRow targ) throws DBException: get(@InDB * DBRow+.*) && target(targ) {
    targ.load();
}

I now need to determine the value of the accesed field, that is specified by the get pointcut. Is this possible in AspectJ?

Upvotes: 0

Views: 148

Answers (1)

kriegaex
kriegaex

Reputation: 67457

For set() pointcuts you can bind the value via args(), but not for get() pointcuts. So in order to get the value without any hacky reflection tricks, just use an around() advice instead of before(). This way you can get the field value as a return value of proceed():

Object around(DBRow dbRow) : get(@InDB * DBRow+.*) && target(dbRow) {
    Object value = proceed(dbRow);
    System.out.println(thisJoinPoint);
    System.out.println("  " + dbRow + " -> " + value);
    dbRow.load();
    return value;
}

Upvotes: 2

Related Questions