Get the value of the accessed field within a get pointcut

痞子三分冷 提交于 2019-12-13 04:49:10

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/27906712/get-the-value-of-the-accessed-field-within-a-get-pointcut

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!