Bug 274854 - Advice called twice when advised method called from outer method in same class
Summary: Advice called twice when advised method called from outer method in same class
Status: RESOLVED INVALID
Alias: None
Product: AspectJ
Classification: Tools
Component: Compiler (show other bugs)
Version: 1.6.4   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: aspectj inbox CLA
QA Contact:
URL: http://www.jivesoftware.com
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-05-04 12:55 EDT by Scott Hirdes CLA
Modified: 2009-05-04 17:56 EDT (History)
1 user (show)

See Also:


Attachments
Example project to recreate duplicate call to advice (1.25 KB, application/gzip)
2009-05-04 12:57 EDT, Scott Hirdes CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Scott Hirdes CLA 2009-05-04 12:55:39 EDT
See the attached source for example source to recreate the issue.

In the source, I have a class that has a method, "innerMethod," which is advised based upon my own annotation, "Test".  When "innerMethod" is called by another method within the same class, "outerMethod," in the below class, the advise ends up getting called twice even though the outer method is not advised.

The source includes the build.xml file that I used to build the source to reproduce the problem.
Comment 1 Scott Hirdes CLA 2009-05-04 12:57:34 EDT
Created attachment 134268 [details]
Example project to recreate duplicate call to advice
Comment 2 Scott Hirdes CLA 2009-05-04 13:06:24 EDT
Should have had this my initial post.

Here is the output of running the provided example program:

> scott@ipswitch:/media/data/code/aspectjrecreate/target$ java -cp $MYTEST > com.mytest.ExecuteTest
> In the aspect
> In the aspect
> Hello world!

See the "In the aspect" string is output twice.
Comment 3 Andrew Clement CLA 2009-05-04 13:15:46 EDT
Hi.

Because you didn't specify a kind of join point, you got both join points advised for the method invocation.  On -showWeaveInfo we can see that both method-call and method-execution for innerMethod got advised:

>ajc -1.5 -showWeaveInfo *.java *.aj
Join point 'method-call(void com.mytest.ExecuteTest.innerMethod(java.lang.String))' in Type 'com.mytest.ExecuteTest' (ExecuteTest.java:13) advised by before advice from 'com.mytest.TestAspect' (TestAspect.aj:7)

Join point 'method-execution(void com.mytest.ExecuteTest.innerMethod(java.lang.String))' in Type 'com.mytest.ExecuteTest' (ExecuteTest.java:17) advised by before advice from 'com.mytest.TestAspect' (TestAspect.aj:7)


Hence the advise firing twice.  It is invoked at the point where the method is invoked (so in outerMethod()) and then again from the method body (in innerMethod()).  I suspect you just wanted the method-execution join point.  So I would change the pointcut to:

   before() : execution(@com.mytest.Test * *(..)) {

You will also find that including the annotation in the execution() pointcut signature is more performant than using @annotation().
Comment 4 Scott Hirdes CLA 2009-05-04 17:56:52 EDT
Thanks for the resolution to this!