0

我知道方面实例(由于 perthis 或 pertarget)与它们的对象一起自动处置,但我想知道是否有一种方法可以在对象生命周期的任何时候以编程方式执行此操作。目的是对方面代码的开销进行更细粒度的控制。

考虑以下示例,我想检查每个帐户在第一次提款之前是否有初始存款。请注意,一旦我观察到第一次存款,则无需进一步保留方面实例:

aspect accounting perthis (execution(Account.new(..))) {    

after () : execution(* Account.deposit(..))
{
         //fine... discard aspect instance
}   

before () : execution(* Account.withdraw(..))
{
    assert first_deposit:"Cannot withdraw before a first deposit";  
}
4

1 回答 1

0

Are you sure that the aspect itself is really the problem? An aspect instance will be fairly small if it doesn't contain much state. And if your aspect instance does contain significant amount of state, you'd be better off just nulling out the fields in the aspect instead of trying to disconnect the aspect from the instance.

于 2013-04-05T19:07:53.377 回答