I'm pretty new to AspectJ and have a problem that inspite if some research I'm not able to fix. I have the following aspect concerning a Bank, the aspect checks whether the balance of the Bank holds after every public method invocation.
pointcut BankCheck(Bank bank): call(public * Bank.*(..)) && target(bank);
Object around(Bank bank): BankCheck(bank) {
int balance = bank.getTotalBalance();
Object result = proceed(bank);
if (balance != bank.getTotalBalance()) {
LOGGER.warn("The total balance of the bank is not equal.");
} else {
LOGGER.info("Ok");
}
return result;
}
The problem is that in the aspect I use the method bank.getTotalBalance() which is itself a public Bank method. Therefore the aspect is advised every time and this recursion problem keeps on going until an exception is trowed. Is there a way to fix this, for example by turning off the advise mechanism inside the aspect?
Try this:
public aspect BankTotalBalanceAspect {
pointcut BankCheck(Bank bank): call(public * Bank.*(..)) && target(bank);
Object around(Bank bank): BankCheck(bank) && !within(BankTotalBalanceAspect) {
int balance = bank.getTotalBalance();
Object result = proceed(bank);
if (balance != bank.getTotalBalance()) {
LOGGER.warn("The total balance of the bank is not equal.");
} else {
LOGGER.info("Ok");
}
return result;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With