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 43KB

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