| <html><head> | 
 |       <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> | 
 |    <title>Advice</title><link rel="stylesheet" href="aspectj-docs.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.44"><link rel="home" href="index.html" title="The AspectJTM Programming Guide"><link rel="up" href="language.html" title="Chapter 2. The AspectJ Language"><link rel="previous" href="language-joinPoints.html" title="Join Points and Pointcuts"><link rel="next" href="language-interType.html" title="Inter-type declarations"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Advice</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="language-joinPoints.html">Prev</a> </td><th width="60%" align="center">Chapter 2. The AspectJ Language</th><td width="20%" align="right"> <a accesskey="n" href="language-interType.html">Next</a></td></tr></table><hr></div><div class="sect1"><a name="language-advice"></a><div class="titlepage"><div><h2 class="title" style="clear: both"><a name="language-advice"></a>Advice</h2></div></div><p> | 
 |       Advice defines pieces of aspect implementation that execute at | 
 |       well-defined points in the execution of the program. Those points can | 
 |       be given either by named pointcuts (like the ones you've seen above) | 
 |       or by anonymous pointcuts. Here is an example of an advice on a named | 
 |       pointcut: | 
 |     </p><pre class="programlisting"> | 
 |   pointcut setter(Point p1, int newval): target(p1) && args(newval) | 
 |                                          (call(void setX(int) || | 
 |                                           call(void setY(int))); | 
 |  | 
 |   before(Point p1, int newval): setter(p1, newval) { | 
 |       System.out.println("About to set something in " + p1 + | 
 |                          " to the new value " + newval); | 
 |   } | 
 | </pre><p> | 
 |       And here is exactly the same example, but using an anonymous | 
 |       pointcut: | 
 |     </p><pre class="programlisting"> | 
 |   before(Point p1, int newval): target(p1) && args(newval) | 
 |                                 (call(void setX(int)) || | 
 |                                  call(void setY(int))) { | 
 |       System.out.println("About to set something in " + p1 + | 
 |                          " to the new value " + newval); | 
 |   } | 
 | </pre><p> | 
 |       Here are examples of the different advice: | 
 |     </p><p> | 
 |       This before advice runs just before the join points picked out by the | 
 |       (anonymous) pointcut: | 
 |     </p><pre class="programlisting"> | 
 |   before(Point p, int x): target(p) && args(x) && call(void setX(int)) { | 
 |       if (!p.assertX(x)) return; | 
 |   } | 
 | </pre><p> | 
 |       This after advice runs just after each join point picked out by the | 
 |       (anonymous) pointcut, regardless of whether it returns normally or throws | 
 |       an exception: | 
 |     </p><pre class="programlisting"> | 
 |   after(Point p, int x): target(p) && args(x) && call(void setX(int)) { | 
 |       if (!p.assertX(x)) throw new PostConditionViolation(); | 
 |   } | 
 | </pre><p> | 
 |       This after returning advice runs just after each join point picked | 
 |       out by the (anonymous) pointcut, but only if it returns normally. | 
 |       The return value can be accessed, and is named <tt>x</tt> | 
 |       here.  After the advice runs, the return value is returned: | 
 |     </p><pre class="programlisting"> | 
 |   after(Point p) returning(int x): target(p) && call(int getX()) { | 
 |       System.out.println("Returning int value " + x + " for p = " + p); | 
 |   } | 
 | </pre><p> | 
 |       This after throwing advice runs just after each join point picked out by | 
 |       the (anonymous) pointcut, but only when it throws an exception of type | 
 |       <tt>Exception</tt>.  Here the exception value can be accessed | 
 |       with the name <tt>e</tt>.  The advice re-raises the exception | 
 |       after it's done: | 
 |     </p><pre class="programlisting"> | 
 |   after() throwing(Exception e): target(Point) && call(void setX(int)) { | 
 |       System.out.println(e); | 
 |   } | 
 | </pre><p> | 
 |       This around advice traps the execution of the join point; it runs | 
 |       <span class="emphasis"><i>instead</i></span> of the join point.  The original action | 
 |       associated with the join point can be invoked through the special | 
 |       <tt>proceed</tt> call: | 
 |     </p><pre class="programlisting"> | 
 | void around(Point p, int x): target(p) | 
 |                           && args(x) | 
 |                           && call(void setX(int)) { | 
 |     if (p.assertX(x)) proceed(p, x); | 
 |     p.releaseResources(); | 
 | } | 
 | </pre></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="language-joinPoints.html">Prev</a> </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right"> <a accesskey="n" href="language-interType.html">Next</a></td></tr><tr><td width="40%" align="left">Join Points and Pointcuts </td><td width="20%" align="center"><a accesskey="u" href="language.html">Up</a></td><td width="40%" align="right"> Inter-type declarations</td></tr></table></div></body></html> |