Recipe5.3.Capturing the Object Handling the Exception


Recipe 5.3. Capturing the Object Handling the Exception

Problem

Within the advice triggered by a join point captured using the handler(TypePattern) pointcut, you want to access the object that caught the exception and use it within the corresponding advice.

Solution

Combine the this([Type | Identifier]) pointcut with the handler(TypePattern) pointcut to expose the exception handling object as an identifier on your pointcut that can then be passed to the corresponding advice.

Discussion

Example 5-3 shows how the exception handling MyClass object is passed to the before( ) advice as the myObject identifier on the myExceptionHandlerPointcut pointcut.

Example 5-3. Accessing the object that contained the catch block that handled the MyException exception
public aspect AccessHandlingObject  {    pointcut myExceptionHandlerPointcut(MyClass myObject) :        handler(MyException) &&        this(myObject);    // Advice declaration    before(MyClass myObject) : myExceptionHandlerPointcut(myObject)    {       System.out.println(          "-------------- Aspect Advice Logic ---------------");       System.out.println(          "In the advice picked by " + "myExceptionHandlerPointcut( )");       System.out.println(          "Signature: "             + thisJoinPoint.getStaticPart( ).getSignature( ));       System.out.println(          "Source Line: "             + thisJoinPoint.getStaticPart( ).getSourceLocation( ));              System.out.println("Exception caught by:" + myObject);              System.out.println(          "--------------------------------------------------");    } }

See Also

The handler(TypePattern) pointcut's syntax is shown in Recipe 5.1; Recipe 5.1 also shows some of the wildcard variations that can be used in a TypePattern; the this([Type | Identifier]) pointcut declaration is explained in Recipe 11.1 combining pointcut logic using a logical AND (&&) is shown in Recipe 12.2; Chapter 13 describes the different types of advice available in AspectJ.



AspectJ Cookbook
Aspectj Cookbook
ISBN: 0596006543
EAN: 2147483647
Year: 2006
Pages: 203
Authors: Russ Miles

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net