Reputation: 1215
I'm learning Java and somehow I got into AspectJ. I tried to execute this code from tutorial book:
pointcut adviceExecutionPointcut( ) : adviceexecution( );
// Advice declaration
before( ) : adviceExecutionPointcut( )
&& !within(AdviceExecutionRecipe +)
{
System.out.println(
"------------------- Aspect Advice Logic --------------------");
System.out.println("In the advice picked by ExecutionRecipe");
System.out.println(
"Signature: "
+ thisJoinPoint.getStaticPart( ).getSignature( ));
System.out.println(
"Source Line: "
+ thisJoinPoint.getStaticPart( ).getSourceLocation( ));
System.out.println(
"------------------------------------------------------------");
}
}
And it somehow gave me an error like this.
Exception in thread "main" java.lang.StackOverflowError
at helloWorld.ajc$before$helloWorld$2$5e65e204(helloWorld.aj)
at helloWorld.ajc$before$helloWorld$2$5e65e204(helloWorld.aj)
at helloWorld.ajc$before$helloWorld$2$5e65e204(helloWorld.aj)
at helloWorld.ajc$before$helloWorld$2$5e65e204(helloWorld.aj)
at helloWorld.ajc$before$helloWorld$2$5e65e204(helloWorld.aj)
For reference this is the example code and the advice code.
public class myClass {
public void foo(int number, String name){
System.out.println("Inside foo(int, String)");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
myClass myObject = new myClass();
myObject.foo(1, "ican");
}
public aspect helloWorld {
public int a;
long startTime;
pointcut callPointcut(int value, String name) :
call(* myClass.foo(int, String)) && args(value,name);
before(int value, String name) : callPointcut(value,name)
{
startTime = System.nanoTime();
System.out.println("Start Time in ns :" + startTime);
}
Any help would be appreciated. Thanks
Upvotes: 0
Views: 148
Reputation: 2560
I don't see the type declaration for the aspect containing the adviceexecution pointcut? I suspect you dropped that advice block into your helloWorld
aspect. The reason you are getting the recursive stackoverflow is because the advice is applying to itself. The guard !within(AdviceExecutionRecipe +)
is meant to stop the advice applying to itself. However, you can't just change that from AdviceExecutionRecipe
to helloWorld
because then it won't apply to any of your advice. So I would keep that advice block in a separate aspect and call it AdviceExecutionRecipe
. With that in place, it works for me:
$ ajc -1.8 *.java -showWeaveInfo
Join point 'adviceexecution(void helloWorld.ajc$before$helloWorld$1$68d3c671(int, java.lang.String))' in Type 'helloWorld' (helloWorld.java:9) advised by before advice from 'AdviceExecutionRecipe' (AdviceExecutionRecipe.java:5)
Join point 'method-call(void myClass.foo(int, java.lang.String))' in Type 'myClass' (myClass.java:10) advised by before advice from 'helloWorld' (helloWorld.java:9)
$ java myClass
------------------- Aspect Advice Logic --------------------
In the advice picked by ExecutionRecipe
Signature: void helloWorld.before(int, String)
Source Line: helloWorld.java:9
------------------------------------------------------------
Start Time in ns :216368803494701
Inside foo(int, String)
Upvotes: 2