You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

language.xml 47KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. <chapter id="language" xreflabel="The AspectJ Language">
  2. <title>The AspectJ Language</title>
  3. <sect1 id="language-intro">
  4. <title>Introduction</title>
  5. <para>
  6. The previous chapter, <xref linkend="starting" />, was a brief
  7. overview of the AspectJ language. You should read this chapter to
  8. understand AspectJ's syntax and semantics. It covers the same
  9. material as the previous chapter, but more completely and in much
  10. more detail.
  11. </para>
  12. <para>
  13. We will start out by looking at an example aspect that we'll build
  14. out of a pointcut, an introduction, and two pieces of advice. This
  15. example aspect will gives us something concrete to talk about.
  16. </para>
  17. </sect1>
  18. <!-- ============================== -->
  19. <sect1 id="language-anatomy">
  20. <title>The Anatomy of an Aspect</title>
  21. <para>
  22. This lesson explains the parts of AspectJ's aspects. By reading this
  23. lesson you will have an overview of what's in an aspect and you will
  24. be exposed to the new terminology introduced by AspectJ.
  25. </para>
  26. <sect2 id="an-example-aspect" xreflabel="an-example-aspect">
  27. <title>An Example Aspect</title>
  28. <para>
  29. Here's an example of an aspect definition in AspectJ:
  30. </para>
  31. <programlisting><![CDATA[
  32. 1 aspect FaultHandler {
  33. 2
  34. 3 private boolean Server.disabled = false;
  35. 4
  36. 5 private void reportFault() {
  37. 6 System.out.println("Failure! Please fix it.");
  38. 7 }
  39. 8
  40. 9 public static void fixServer(Server s) {
  41. 10 s.disabled = false;
  42. 11 }
  43. 12
  44. 13 pointcut services(Server s): target(s) && call(public * *(..));
  45. 14
  46. 15 before(Server s): services(s) {
  47. 16 if (s.disabled) throw new DisabledException();
  48. 17 }
  49. 18
  50. 19 after(Server s) throwing (FaultException e): services(s) {
  51. 20 s.disabled = true;
  52. 21 reportFault();
  53. 22 }
  54. 23 }
  55. ]]></programlisting>
  56. <para>
  57. The <literal>FaultHandler</literal> consists of one inter-type
  58. field on <literal>Server</literal> (line 03), two methods (lines
  59. 05-07 and 09-11), one pointcut definition (line 13), and two pieces
  60. of advice (lines 15-17 and 19-22).
  61. </para>
  62. <para>
  63. This covers the basics of what aspects can contain. In general,
  64. aspects consist of an association of other program entities,
  65. ordinary variables and methods, pointcut definitions, inter-type declarations,
  66. and advice, where advice may be before, after or around advice. The
  67. remainder of this lesson focuses on those crosscut-related
  68. constructs.
  69. </para>
  70. </sect2>
  71. <sect2 id="pointcuts" xreflabel="pointcuts">
  72. <title>Pointcuts</title>
  73. <para>
  74. AspectJ's pointcut definitions give names to pointcuts. Pointcuts
  75. themselves pick out join points, i.e. interesting points in the
  76. execution of a program. These join points can be method or
  77. constructor invocations and executions, the handling of exceptions,
  78. field assignments and accesses, etc. Take, for example, the
  79. pointcut definition in line 13:
  80. </para>
  81. <programlisting><![CDATA[
  82. pointcut services(Server s): target(s) && call(public * *(..))
  83. ]]></programlisting>
  84. <para>
  85. This pointcut, named <literal>services</literal>, picks out those
  86. points in the execution of the program when
  87. <literal>Server</literal> objects have their public methods called.
  88. It also allows anyone using the <literal>services</literal>
  89. pointcut to access the <literal>Server</literal> object whose
  90. method is being called.
  91. </para>
  92. <para>
  93. The idea behind this pointcut in the
  94. <literal>FaultHandler</literal> aspect is that
  95. fault-handling-related behavior must be triggered on the calls to
  96. public methods. For example, the server may be unable to proceed
  97. with the request because of some fault. The calls of those methods
  98. are, therefore, interesting events for this aspect, in the sense
  99. that certain fault-related things will happen when these events
  100. occur.
  101. </para>
  102. <para>
  103. Part of the context in which the events occur is exposed by the
  104. formal parameters of the pointcut. In this case, that consists of
  105. objects of type <literal>Server</literal>. That formal parameter
  106. is then being used on the right hand side of the declaration in
  107. order to identify which events the pointcut refers to. In this
  108. case, a pointcut picking out join points where a Server is the
  109. target of some operation (target(s)) is being composed
  110. (<literal><![CDATA[&&]]></literal>, meaning and) with a pointcut
  111. picking out call join points (call(...)). The calls are identified
  112. by signatures that can include wild cards. In this case, there are
  113. wild cards in the return type position (first *), in the name
  114. position (second *) and in the argument list position (..); the
  115. only concrete information is given by the qualifier
  116. <literal>public</literal>.
  117. </para>
  118. <para>
  119. Pointcuts pick out arbitrarily large numbers of join points of a
  120. program. But they pick out only a small number of
  121. <emphasis>kinds</emphasis> of join points. Those kinds of join
  122. points correspond to some of the most important concepts in
  123. Java. Here is an incomplete list: method call, method execution,
  124. exception handling, instantiation, constructor execution, and
  125. field access. Each kind of join point can be picked out by its
  126. own specialized pointcut that you will learn about in other parts
  127. of this guide.
  128. </para>
  129. </sect2>
  130. <!-- ============================== -->
  131. <!-- ============================== -->
  132. <sect2 id="advice" xreflabel="advice">
  133. <title>Advice</title>
  134. <para>
  135. A piece of advice brings together a pointcut and a body of code to
  136. define aspect implementation that runs at join points picked out by
  137. the pointcut. For example, the advice in lines 15-17 specifies that
  138. the following piece of code
  139. </para>
  140. <programlisting><![CDATA[
  141. {
  142. if (s.disabled) throw new DisabledException();
  143. }
  144. ]]></programlisting>
  145. <para>
  146. is executed when instances of the <literal>Server</literal> class
  147. have their public methods called, as specified by the pointcut
  148. <literal>services</literal>. More specifically, it runs when those
  149. calls are made, just before the corresponding methods are executed.
  150. </para>
  151. <para>
  152. The advice in lines 19-22 defines another piece of implementation
  153. that is executed on the same pointcut:
  154. </para>
  155. <programlisting><![CDATA[
  156. {
  157. s.disabled = true;
  158. reportFault();
  159. }
  160. ]]></programlisting>
  161. <para>
  162. But this second method executes after those operations throw
  163. exception of type <literal>FaultException</literal>.
  164. </para>
  165. <para>
  166. There are two other variations of after advice: upon successful
  167. return and upon return, either successful or with an exception.
  168. There is also a third kind of advice called around. You will see
  169. those in other parts of this guide.
  170. </para>
  171. </sect2>
  172. </sect1>
  173. <!-- ============================== -->
  174. <sect1 id="language-joinPoints">
  175. <title>Join Points and Pointcuts</title>
  176. <para>
  177. Consider the following Java class:
  178. </para>
  179. <programlisting><![CDATA[
  180. class Point {
  181. private int x, y;
  182. Point(int x, int y) { this.x = x; this.y = y; }
  183. void setX(int x) { this.x = x; }
  184. void setY(int y) { this.y = y; }
  185. int getX() { return x; }
  186. int getY() { return y; }
  187. }
  188. ]]></programlisting>
  189. <para>
  190. In order to get an intuitive understanding of AspectJ's join points
  191. and pointcuts, let's go back to some of the basic principles of
  192. Java. Consider the following a method declaration in class Point:
  193. </para>
  194. <programlisting><![CDATA[
  195. void setX(int x) { this.x = x; }
  196. ]]></programlisting>
  197. <para>
  198. This piece of program says that when method named
  199. <literal>setX</literal> with an <literal>int</literal> argument
  200. called on an object of type <literal>Point</literal>, then the method
  201. body <literal>{ this.x = x; }</literal> is executed. Similarly, the
  202. constructor of the class states that when an object of type
  203. <literal>Point</literal> is instantiated through a constructor with
  204. two <literal>int</literal> arguments, then the constructor body
  205. <literal>{ this.x = x; this.y = y; }</literal> is executed.
  206. </para>
  207. <para>
  208. One pattern that emerges from these descriptions is
  209. <blockquote>
  210. When something happens, then something gets executed.
  211. </blockquote>
  212. In object-oriented programs, there are several kinds of "things that
  213. happen" that are determined by the language. We call these the join
  214. points of Java. Join points consist of things like method calls,
  215. method executions, object instantiations, constructor executions,
  216. field references and handler executions. (See the <xref
  217. linkend="quick" /> for a complete listing.)
  218. </para>
  219. <para>
  220. Pointcuts pick out these join points. For example, the pointcut
  221. </para>
  222. <programlisting><![CDATA[
  223. pointcut setter(): target(Point) &&
  224. (call(void setX(int)) ||
  225. call(void setY(int)));
  226. ]]></programlisting>
  227. <para>
  228. picks out each call to <literal>setX(int)</literal> or
  229. <literal>setY(int)</literal> when called on an instance of
  230. <literal>Point</literal>. Here's another example:
  231. </para>
  232. <programlisting><![CDATA[
  233. pointcut ioHandler(): within(MyClass) && handler(IOException);
  234. ]]></programlisting>
  235. <para>
  236. This pointcut picks out each the join point when exceptions of type
  237. <literal>IOException</literal> are handled inside the code defined by
  238. class <literal>MyClass</literal>.
  239. </para>
  240. <para>
  241. Pointcut definitions consist of a left-hand side and a right-hand side,
  242. separated by a colon. The left-hand side consists of the pointcut name
  243. and the pointcut parameters (i.e. the data available when the events
  244. happen). The right-hand side consists of the pointcut itself.
  245. </para>
  246. <sect2 id="some-example-pointcuts" xreflabel="some-example-pointcuts">
  247. <title>Some Example Pointcuts</title>
  248. <para>
  249. Here are examples of pointcuts picking out
  250. </para>
  251. <variablelist>
  252. <varlistentry>
  253. <term>when a particular method body executes</term>
  254. <listitem>
  255. <para>
  256. <literal>execution(void Point.setX(int))</literal>
  257. </para>
  258. </listitem>
  259. </varlistentry>
  260. <varlistentry>
  261. <term>when a method is called</term>
  262. <listitem>
  263. <para>
  264. <literal>call(void Point.setX(int))</literal>
  265. </para>
  266. </listitem>
  267. </varlistentry>
  268. <varlistentry>
  269. <term>when an exception handler executes</term>
  270. <listitem>
  271. <para>
  272. <literal>handler(ArrayOutOfBoundsException)</literal>
  273. </para>
  274. </listitem>
  275. </varlistentry>
  276. <varlistentry>
  277. <term>
  278. when the object currently executing
  279. (i.e. <literal>this</literal>) is of type
  280. <literal>SomeType</literal>
  281. </term>
  282. <listitem>
  283. <para>
  284. <literal>this(SomeType)</literal>
  285. </para>
  286. </listitem>
  287. </varlistentry>
  288. <varlistentry>
  289. <term>
  290. when the target object is of type <literal>SomeType</literal>
  291. </term>
  292. <listitem>
  293. <para>
  294. <literal>target(SomeType)</literal>
  295. </para>
  296. </listitem>
  297. </varlistentry>
  298. <varlistentry>
  299. <term>
  300. when the executing code belongs to
  301. class <literal>MyClass</literal>
  302. </term>
  303. <listitem>
  304. <para>
  305. <literal>within(MyClass)</literal>
  306. </para>
  307. </listitem>
  308. </varlistentry>
  309. <varlistentry>
  310. <term>
  311. when the join point is in the control flow of a call to a
  312. <literal>Test</literal>'s no-argument <literal>main</literal>
  313. method
  314. </term>
  315. <listitem>
  316. <para>
  317. <literal>cflow(call(void Test.main()))</literal>
  318. </para>
  319. </listitem>
  320. </varlistentry>
  321. </variablelist>
  322. <para>
  323. Pointcuts compose through the operations <literal>or</literal>
  324. ("<literal>||</literal>"), <literal>and</literal>
  325. ("<literal><![CDATA[&&]]></literal>") and <literal>not</literal>
  326. ("<literal>!</literal>").
  327. </para>
  328. <itemizedlist>
  329. <listitem>
  330. <para>
  331. It is possible to use wildcards. So
  332. <orderedlist>
  333. <listitem>
  334. <para>
  335. <literal>execution(* *(..))</literal>
  336. </para>
  337. </listitem>
  338. <listitem>
  339. <para>
  340. <literal>call(* set(..))</literal>
  341. </para>
  342. </listitem>
  343. </orderedlist>
  344. means (1) the execution of any method regardless of return or
  345. parameter types, and (2) the call to any method named
  346. <literal>set</literal> regardless of return or parameter types
  347. -- in case of overloading there may be more than one such
  348. <literal>set</literal> method; this pointcut picks out calls to
  349. all of them.
  350. </para>
  351. </listitem>
  352. <listitem>
  353. <para>
  354. You can select elements based on types. For example,
  355. <orderedlist>
  356. <listitem>
  357. <para>
  358. <literal>execution(int *())</literal>
  359. </para>
  360. </listitem>
  361. <listitem>
  362. <para>
  363. <literal>call(* setY(long))</literal>
  364. </para>
  365. </listitem>
  366. <listitem>
  367. <para>
  368. <literal>call(* Point.setY(int))</literal>
  369. </para>
  370. </listitem>
  371. <listitem>
  372. <para>
  373. <literal>call(*.new(int, int))</literal>
  374. </para>
  375. </listitem>
  376. </orderedlist>
  377. means (1) the execution of any method with no parameters that
  378. returns an <literal>int</literal>, (2) the call to any
  379. <literal>setY</literal> method that takes a
  380. <literal>long</literal> as an argument, regardless of return
  381. type or declaring type, (3) the call to any of
  382. <literal>Point</literal>'s <literal>setY</literal> methods that
  383. take an <literal>int</literal> as an argument, regardless of
  384. return type, and (4) the call to any classes' constructor, so
  385. long as it takes exactly two <literal>int</literal>s as
  386. arguments.
  387. </para>
  388. </listitem>
  389. <listitem>
  390. <para>
  391. You can compose pointcuts. For example,
  392. <orderedlist>
  393. <listitem>
  394. <para>
  395. <literal>target(Point) <![CDATA[&&]]> call(int *())</literal>
  396. </para>
  397. </listitem>
  398. <listitem>
  399. <para>
  400. <literal>call(* *(..)) <![CDATA[&&]]> (within(Line) || within(Point))</literal>
  401. </para>
  402. </listitem>
  403. <listitem>
  404. <para>
  405. <literal>within(*) <![CDATA[&&]]> execution(*.new(int))</literal>
  406. </para>
  407. </listitem>
  408. <listitem>
  409. <para>
  410. <literal>
  411. !this(Point) <![CDATA[&&]]> call(int *(..))
  412. </literal>
  413. </para>
  414. </listitem>
  415. </orderedlist>
  416. means (1) any call to an <literal>int</literal> method with no
  417. arguments on an instance of <literal>Point</literal>,
  418. regardless of its name, (2) any call to any method where the
  419. call is made from the code in <literal>Point</literal>'s or
  420. <literal>Line</literal>'s type declaration, (3) the execution of
  421. any constructor taking exactly one <literal>int</literal>
  422. argument, regardless of where the call is made from, and
  423. (4) any method call to an <literal>int</literal> method when
  424. the executing object is any type except <literal>Point</literal>.
  425. </para>
  426. </listitem>
  427. <listitem>
  428. <para>
  429. You can select methods and constructors based on their
  430. modifiers and on negations of modifiers. For example, you can
  431. say:
  432. <orderedlist>
  433. <listitem>
  434. <para>
  435. <literal>call(public * *(..))</literal>
  436. </para>
  437. </listitem>
  438. <listitem>
  439. <para>
  440. <literal>execution(!static * *(..))</literal>
  441. </para>
  442. </listitem>
  443. <listitem>
  444. <para>
  445. <literal> execution(public !static * *(..))</literal>
  446. </para>
  447. </listitem>
  448. </orderedlist>
  449. which means (1) any call to a public method, (2) any
  450. execution of a non-static method, and (3) any execution of a
  451. public, non-static method.
  452. </para>
  453. </listitem>
  454. <listitem>
  455. <para>
  456. Pointcuts can also deal with interfaces. For example, given the
  457. interface </para>
  458. <programlisting><![CDATA[
  459. interface MyInterface { ... }
  460. ]]></programlisting>
  461. <para>
  462. the pointcut <literal>call(* MyInterface.*(..))</literal> picks
  463. out any call to a method in <literal>MyInterface</literal>'s
  464. signature -- that is, any method defined by
  465. <literal>MyInterface</literal> or inherited by one of its a
  466. supertypes.
  467. </para>
  468. </listitem>
  469. </itemizedlist>
  470. </sect2>
  471. <sect2 id="call-vs-execution" xreflabel="call-vs-execution">
  472. <title>call vs. execution</title>
  473. <para>
  474. When methods and constructors run, there are two interesting times
  475. associated with them. That is when they are called, and when they
  476. actually execute.
  477. </para>
  478. <para>
  479. AspectJ exposes these times as call and execution join points,
  480. respectively, and allows them to be picked out specifically by
  481. <literal>call</literal> and <literal>execution</literal> pointcuts.
  482. </para>
  483. <para>
  484. So what's the difference between these join points? Well, there are a
  485. number of differences:
  486. </para>
  487. <para>
  488. Firstly, the lexical pointcut declarations
  489. <literal>within</literal> and <literal>withincode</literal> match
  490. differently. At a call join point, the enclosing code is that of
  491. the call site. This means that <literal>call(void m())
  492. <![CDATA[&&]]> withincode(void m())</literal> will only capture
  493. directly recursive calls, for example. At an execution join point,
  494. however, the program is already executing the method, so the
  495. enclosing code is the method itself: <literal>execution(void m())
  496. <![CDATA[&&]]> withincode(void m())</literal> is the same as
  497. <literal>execution(void m())</literal>.
  498. </para>
  499. <para>
  500. Secondly, the call join point does not capture super calls to
  501. non-static methods. This is because such super calls are different in
  502. Java, since they don't behave via dynamic dispatch like other calls to
  503. non-static methods.
  504. </para>
  505. <para>
  506. The rule of thumb is that if you want to pick a join point that
  507. runs when an actual piece of code runs (as is often the case for
  508. tracing), use <literal>execution</literal>, but if you want to pick
  509. one that runs when a particular <emphasis>signature</emphasis> is
  510. called (as is often the case for production aspects), use
  511. <literal>call</literal>.
  512. </para>
  513. </sect2>
  514. <!-- ============================== -->
  515. <sect2 id="pointcut-composition" xreflabel="pointcut-composition">
  516. <title>Pointcut composition</title>
  517. <para>
  518. Pointcuts are put together with the operators and (spelled
  519. <literal>&amp;&amp;</literal>), or (spelled <literal>||</literal>),
  520. and not (spelled <literal>!</literal>). This allows the creation
  521. of very powerful pointcuts from the simple building blocks of
  522. primitive pointcuts. This composition can be somewhat confusing
  523. when used with primitive pointcuts like <literal>cflow</literal>
  524. and <literal>cflowbelow</literal>. Here's an example:
  525. </para>
  526. <para>
  527. <literal>cflow(<replaceable>P</replaceable>)</literal> picks out
  528. each join point in the control flow of the join points picked out
  529. by <replaceable>P</replaceable>. So, pictorially:
  530. </para>
  531. <programlisting>
  532. P ---------------------
  533. \
  534. \ cflow of P
  535. \
  536. </programlisting>
  537. <para>
  538. What does <literal>cflow(<replaceable>P</replaceable>) &amp;&amp;
  539. cflow(<replaceable>Q</replaceable>)</literal> pick out? Well, it
  540. picks out each join point that is in both the control flow of
  541. <replaceable>P</replaceable> and in the control flow of
  542. <replaceable>Q</replaceable>. So...
  543. </para>
  544. <programlisting>
  545. P ---------------------
  546. \
  547. \ cflow of P
  548. \
  549. \
  550. \
  551. Q -------------\-------
  552. \ \
  553. \ cflow of Q \ cflow(P) &amp;&amp; cflow(Q)
  554. \ \
  555. </programlisting>
  556. <para>
  557. Note that <replaceable>P</replaceable> and
  558. <replaceable>Q</replaceable> might not have any join points in
  559. common... but their control flows might have join points in common.
  560. </para>
  561. <para>
  562. But what does <literal>cflow(<replaceable>P</replaceable>
  563. &amp;&amp; <replaceable>Q</replaceable>)</literal> mean? Well, it
  564. means the control flow of those join points that are both picked
  565. out by <replaceable>P</replaceable> and picked out by
  566. <replaceable>Q</replaceable>.
  567. </para>
  568. <programlisting>
  569. P &amp;&amp; Q -------------------
  570. \
  571. \ cflow of (P &amp;&amp; Q)
  572. \
  573. </programlisting>
  574. <para>
  575. and if there are <emphasis>no</emphasis> join points that are both
  576. picked by <replaceable>P</replaceable> and picked out by
  577. <replaceable>Q</replaceable>, then there's no chance that there are
  578. any join points in the control flow of
  579. <literal>(<replaceable>P</replaceable> &amp;&amp;
  580. <replaceable>Q</replaceable>)</literal>.
  581. </para>
  582. <para>
  583. Here's some code that expresses this.
  584. </para>
  585. <programlisting><![CDATA[
  586. public class Test {
  587. public static void main(String[] args) {
  588. foo();
  589. }
  590. static void foo() {
  591. goo();
  592. }
  593. static void goo() {
  594. System.out.println("hi");
  595. }
  596. }
  597. aspect A {
  598. pointcut fooPC(): execution(void Test.foo());
  599. pointcut gooPC(): execution(void Test.goo());
  600. pointcut printPC(): call(void java.io.PrintStream.println(String));
  601. before(): cflow(fooPC()) && cflow(gooPC()) && printPC() && !within(A) {
  602. System.out.println("should occur");
  603. }
  604. before(): cflow(fooPC() && gooPC()) && printPC() && !within(A) {
  605. System.out.println("should not occur");
  606. }
  607. }
  608. ]]></programlisting>
  609. <para>
  610. The <literal>!within(<replaceable>A</replaceable>)</literal>
  611. pointcut above is required to avoid the <literal>printPC</literal>
  612. pointcut applying to the <literal>System.out.println</literal>
  613. call in the advice body. If this was not present a recursive call
  614. would result as the pointcut would apply to its own advice.
  615. (See <xref linkend="pitfalls-infiniteLoops"/> for more details.)
  616. </para>
  617. </sect2>
  618. <!-- ============================== -->
  619. <sect2 id="pointcut-parameters" xreflabel="pointcut-parameters">
  620. <title>Pointcut Parameters</title>
  621. <para>
  622. Consider again the first pointcut definition in this chapter:
  623. </para>
  624. <programlisting><![CDATA[
  625. pointcut setter(): target(Point) &&
  626. (call(void setX(int)) ||
  627. call(void setY(int)));
  628. ]]></programlisting>
  629. <para>
  630. As we've seen, this pointcut picks out each call to
  631. <literal>setX(int)</literal> or <literal>setY(int)</literal>
  632. methods where the target is an instance of
  633. <literal>Point</literal>. The pointcut is given the name
  634. <literal>setters</literal> and no parameters on the left-hand
  635. side. An empty parameter list means that none of the context from
  636. the join points is published from this pointcut. But consider
  637. another version of version of this pointcut definition:
  638. </para>
  639. <programlisting><![CDATA[
  640. pointcut setter(Point p): target(p) &&
  641. (call(void setX(int)) ||
  642. call(void setY(int)));
  643. ]]></programlisting>
  644. <para>
  645. This version picks out exactly the same join points. But in this
  646. version, the pointcut has one parameter of type
  647. <literal>Point</literal>. This means that any advice that uses this
  648. pointcut has access to a <literal>Point</literal> from each join
  649. point picked out by the pointcut. Inside the pointcut definition
  650. this <literal>Point</literal> is named <literal>p</literal> is
  651. available, and according to the right-hand side of the definition,
  652. that <literal>Point p</literal> comes from the
  653. <literal>target</literal> of each matched join point.
  654. </para>
  655. <para>
  656. Here's another example that illustrates the flexible mechanism for
  657. defining pointcut parameters:
  658. </para>
  659. <programlisting><![CDATA[
  660. pointcut testEquality(Point p): target(Point) &&
  661. args(p) &&
  662. call(boolean equals(Object));
  663. ]]></programlisting>
  664. <para>
  665. This pointcut also has a parameter of type
  666. <literal>Point</literal>. Similar to the
  667. <literal>setters</literal> pointcut, this means that anyone using
  668. this pointcut has access to a <literal>Point</literal> from each
  669. join point. But in this case, looking at the right-hand side we
  670. find that the object named in the parameters is not the target
  671. <literal>Point</literal> object that receives the call; it's the
  672. argument (also of type <literal>Point</literal>) passed to the
  673. <literal>equals</literal> method when some other
  674. <literal>Point</literal> is the target. If we wanted access to both
  675. <literal>Point</literal>s, then the pointcut definition that would
  676. expose target <literal>Point p1</literal> and argument
  677. <literal>Point p2</literal> would be
  678. </para>
  679. <programlisting><![CDATA[
  680. pointcut testEquality(Point p1, Point p2): target(p1) &&
  681. args(p2) &&
  682. call(boolean equals(Object));
  683. ]]></programlisting>
  684. <para>
  685. Let's look at another variation of the <literal>setters</literal> pointcut:
  686. </para>
  687. <programlisting><![CDATA[
  688. pointcut setter(Point p, int newval): target(p) &&
  689. args(newval) &&
  690. (call(void setX(int)) ||
  691. call(void setY(int)));
  692. ]]></programlisting>
  693. <para>
  694. In this case, a <literal>Point</literal> object and an
  695. <literal>int</literal> value are exposed by the named
  696. pointcut. Looking at the the right-hand side of the definition, we
  697. find that the <literal>Point</literal> object is the target object,
  698. and the <literal>int</literal> value is the called method's
  699. argument.
  700. </para>
  701. <para>
  702. The use of pointcut parameters is relatively flexible. The most
  703. important rule is that all the pointcut parameters must be bound at
  704. every join point picked out by the pointcut. So, for example, the
  705. following pointcut definition will result in a compilation error:
  706. <programlisting><![CDATA[
  707. pointcut badPointcut(Point p1, Point p2):
  708. (target(p1) && call(void setX(int))) ||
  709. (target(p2) && call(void setY(int)));
  710. ]]></programlisting>
  711. because <literal>p1</literal> is only bound when calling
  712. <literal>setX</literal>, and <literal>p2</literal> is only bound
  713. when calling <literal>setY</literal>, but the pointcut picks out
  714. all of these join points and tries to bind both
  715. <literal>p1</literal> and <literal>p2</literal>.
  716. </para>
  717. </sect2>
  718. <!-- ============================== -->
  719. <sect2 id="example" xreflabel="example">
  720. <title>Example: <literal>HandleLiveness</literal></title>
  721. <para>
  722. The example below consists of two object classes (plus an exception
  723. class) and one aspect. Handle objects delegate their public,
  724. non-static operations to their <literal>Partner</literal>
  725. objects. The aspect <literal>HandleLiveness</literal> ensures that,
  726. before the delegations, the partner exists and is alive, or else it
  727. throws an exception.
  728. </para>
  729. <programlisting><![CDATA[
  730. class Handle {
  731. Partner partner = new Partner();
  732. public void foo() { partner.foo(); }
  733. public void bar(int x) { partner.bar(x); }
  734. public static void main(String[] args) {
  735. Handle h1 = new Handle();
  736. h1.foo();
  737. h1.bar(2);
  738. }
  739. }
  740. class Partner {
  741. boolean isAlive() { return true; }
  742. void foo() { System.out.println("foo"); }
  743. void bar(int x) { System.out.println("bar " + x); }
  744. }
  745. aspect HandleLiveness {
  746. before(Handle handle): target(handle) && call(public * *(..)) {
  747. if ( handle.partner == null || !handle.partner.isAlive() ) {
  748. throw new DeadPartnerException();
  749. }
  750. }
  751. }
  752. class DeadPartnerException extends RuntimeException {}
  753. ]]></programlisting>
  754. </sect2>
  755. <sect2 id="pointcut-best-practice" xreflabel="pointcut-best-practice">
  756. <title>Writing good pointcuts</title>
  757. <para>
  758. During compilation, AspectJ processes pointcuts in order to try and optimize matching performance. Examining code and determining
  759. if each join point matches (statically or dynamically) a given pointcut is a costly process.
  760. (A dynamic match means the match cannot be fully determined from static analysis and a test will be placed in the code
  761. to determine if there is an actual match when the code is running).
  762. On first encountering a pointcut declaration, AspectJ will rewrite it into an optimal form for the matching process.
  763. What does this mean? Basically pointcuts are rewritten in DNF (Disjunctive Normal Form) and the components of the pointcut
  764. are sorted such that those components that are cheaper to evaluate are checked first. This means users do not have to worry
  765. about understanding the performance of various pointcut designators and may supply them in any order in their
  766. pointcut declarations.
  767. </para>
  768. <para>
  769. However, AspectJ can only work with what it is told, and for optimal performance of matching the user should think
  770. about what they are trying to achieve and narrow the search space for matches as much as they can in the definition.
  771. Basically there are three kinds of pointcut designator: kinded, scoping and context:
  772. </para>
  773. <itemizedlist>
  774. <listitem>
  775. Kinded designators are those which select a particular kind of join point. For example: execution, get, set, call, handler
  776. </listitem>
  777. <listitem>
  778. Scoping designators are those which select a group of join points of interest (of probably many kinds). For example: within, withincode
  779. </listitem>
  780. <listitem>
  781. Contextual designators are those that match (and optionally bind) based on context. For example: this, target, @annotation
  782. </listitem>
  783. </itemizedlist>
  784. <para>
  785. A well written pointcut should
  786. try and include at least the first two types (kinded and scoping), whilst the contextual designators may be included if wishing to
  787. match based on join point context, or bind that context for use in the advice. Supplying either just a kinded designator or
  788. just a contextual designator will work but could affect weaving performance (time and memory used)
  789. due to all the extra processing and analysis.
  790. Scoping designators are very fast to match, they can very quickly dismiss groups of join points that should not be further
  791. processed - that is why a good pointcut should always include one if possible.
  792. </para>
  793. </sect2>
  794. </sect1>
  795. <!-- ============================== -->
  796. <sect1 id="language-advice">
  797. <title>Advice</title>
  798. <para>
  799. Advice defines pieces of aspect implementation that execute at
  800. well-defined points in the execution of the program. Those points can
  801. be given either by named pointcuts (like the ones you've seen above)
  802. or by anonymous pointcuts. Here is an example of an advice on a named
  803. pointcut:
  804. </para>
  805. <programlisting><![CDATA[
  806. pointcut setter(Point p1, int newval): target(p1) && args(newval)
  807. (call(void setX(int) ||
  808. call(void setY(int)));
  809. before(Point p1, int newval): setter(p1, newval) {
  810. System.out.println("About to set something in " + p1 +
  811. " to the new value " + newval);
  812. }
  813. ]]></programlisting>
  814. <para>
  815. And here is exactly the same example, but using an anonymous
  816. pointcut:
  817. </para>
  818. <programlisting><![CDATA[
  819. before(Point p1, int newval): target(p1) && args(newval)
  820. (call(void setX(int)) ||
  821. call(void setY(int))) {
  822. System.out.println("About to set something in " + p1 +
  823. " to the new value " + newval);
  824. }
  825. ]]></programlisting>
  826. <para>
  827. Here are examples of the different advice:
  828. </para>
  829. <para>
  830. This before advice runs just before the join points picked out by the
  831. (anonymous) pointcut:
  832. </para>
  833. <programlisting><![CDATA[
  834. before(Point p, int x): target(p) && args(x) && call(void setX(int)) {
  835. if (!p.assertX(x)) return;
  836. }
  837. ]]></programlisting>
  838. <para>
  839. This after advice runs just after each join point picked out by the
  840. (anonymous) pointcut, regardless of whether it returns normally or throws
  841. an exception:
  842. </para>
  843. <programlisting><![CDATA[
  844. after(Point p, int x): target(p) && args(x) && call(void setX(int)) {
  845. if (!p.assertX(x)) throw new PostConditionViolation();
  846. }
  847. ]]></programlisting>
  848. <para>
  849. This after returning advice runs just after each join point picked
  850. out by the (anonymous) pointcut, but only if it returns normally.
  851. The return value can be accessed, and is named <literal>x</literal>
  852. here. After the advice runs, the return value is returned:
  853. </para>
  854. <programlisting><![CDATA[
  855. after(Point p) returning(int x): target(p) && call(int getX()) {
  856. System.out.println("Returning int value " + x + " for p = " + p);
  857. }
  858. ]]></programlisting>
  859. <para>
  860. This after throwing advice runs just after each join point picked out by
  861. the (anonymous) pointcut, but only when it throws an exception of type
  862. <literal>Exception</literal>. Here the exception value can be accessed
  863. with the name <literal>e</literal>. The advice re-raises the exception
  864. after it's done:
  865. </para>
  866. <programlisting><![CDATA[
  867. after() throwing(Exception e): target(Point) && call(void setX(int)) {
  868. System.out.println(e);
  869. }
  870. ]]></programlisting>
  871. <para>
  872. This around advice traps the execution of the join point; it runs
  873. <emphasis>instead</emphasis> of the join point. The original action
  874. associated with the join point can be invoked through the special
  875. <literal>proceed</literal> call:
  876. </para>
  877. <programlisting><![CDATA[
  878. void around(Point p, int x): target(p)
  879. && args(x)
  880. && call(void setX(int)) {
  881. if (p.assertX(x)) proceed(p, x);
  882. p.releaseResources();
  883. }
  884. ]]></programlisting>
  885. </sect1>
  886. <!-- ============================== -->
  887. <sect1 id="language-interType">
  888. <title>Inter-type declarations</title>
  889. <para>
  890. Aspects can declare members (fields, methods, and constructors) that
  891. are owned by other types. These are called inter-type members.
  892. Aspects can also declare that other types implement new interfaces or
  893. extend a new class. Here are examples of some such inter-type
  894. declarations:
  895. </para>
  896. <para>
  897. This declares that each <literal>Server</literal> has a
  898. <literal>boolean</literal> field named <literal>disabled</literal>,
  899. initialized to <literal>false</literal>:
  900. <programlisting><![CDATA[
  901. private boolean Server.disabled = false;
  902. ]]></programlisting>
  903. It is declared <literal>private</literal>, which means that it is
  904. private <emphasis>to the aspect</emphasis>: only code in the aspect
  905. can see the field. And even if <literal>Server</literal> has
  906. another private field named <literal>disabled</literal> (declared in
  907. <literal>Server</literal> or in another aspect) there won't be a name
  908. collision, since no reference to <literal>disabled</literal> will be
  909. ambiguous.
  910. </para>
  911. <para>
  912. This declares that each <literal>Point</literal> has an
  913. <literal>int</literal> method named <literal>getX</literal> with no
  914. arguments that returns whatever <literal>this.x</literal> is:
  915. <programlisting><![CDATA[
  916. public int Point.getX() { return this.x; }
  917. ]]></programlisting>
  918. Inside the body, <literal>this</literal> is the
  919. <literal>Point</literal> object currently executing. Because the
  920. method is publically declared any code can call it, but if there is
  921. some other <literal>Point.getX()</literal> declared there will be a
  922. compile-time conflict.
  923. </para>
  924. <para>
  925. This publically declares a two-argument constructor for
  926. <literal>Point</literal>:
  927. <programlisting><![CDATA[
  928. public Point.new(int x, int y) { this.x = x; this.y = y; }
  929. ]]></programlisting>
  930. </para>
  931. <para>
  932. This publicly declares that each <literal>Point</literal> has an
  933. <literal>int</literal> field named <literal>x</literal>, initialized
  934. to zero:
  935. <programlisting><![CDATA[
  936. public int Point.x = 0;
  937. ]]></programlisting>
  938. Because this is publically declared, it is an error if
  939. <literal>Point</literal> already has a field named
  940. <literal>x</literal> (defined by <literal>Point</literal> or by
  941. another aspect).
  942. </para>
  943. <para>
  944. This declares that the <literal>Point</literal> class implements the
  945. <literal>Comparable</literal> interface:
  946. <programlisting><![CDATA[
  947. declare parents: Point implements Comparable;
  948. ]]></programlisting>
  949. Of course, this will be an error unless <literal>Point</literal>
  950. defines the methods required by <literal>Comparable</literal>.
  951. </para>
  952. <para>
  953. This declares that the <literal>Point</literal> class extends the
  954. <literal>GeometricObject</literal> class.
  955. <programlisting><![CDATA[
  956. declare parents: Point extends GeometricObject;
  957. ]]></programlisting>
  958. </para>
  959. <para>
  960. An aspect can have several inter-type declarations. For example, the
  961. following declarations
  962. <programlisting><![CDATA[
  963. public String Point.name;
  964. public void Point.setName(String name) { this.name = name; }
  965. ]]></programlisting>
  966. publicly declare that Point has both a String field
  967. <literal>name</literal> and a <literal>void</literal> method
  968. <literal>setName(String)</literal> (which refers to the
  969. <literal>name</literal> field declared by the aspect).
  970. </para>
  971. <para>
  972. An inter-type member can only have one target type, but often you may
  973. wish to declare the same member on more than one type. This can be
  974. done by using an inter-type member in combination with a private
  975. interface:
  976. <programlisting><![CDATA[
  977. aspect A {
  978. private interface HasName {}
  979. declare parents: (Point || Line || Square) implements HasName;
  980. private String HasName.name;
  981. public String HasName.getName() { return name; }
  982. }
  983. ]]></programlisting>
  984. This declares a marker interface <literal>HasName</literal>, and also declares that any
  985. type that is either <literal>Point</literal>,
  986. <literal>Line</literal>, or <literal>Square</literal> implements that
  987. interface. It also privately declares that all <literal>HasName</literal>
  988. object have a <literal>String</literal> field called
  989. <literal>name</literal>, and publically declares that all
  990. <literal>HasName</literal> objects have a <literal>String</literal>
  991. method <literal>getName()</literal> (which refers to the privately
  992. declared <literal>name</literal> field).
  993. </para>
  994. <para>
  995. As you can see from the above example, an aspect can declare that
  996. interfaces have fields and methods, even non-constant fields and
  997. methods with bodies.
  998. </para>
  999. <!-- ============================== -->
  1000. <sect2 id="inter-type-scope" xreflabel="inter-type-scope">
  1001. <title>Inter-type Scope</title>
  1002. <para>
  1003. AspectJ allows private and package-protected (default) inter-type declarations in
  1004. addition to public inter-type declarations. Private means private in
  1005. relation to the aspect, not necessarily the target type. So, if an
  1006. aspect makes a private inter-type declaration of a field
  1007. <programlisting><![CDATA[
  1008. private int Foo.x;
  1009. ]]></programlisting>
  1010. Then code in the aspect can refer to <literal>Foo</literal>'s
  1011. <literal>x</literal> field, but nobody else can. Similarly, if an
  1012. aspect makes a package-protected introduction,
  1013. </para>
  1014. <programlisting><![CDATA[
  1015. int Foo.x;
  1016. ]]></programlisting>
  1017. <para>
  1018. then everything in the aspect's package (which may or may not be
  1019. <literal>Foo</literal>'s package) can access <literal>x</literal>.
  1020. </para>
  1021. </sect2>
  1022. <!-- ============================== -->
  1023. <sect2 id="example-pointassertions" xreflabel="example-pointassertions">
  1024. <title>Example: <literal>PointAssertions</literal></title>
  1025. <para>
  1026. The example below consists of one class and one aspect. The aspect
  1027. privately declares the assertion methods of
  1028. <literal>Point</literal>, <literal>assertX</literal> and
  1029. <literal>assertY</literal>. It also guards calls to
  1030. <literal>setX</literal> and <literal>setY</literal> with calls to
  1031. these assertion methods. The assertion methods are declared
  1032. privately because other parts of the program (including the code in
  1033. <literal>Point</literal>) have no business accessing the assert
  1034. methods. Only the code inside of the aspect can call those
  1035. methods.
  1036. </para>
  1037. <programlisting><![CDATA[
  1038. class Point {
  1039. int x, y;
  1040. public void setX(int x) { this.x = x; }
  1041. public void setY(int y) { this.y = y; }
  1042. public static void main(String[] args) {
  1043. Point p = new Point();
  1044. p.setX(3); p.setY(333);
  1045. }
  1046. }
  1047. aspect PointAssertions {
  1048. private boolean Point.assertX(int x) {
  1049. return (x <= 100 && x >= 0);
  1050. }
  1051. private boolean Point.assertY(int y) {
  1052. return (y <= 100 && y >= 0);
  1053. }
  1054. before(Point p, int x): target(p) && args(x) && call(void setX(int)) {
  1055. if (!p.assertX(x)) {
  1056. System.out.println("Illegal value for x"); return;
  1057. }
  1058. }
  1059. before(Point p, int y): target(p) && args(y) && call(void setY(int)) {
  1060. if (!p.assertY(y)) {
  1061. System.out.println("Illegal value for y"); return;
  1062. }
  1063. }
  1064. }
  1065. ]]></programlisting>
  1066. </sect2>
  1067. </sect1>
  1068. <!-- ================================================== -->
  1069. <sect1 id="language-thisJoinPoint">
  1070. <title>thisJoinPoint</title>
  1071. <para>
  1072. AspectJ provides a special reference variable,
  1073. <literal>thisJoinPoint</literal>, that contains reflective
  1074. information about the current join point for the advice to use. The
  1075. <literal>thisJoinPoint</literal> variable can only be used in the
  1076. context of advice, just like <literal>this</literal> can only be used
  1077. in the context of non-static methods and variable initializers. In
  1078. advice, <literal>thisJoinPoint</literal> is an object of type <ulink
  1079. url="../api/org/aspectj/lang/JoinPoint.html"><literal>org.aspectj.lang.JoinPoint</literal></ulink>.
  1080. </para>
  1081. <para>
  1082. One way to use it is simply to print it out. Like all Java objects,
  1083. <literal>thisJoinPoint</literal> has a <literal>toString()</literal>
  1084. method that makes quick-and-dirty tracing easy:
  1085. </para>
  1086. <programlisting><![CDATA[
  1087. aspect TraceNonStaticMethods {
  1088. before(Point p): target(p) && call(* *(..)) {
  1089. System.out.println("Entering " + thisJoinPoint + " in " + p);
  1090. }
  1091. }
  1092. ]]></programlisting>
  1093. <para>
  1094. The type of <literal>thisJoinPoint</literal> includes a rich
  1095. reflective class hierarchy of signatures, and can be used to access
  1096. both static and dynamic information about join points such as the
  1097. arguments of the join point:
  1098. <programlisting><![CDATA[
  1099. thisJoinPoint.getArgs()
  1100. ]]></programlisting>
  1101. In addition, it holds an object consisting of all the static
  1102. information about the join point such as corresponding line number
  1103. and static signature:
  1104. <programlisting><![CDATA[
  1105. thisJoinPoint.getStaticPart()
  1106. ]]></programlisting>
  1107. If you only need the static information about the join point, you may
  1108. access the static part of the join point directly with the special
  1109. variable <literal>thisJoinPointStaticPart</literal>. Using
  1110. <literal>thisJoinPointStaticPart</literal> will avoid the run-time
  1111. creation of the join point object that may be necessary when using
  1112. <literal>thisJoinPoint</literal> directly.
  1113. </para>
  1114. <para>It is always the case that
  1115. </para>
  1116. <programlisting><![CDATA[
  1117. thisJoinPointStaticPart == thisJoinPoint.getStaticPart()
  1118. thisJoinPoint.getKind() == thisJoinPointStaticPart.getKind()
  1119. thisJoinPoint.getSignature() == thisJoinPointStaticPart.getSignature()
  1120. thisJoinPoint.getSourceLocation() == thisJoinPointStaticPart.getSourceLocation()
  1121. ]]></programlisting>
  1122. <para>
  1123. One more reflective variable is available:
  1124. <literal>thisEnclosingJoinPointStaticPart</literal>. This, like
  1125. <literal>thisJoinPointStaticPart</literal>, only holds the static
  1126. part of a join point, but it is not the current but the enclosing
  1127. join point. So, for example, it is possible to print out the calling
  1128. source location (if available) with
  1129. </para>
  1130. <programlisting><![CDATA[
  1131. before() : execution (* *(..)) {
  1132. System.err.println(thisEnclosingJoinPointStaticPart.getSourceLocation())
  1133. }
  1134. ]]></programlisting>
  1135. </sect1>
  1136. </chapter>