在获取切入点中获取被访问字段的值 [英] Get the value of the accessed field within a get pointcut

查看:92
本文介绍了在获取切入点中获取被访问字段的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个切入点,可以监听对DBRow和所有子类中字段的访问

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

我现在需要确定get切入点指定的acesed字段的值. 在AspectJ中有可能吗?

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

推荐答案

对于set()切入点,您可以通过args()绑定值,但对于get()切入点则不能.因此,为了获得没有任何反省技巧的价值,只需使用around()建议而不是before().这样,您就可以将字段值作为返回值proceed():

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

这篇关于在获取切入点中获取被访问字段的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