フィールド値の変更を検出する必要があります。以前の値と新しい値を比較したい。フィールド名またはそのタイプがわかりません。(詳細な背景はこちら。)指定されたクラスのサンプル:
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 つのスレッドから呼び出されます。