I am trying to write an aspect to catch all methods execution and retrieve the line number where the methods have been invoked. For example, if I have:
public class MyMainClass {
public static void main(String[] args){
MyArray m = new MyArray(5);
m.setValue //...
//...
}
}
I would like to write a pointcut and an advice able to get the line where the method setValue has been called (i.e. line number 4) and not the source code line, where the method has been implemented (accessible with thisJoinPoint.getSourceLocation().getLine()
).
Is there any way to do this? Thanks!
UPDATE: I just found out that this code:
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
System.out.println("[AspectJ]LineNumber: " + trace[trace.length-1].getLineNumber());
prints the last method invocation line number, the problem is that it is not useful when there is method invocation inside a method implementation (because, in that case I should decrease trace elements position, but I don't know how to understand when to do that).