Bug 51164 - Advice written for point cut defined on super class's method also gets weaved in sub classes, if sub classes overrides the method with exact signature
Summary: Advice written for point cut defined on super class's method also gets weaved...
Status: RESOLVED INVALID
Alias: None
Product: AspectJ
Classification: Tools
Component: Compiler (show other bugs)
Version: 1.1.1   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Jim Hugunin CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-02-04 10:29 EST by Manoj K. Gupta CLA
Modified: 2004-02-05 14:59 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Manoj K. Gupta CLA 2004-02-04 10:29:11 EST
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));
Comment 1 Jim Hugunin CLA 2004-02-04 12:45:37 EST
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.
Comment 2 Manoj K. Gupta CLA 2004-02-05 14:59:16 EST
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