Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] JoinPoint SourceLocation vs. Stack Trace Interigation

It is my understanding that the use of JoinPoint is a reflective operation.  Therefore, I have the choice between two supposed inefficient mechanisms of obtaining the type of class and line number located at the join point, reflective JoinPoint.getSourceLocation() vs generating an exception and interrogating the stack:

    @After("call (net.kedges.Foo+.new(..))")
    public void fooCreatedHere(JoinPoint thisJoinPoint) {
        final SourceLocation sl = thisJoinPoint.getSourceLocation();
        final StringBuilder sb = new StringBuilder(sl.getWithinType().getName());
        sb.append('(').append(sl.getFileName()).append(':').append(sl.getLine()).append(')');
        System.out.println("Foo created here: " + sb.toString());
        System.out.println("Foo created here: " + new Exception().getStackTrace()[1].toString());
    }

Might produce the following in my stout:
Foo created here: net.kedges.Goo(Goo.java:666)
Foo created here: net.kedges.Goo.createFoo(Goo.java:666)

I would gladly sacrifice the method information provided in within the StackTraceElement for my purposes if accessing JoinPoint.SourceLocation was a typically more expeditious operation [, but curious all the same as to why it was omitted from SourceLocation...]  Which operation is supposedly more efficient?


Kind Regards,
Greg Kedge




Back to the top