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

3 つの選択肢があることを再確認します。

  • フィールド値マップを使用した pertarget/perthis の周りのセット
  • リフレクション付きのシングルトンアラウンドセット
  • 宣言された親とフィールド値マップを使用したシングルトン アラウンド セット

最善の解決策は、ポイントカットから直接前の値を取得することです (リフレクションやポイントカット間のフィールド値の記憶なし)。出来ますか?そうでない場合、どの代替案が最高のパフォーマンスを発揮しますか?

その他の注意事項

set pointcut の以前の値に関するこの議論を見つけましたが、かなり古いものです。

このメカニズムはすべて、JSF セッション スコープの Bean の内部状態の変化を検出するためのものです - Google App Engine の修正。通常、このような Bean には 100 未満のフィールドがあります。すべてが 1 つのスレッドから呼び出されます。

4

3 に答える 3

5

残念ながら、現在 AspectJ には、フィールドの古い値を表示するための組み込み機能はありません。

既に取得した 2 つのソリューションは非常に標準的なものであり、この場合はおそらくリフレクション ソリューションが最適です。

別のオプションは次のとおりです。

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

(ここにコードを書いたので、間違いがあるかもしれません)

ただし、これにより、各インスタンスを追跡するためのマップが作成され、各インスタンスにそのマップを保持する追加のフィールドが作成されるため、ターゲットごとの側面のオーバーヘッドは多かれ少なかれ同じになります。

私は現在、これに似たアスペクトを使用していますが、その場合、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;
        }
    }

「ポイントカット間のフィールド値を覚えたくない」と書いたことは知っています。私の知る限り、他に方法はありません。

于 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 に答える