Community
Participate
Working Groups
Consider the following code snippets: Account.java: public class Account{ private int balance=0; public void credit(int amount){ balance=balance +amount; System.out.println("amount credited"); } } SavingsAccount.java: public class SavingsAccount extends Account{ public void credit(int amount){ super.credit(amount); } public static void main(String args[]){ SavingsAccount sa = new SavingsAccount(); sa.credit(5); } } AspectLogging.java import Account; public aspect AspectLogging { pointcut debit(): execution(void Account.credit(int)); before(): debit() { System.out.println("check balance"); } } after I compile this with ajc: ajc Account.java SavingsAccount.java AspectLogging.java the before advice also gets weaved in the credit method of SavingsAccount.java. I believe it should only be weaved in Account.java unless I change the pointcut definition to look like this: pointcut debit(): execution(void Account+.credit(int));
Thanks for the very clear report with a self-contained test. This makes it easy to understand your question. Signature matching in AspectJ uses an OO notion of substitutability. The method SavingsAccount.credit must also be usable as if it is the method Account.credit. We've designed the signature matching rules to support this. If you want to restrict your matching to a single lexical class body, use: execution(void Account.credit(int)) && within(Account) Your program is behaving correctly according to the language spec.
Jim, Thanks for quickly attending to this. I am confused than what is the difference between execution(void Account.credit(int)); & execution(void Account+.credit(int)); I think it is kind of confusing. I also checked the language semantics & could not find what you wrote below, although it is doing what you wrote below. Appreciate if you can take the confusion out. regards Manoj GUpta