Recipe 13.2. Accessing the Join Point ContextProblemYou want to access the join point context from within your advice. SolutionUse the thisJoinPoint and thisJoinPointStaticPart variable. DiscussionClasses in Java have a this variable to allow their objects to reference and work with themselves. Aspects are converted by the AspectJ compiler into classes; therefore, the aspects have a this reference. However, an additional join point context can be exposed to advice from the join points that trigger it. AspectJ provides the thisJoinPoint variable to expose this join point context. In addition to thisJoinPoint, the thisJoinPointStaticPart variable is useful if the context that is being accessed can be assessed statically. Example 13-2 shows some of the information that is available from the generic thisJoinPoint variable. Example 13-2. Using the thisJoinPoint variablepublic aspect ThisJoinPointRecipe { /* Specifies calling advice whenever a method matching the following rules gets called: Class Name: MyClass Method Name: foo Method Return Type: void Method Parameters: an int followed by a String */ pointcut callPointCut( ) : call(void MyClass.foo(int, String)); // Advice declaration before( ) : callPointCut( ) && !within(ThisJoinPointRecipe +) { System.out.println( "------------------- Aspect Advice Logic --------------------"); System.out.println( "Exercising the static parts of AspectJ 1.1.1 thisJoinPoint"); System.out.println( "Source Line: " + thisJoinPointStaticPart.getSourceLocation( )); System.out.println( "Join Point Kind: " + thisJoinPointStaticPart.getKind( )); System.out.println( "Simple toString: " + thisJoinPointStaticPart.toString( )); System.out.println( "Simple toShortString: " + thisJoinPointStaticPart.toShortString( )); System.out.println( "Simple toLongString: " + thisJoinPointStaticPart.toLongString( )); System.out.println( "Exercising the join point generic signature of AspectJ 1.1.1 thisJoinPoint"); System.out.println( "Signature: " + thisJoinPointStaticPart.getSignature( )); System.out.println( "Signature name: " + thisJoinPointStaticPart.getSignature( ).getName( )); System.out.println( "Signature declaring type: " + thisJoinPointStaticPart.getSignature( ).getDeclaringType( )); System.out.println( "------------------------------------------------------------"); } // Advice declaration before( ) : callPointCut( ) && !within(ThisJoinPointRecipe +) { System.out.println( "------------------- Aspect Advice Logic --------------------"); System.out.println( "Exercising the dynamic parts of AspectJ 1.1.1 thisJoinPoint"); System.out.println( "Get the this reference: " + thisJoinPoint.getThis( )); System.out.println( "Getting the Target: " + thisJoinPoint.getTarget( )); System.out.println("Join Point Arguments: "); Object[] args = thisJoinPoint.getArgs( ); for (int count = 0; count < args.length; count++) { System.out.println(args[count]); } System.out.println( "------------------------------------------------------------"); } } The code in Example 13-2 produces the following output: ------------------- Aspect Advice Logic -------------------- Exercising the static parts of AspectJ 1.1.1 thisJoinPoint Source Line: MyClass.java:14 Join Point Kind: method-call Simple toString: call(void MyClass.foo(int, String)) Simple toShortString: call(MyClass.foo(..)) Simple toLongString: call(public void MyClass.foo(int, java.lang.String)) Exercising the join point generic signature of AspectJ 1.1.1 thisJoinPoint Signature: void MyClass.foo(int, String) Signature name: foo Signature declaring type: class MyClass ------------------------------------------------------------ ------------------- Aspect Advice Logic -------------------- Exercising the dynamic parts of AspectJ 1.1.1 thisJoinPoint Get the this reference: null Getting the Target: MyClass@d19bc8 Join Point Arguments: 1 Russ Miles ------------------------------------------------------------ The thisJoinPoint variable contains static and dynamic context information about the triggering join point. Static join point context information contains anything that can be decided at compile and weave time, as explained in Chapter 1. Dynamic join point context information can only be populated at runtime because it is dependent on the actual runtime state of the join point context. To keep things as efficient as possible, the static join point information can be accessed by using the thisJoinPoint.getStaticPart() method or, as shown in Example 13-2, by accessing the thisJoinPointStaticPart variable. If a particular advice only uses the getStaticPart() method or thisJoinPointStaticPart, then the AspectJ compiler can perform compilation optimizations to reduce the overhead associated with accessing the join point context.
See AlsoThe call(Signature) pointcut is described in Recipe 4.1; the within(TypePattern) pointcut is described in Recipe 9.1; the Appendix contains a quick reference for the JoinPoint class and its subclasses. |