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 org.aspectj.lang.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:
aspect 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 such as the
arguments of the join point:
thisJoinPoint.getArgs()
In addition, it holds an object consisting of all the static information about the join point such as corresponding line number and static signature:
thisJoinPoint.getStaticPart()
If you only need the static information about the join point, you may
access the static part of the join point directly with the special
variable thisJoinPointStaticPart
. Using
thisJoinPointStaticPart
will avoid the run-time
creation of the join point object that may be necessary when using
thisJoinPoint
directly.
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()) }