暴露AspectJ set-pointcut中的先前值

zac*_*usz 9 java aop aspectj pointcuts pointcut

我必须检测字段值的变化.我想将之前的值与新值进行比较.我不知道字段名称或类型.(更多背景信息.)对于给定类的示例:

package eu.zacheusz.aspectjtries;

@eu.zacheusz.aspectjtries.MyAnnotation
public class Sample {
    private String field;
    public void modify(){
        this.field = "new";
    }
    public static void main(String[] a){
        new Sample().modify();
    }
}
Run Code Online (Sandbox Code Playgroud)

我有这个方面:

    package eu.zacheusz.aspectjtries.aspects;

    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.Aspect;

    @Aspect
    public class SampleAspect {

        @After(" set(!static !final !transient * (@eu.zacheusz.aspectjtries.MyAnnotation *) . *) && args(value) && target(m) ")
        public void afterSetField(Object m, Object value){
            System.out.println("After set field. value=" + value + " target=" + m.getClass());
        }
}
Run Code Online (Sandbox Code Playgroud)

问题是args暴露在字段集合关联点传递的值而不是字段的当前值.第27页的演示中,我发现:

sets(int p._x)[oldVal] [newVal]
Run Code Online (Sandbox Code Playgroud)

但它似乎根本没有用我的代码(注释)编译.当我尝试:

