I am trying to use Aspect Oriented Programming to execute a simple Fibonacci function and trace all calls to any method apart from the ones in Java and also display the nesting level of them.
Java Code:
package tracing;
public class Test {
static int fib(int n) {
if (n<=1)
return n;
else
return fib(n-1) + fib(n-2);
}
static void report(int n,int r) {
System.out.println("fib("+n+")="+r);
}
public static void main(String[] a) {
report(4,fib(4));
}
}
AspectJ Code:
package tracing;
public aspect Trace {
String prefix = "";
Object around(): call(* *(..)) && !within(java..*) && !within(FigureEditor..*) {
System.out.println(prefix+thisJoinPoint.getSignature());
prefix = ">" + prefix;
Object result = proceed();
prefix = prefix.substring(1);
return result;
}
}
Note: && !within(FigureEditor..*)
is used only to avoid the functions in a class of a different package.
Console Output - Errors:
Exception in thread "main" java.lang.StackOverflowError at
org.aspectj.runtime.internal.AroundClosure.<init>(AroundClosure.java:34)
at tracing.Trace$AjcClosure1.<init>(Trace.aj:1) at
tracing.Trace.ajc$around$tracing_Trace$1$ef88057b(Trace.aj:7) at
tracing.Trace.ajc$around$tracing_Trace$1$ef88057b(Trace.aj:7) at
tracing.Trace.ajc$around$tracing_Trace$1$ef88057b(Trace.aj:7) at
tracing.Trace.ajc$around$tracing_Trace$1$ef88057b(Trace.aj:7) at
tracing.Trace.ajc$around$tracing_Trace$1$ef88057b(Trace.aj:7) at
tracing.Trace.ajc$around$tracing_Trace$1$ef88057b(Trace.aj:7)
Update: The output I want is similar to below:
void Test.main(String [])
>void Test.report(int, int)<br>
>>int Test.fib(int)<br>
>>>int Test.fib(int)<br>
>>>>int Test.fib(int)<br>
>>>>>int Test.fib(int)<br>
>>>>>int Test.fib(int)<br>
>>>>int Test.fib(int)<br>
>>>int Test.fib(int)<br>
>>>>int Test.fib(int)<br>
>>>>int Test.fib(int)<br>
>> void Test.write(String) fib(4)=3