AspectJ provides a special reference variable, thisJoinPoint, that contains reflective information about the current join point for the advice to use. The thisJoinPoint variable can only be used in the context of advice, just like this can only be used in the context of non-static methods and variable initializers. In advice, thisJoinPoint is an object of type JoinPoint.
One way to use it is simply to print it out. Like all Java objects, thisJoinPoint has a toString() method that makes quick-and-dirty tracing easy.
class TraceNonStaticMethods { before(Point p): target(p) && call(* *(..)) { System.out.println("Entering " + thisJoinPoint + " in " + p); } }
The type of thisJoinPoint includes a rich reflective class hierarchy of signatures, and can be used to access both static and dynamic information about join points. If, however, only the static information about the join point (such as the Signature) is desired, a lightweight join-point object is available from the thisJoinPointStaticPart special variable. This object is the same object you would get from
thisJoinPoint.getStaticPart()
The static part of a join point does not include dynamic information, such as the arguments, which can be accessed with
thisJoinPoint.getArgs()
But it has the performance benefit that repeated execution of the code containing thisJoinPointStaticPart (through, for example, separate method calls) will not result in repeated construction of the reflective object.
It is always the case that
thisJoinPointStaticPart == thisJoinPoint.getStaticPart() thisJoinPoint.getKind() == thisJoinPointStaticPart.getKind() thisJoinPoint.getSignature() == thisJoinPointStaticPart.getSignature() thisJoinPoint.getSourceLocation() == thisJoinPointStaticPart.getSourceLocation()
One more reflective variable is available: thisEnclosingJoinPointStaticPart. This, like thisJoinPointStaticPart, only holds the static part of a join point, but it is not the current but the enclosing join point. So, for example, it is possible to print out the calling source location (if available) with
before() : execution (* *(..)) { System.err.println(thisEnclosingJoinPointStaticPart.getSourceLocation()) }