@After(" set(!static !final !transient * (@eu.zacheusz.aspectjtries.MyAnnotation *) . *)[oldVal] [newVal] && target(m) ")
    public void afterSetField(Object m, Object oldVal, Object newVal){
Run Code Online (Sandbox Code Playgroud)

然后我得到了:

Syntax error on token " set(!static !final !transient * (@eu.zacheusz.aspectjtries.MyAnnotation *) . *)[oldVal] [newVal] && target(m)", "unexpected pointcut element: '['@53:53" expected
Run Code Online (Sandbox Code Playgroud)

这是使用反射的工作解决方案:

@Around(" set(!static !final !transient * (@eu.zacheusz.aspectjtries.MyAnnotation *) . *) && args(newVal) && target(t) ")
public void aroundSetField(ProceedingJoinPoint jp, Object t, Object newVal) throws Throwable{
    Signature signature = jp.getSignature();
    String fieldName = signature.getName();
    Field field = t.getClass().getDeclaredField(fieldName);
    field.setAccessible(true);
    Object oldVal = field.get(t);
    System.out.println("Before set field. "
            + "oldVal=" + oldVal + " newVal=" + newVal + " target.class=" + t.getClass());
    //TODO compare oldVal with newVal and do sth.
    jp.proceed();
}
Run Code Online (Sandbox Code Playgroud)

这是比反射具有更好性能的解决方案(我认为).但是仍然存在很大的开销(附加字段和每个目标的绑定方面实例).

    @Aspect("perthis(set(!static !final !transient * (@eu.zacheusz.aspectjtries.MyAnnotation *) . *))")
    public class SampleAspect {            
        private final Map<String, Object> values = new HashMap<String, Object>();            
        @Around(" set(!static !final !transient * (@eu.zacheusz.aspectjtries.MyAnnotation *) . *) && args(newVal) && target(t) ")
        public void beforeSetField(ProceedingJoinPoint jp, Object t, Object newVal) throws Throwable {
            String fieldName = jp.getSignature().getName();
            Object oldVal = this.values.get(fieldName);
            System.out.println("Before set field. "
                    + "oldVal=" + oldVal + " newVal=" + newVal + " target.class=" + t.getClass());
            //TODO compare oldVal with newVal and do sth.                
            this.values.put(fieldName, newVal);
            jp.proceed();
        }
    }
Run Code Online (Sandbox Code Playgroud)

这里是使用声明父母的解决方案:

@Aspect
public class AspectC {

    public interface FieldTracker {

        Map<String, Object> getValues();
    }
    // this implementation can be outside of the aspect

    public static class FieldTrackerImpl implements FieldTracker {

        private transient Map<String, Object> values;

        @Override
        public Map<String, Object> getValues() {
            if (values == null) {
                values = new HashMap<String, Object>();
            }
            return values;
        }
    }
    // the field type must be the introduced interface. It can't be a class.
    @DeclareParents(value = "@eu.zacheusz.aspectjtries.MyAnnotation *", defaultImpl = FieldTrackerImpl.class)
    private FieldTracker implementedInterface;

    @Around("set(!static !final !transient * (@eu.zacheusz.aspectjtries.MyAnnotation *) . *) && args(newVal) && target(t)")
    public void beforeSetField(final ProceedingJoinPoint jp, final FieldTracker t, final Object newVal) throws Throwable{
        final Map<String, Object> values = t.getValues();
        final String fieldName = jp.getSignature().getName();
        final Object oldVal = values.get(fieldName);
        System.out.println("Before set field " + fieldName
                + " oldVal=" + oldVal + " newVal=" + newVal + " target.class=" + t.getClass());
        //TODO compare oldVal with newVal and do sth.
        values.put(fieldName, newVal);
        jp.proceed();
    }
Run Code Online (Sandbox Code Playgroud)

重新确认有三种选择:

  • 使用字段值映射设置的pertarget/perthis
  • 单身周围设有反射
  • 使用声明父项和字段值映射设置的单例

最好的解决方案是直接从切入点获取先前的值(没有反射或记住切入点之间的字段值).可能吗?如果没有,哪种替代品具有最佳性能?

补充说明

我在设定切入点找到了关于先前值的讨论,但它已经很老了.

所有这些机制都用于检测JSF会话范围的bean内部状态更改 - 修复Google App Engine.这样的bean通常少于100个字段.全部是从一个线程调用的.

Sim*_*nni 5

遗憾的是,AspectJ目前没有内置功能来查看该字段的旧值.

您已经获得的两个解决方案非常标准,在这种情况下,反射可能是最好的.

另一种选择是:

public aspect FieldTracker {

    public interface TrackingField {};
    public Map<String,Object> TrackingField.fields;

    declare parents : @Deprecated * : implements TrackingField;

    void around(TrackingField t, Object val) :
        set(!static !final !transient * TrackingField.*) 
        && args(val) 
        && target(t) 
    {

        String fieldName = thisJoinPointStaticPart.getSignature().getName();
        Object oldVal = t.fields == null ? null : t.fields.get(fieldName);

        // do whatever

        if (val != null) {
            if (t.fields == null) t.fields = new HashMap<String,Object>();
            t.fields.put(fieldName, val);
        }
        proceed(t,val);
    }
}
Run Code Online (Sandbox Code Playgroud)

(我在这里写了这段代码,所以可能会有一些错误)

但是这会创建一个跟踪每个实例的映射,并在每个实例中添加一个用于保存该映射的字段,因此它将为您提供或多或少相同的pertarget方面的开销.

我正在使用与当前类似的方面,但在这种情况下,我需要json序列化的映射(它比使用反射快得多),并且查看旧值的能力只是副作用.


Atr*_*eys 2

该幻灯片看起来使用的是 AspectJ 的早期版本。移植指南告诉我们,对于较旧的建议,需要删除切入点上的“s”。

以下是来自另一个教程的一条建议,该教程未使用 AspectJ 中的注释:

  aspect GuardedX {
      static final int MAX_CHANGE = 100;
      before(int newval): set(static int T.x) && args(newval) {
      if (Math.abs(newval - T.x) > MAX_CHANGE)
          throw new RuntimeException();
      }
  }
Run Code Online (Sandbox Code Playgroud)

关于您的代码:

  • 在比赛结束后应用你的建议对我来说感觉有点奇怪。将建议应用为“之前”似乎更容易理解。
  • 新值是连接点的参数,而不是切入点。切入点指定旧参数。不幸的是,在这个例子中,类型和字段名都是已知的。以便可以在建议中引用它。

需要绑定

从另一个讨论来看,似乎没有办法在不绑定连接点签名中的信息的情况下获取正在设置的字段(或其类型)的当前值。