Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursion using AspectJ

Tags:

java

aspectj

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?

like image 979
Joris Mees Avatar asked Aug 27 '11 10:08

Joris Mees


1 Answers

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;
    }    
}
like image 197
Constantiner Avatar answered Sep 18 '22 16:09

Constantiner