在 get 切入点中获取访问字段的值

Get the value of the accessed field within a get pointcut

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

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

我现在需要确定由 get 切入点指定的访问字段的值。 这在 AspectJ 中可能吗?

对于 set() 切入点,您可以通过 args() 绑定值,但不能用于 get() 切入点。因此,为了在没有任何 hacky 反射技巧的情况下获得价值,只需使用 around() 通知而不是 before()。通过这种方式,您可以获得字段值作为 proceed() 的 return 值:

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