9

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

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

我有这个方面:

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

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

sets(int p._x)[oldVal] [newVal]

但它似乎根本无法用我的代码(注释)编译。当我尝试时:

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

然后我得到:

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

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

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

这是性能比反射更好的解决方案(我认为)。但是仍然有很大的开销(附加字段,以及将切面实例绑定到每个目标)。

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

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

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

重新总结有三种选择:

  • pertarget/perthis 围绕设置与字段值映射
  • 带反射的单例
  • 带有声明父母和字段值映射的单例

最好的解决方案是直接从切入点获取先前的值(无需反射或记住切入点之间的字段值)。可能吗?如果不是,哪个替代方案的性能最好?

补充说明

我发现this discussion about previous value in set pointcut,但它已经很老了。

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

4

3 回答 3

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

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

但这会创建一个映射来跟踪每个实例,并在每个实例中创建一个附加字段来保存该映射,因此它会给您或多或少与 pertarget 方面相同的开销。

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

于 2011-07-21T21:19:40.947 回答
3

有更好的解决方案。它具有比反射更好的性能。

    @Aspect("pertarget(set(!static !final !transient * (@Deprecated *) . *))")
    public class SampleAspect {

        private Object oldVal;

        @Before(" set(!static !final !transient * (@Deprecated *) . *) && args(newVal) && target(t) ")
        public void beforeSetField(Object t, Object newVal) throws Throwable{
            System.out.println("Before set field. "
                    + "oldVal=" + oldVal + " newVal=" + newVal + " target.class=" + t.getClass());
            this.oldVal = newVal;
        }
    }

我知道您写道您“不想记住切入点之间的字段值”。AFAIK没有其他办法。

于 2011-07-20T00:58:53.920 回答
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();
      }
  }

关于您的代码:

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

需要绑定

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

于 2011-07-14T18:58:51.073 回答