2012-01-31 42 views
1

我很努力創建一個aspectj切入點,因爲我無法弄清楚如何限制切入點從對象的構造函數調用到它自己的方法(但是不包括調用方法在同一類的其他實例上)。捕獲來自構造函數的cflow中的調用

背景:

我有需要通知他們的「觀察家」每當對象的數據更改對象的應用程序。爲了實現這個,我有用@NotifiableChange註解裝飾的方法,以及在完成這些方法調用後觸發通知過程的方面。

難度在於,我不想在對象構建期間觸發通知,只有在構建後調用方法時纔會觸發通知。即從objectA的構造函數到objectA自己的方法的調用不應該包含在切入點中。但是,在objectA的構造函數的過程中調用objectB的方法應在包含在切入點中。

我已經完全綁定自己嘗試各種內部代碼,內部,cflow,這個和目標,但不能創建正確的切入點。這是我現在有:(DataChangeNotifier是由相關類實現的接口)

pointcut callsWithinConstructors(DataChangeNotifier notifierObject): 
    // call to a notifiable method 
    call(@NotifiableChange * *(..)) 
    //on this object 
    && this(notifierObject) 
    //in the execution of a constructor 
    && withincode(DataChangeNotifier+.new(..)); 


// cut any method with any parameters with this annotation 
pointcut notifiable(DataChangeNotifier notifierObject): 
    call(@NotifiableChange * DataChangeNotifier+.*(..)) 
    && target(notifierObject) 
    //but not from the constructors (because there should be no notifications during construction) 
    && !cflow(callsWithinConstructors(DataChangeNotifier+)) 
    //and not from the method that gets called by notifiers - this method is handled below 
    && !withincode(* DataChangeNotifier+.listenedDataHasChanged(..)); 

但似乎第一個切入點被排除採取在構造函數中把所有的方法調用,而不僅僅是它自己的方法。

請幫助 - 我要瘋了!

感謝

回答

0

你嘗試poincut斷言:初始化(DataChangeNotifier +)?這將限制連接點到DataChangeNotifier及其子類型的構造函數。

可以進一步限制加入的cflow()執行的連接點(DataChangeNotifier + ..(..))*

如何從一個對象的構造限制一個切入點來調用它自己的方法

pointcut myOwnMethod(): execution(DataChangeNotifier+..*(..)) 
pointcut myConstructor() : initialization(DataChangeNotifier+) 

pointcut executionOfMyOwnMethodsInMyConstructor(): cflow(myConstructor()) && myOwnMethod() 

關於對當前實例(而不是其他實例)的方法的限制,確實是非常棘手......我不確定這是可能的,我明天會做一些測試

+0

謝謝doanduyhai。我可以在初始化和new()內交換切入點看起來相同的結果。不確定初始化是否有一些優點,可以查看一下。關鍵雖然是_current instance_部分,我仍然沒有找到解決方案。 – user1180316 2012-03-16 09:46:44

相關問題