Search code examples
javaaspectjpointcut

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?


Solution

  • 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;
    }