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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  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. <sect2 id="advice" xreflabel="advice">
  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 id="some-example-pointcuts" xreflabel="some-example-pointcuts">
  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(call(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 id="call-vs-execution" xreflabel="call-vs-execution">
  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 id="pointcut-composition" xreflabel="pointcut-composition">
  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() && !within(A) {
  601. System.out.println("should occur");
  602. }
  603. before(): cflow(fooPC() && gooPC()) && printPC() && !within(A) {
  604. System.out.println("should not occur");
  605. }
  606. }
  607. ]]></programlisting>
  608. <para>
  609. The <literal>!within(<replaceable>A</replaceable>)</literal>
  610. pointcut above is required to avoid the <literal>printPC</literal>
  611. pointcut applying to the <literal>System.out.println</literal>
  612. call in the advice body. If this was not present a recursive call
  613. would result as the pointcut would apply to its own advice.
  614. (See <xref linkend="pitfalls-infiniteLoops"/> for more details.)
  615. </para>
  616. </sect2>
  617. <!-- ============================== -->
  618. <sect2 id="pointcut-parameters" xreflabel="pointcut-parameters">
  619. <title>Pointcut Parameters</title>
  620. <para>
  621. Consider again the first pointcut definition in this chapter:
  622. </para>
  623. <programlisting><![CDATA[
  624. pointcut setter(): target(Point) &&
  625. (call(void setX(int)) ||
  626. call(void setY(int)));
  627. ]]></programlisting>
  628. <para>
  629. As we've seen, this pointcut picks out each call to
  630. <literal>setX(int)</literal> or <literal>setY(int)</literal>
  631. methods where the target is an instance of
  632. <literal>Point</literal>. The pointcut is given the name
  633. <literal>setters</literal> and no parameters on the left-hand
  634. side. An empty parameter list means that none of the context from
  635. the join points is published from this pointcut. But consider
  636. another version of version of this pointcut definition:
  637. </para>
  638. <programlisting><![CDATA[
  639. pointcut setter(Point p): target(p) &&
  640. (call(void setX(int)) ||
  641. call(void setY(int)));
  642. ]]></programlisting>
  643. <para>
  644. This version picks out exactly the same join points. But in this
  645. version, the pointcut has one parameter of type
  646. <literal>Point</literal>. This means that any advice that uses this
  647. pointcut has access to a <literal>Point</literal> from each join
  648. point picked out by the pointcut. Inside the pointcut definition
  649. this <literal>Point</literal> is named <literal>p</literal> is
  650. available, and according to the right-hand side of the definition,
  651. that <literal>Point p</literal> comes from the
  652. <literal>target</literal> of each matched join point.
  653. </para>
  654. <para>
  655. Here's another example that illustrates the flexible mechanism for
  656. defining pointcut parameters:
  657. </para>
  658. <programlisting><![CDATA[
  659. pointcut testEquality(Point p): target(Point) &&
  660. args(p) &&
  661. call(boolean equals(Object));
  662. ]]></programlisting>
  663. <para>
  664. This pointcut also has a parameter of type
  665. <literal>Point</literal>. Similar to the
  666. <literal>setters</literal> pointcut, this means that anyone using
  667. this pointcut has access to a <literal>Point</literal> from each
  668. join point. But in this case, looking at the right-hand side we
  669. find that the object named in the parameters is not the target
  670. <literal>Point</literal> object that receives the call; it's the
  671. argument (also of type <literal>Point</literal>) passed to the
  672. <literal>equals</literal> method when some other
  673. <literal>Point</literal> is the target. If we wanted access to both
  674. <literal>Point</literal>s, then the pointcut definition that would
  675. expose target <literal>Point p1</literal> and argument
  676. <literal>Point p2</literal> would be
  677. </para>
  678. <programlisting><![CDATA[
  679. pointcut testEquality(Point p1, Point p2): target(p1) &&
  680. args(p2) &&
  681. call(boolean equals(Object));
  682. ]]></programlisting>
  683. <para>
  684. Let's look at another variation of the <literal>setters</literal> pointcut:
  685. </para>
  686. <programlisting><![CDATA[
  687. pointcut setter(Point p, int newval): target(p) &&
  688. args(newval) &&
  689. (call(void setX(int)) ||
  690. call(void setY(int)));
  691. ]]></programlisting>
  692. <para>
  693. In this case, a <literal>Point</literal> object and an
  694. <literal>int</literal> value are exposed by the named
  695. pointcut. Looking at the the right-hand side of the definition, we
  696. find that the <literal>Point</literal> object is the target object,
  697. and the <literal>int</literal> value is the called method's
  698. argument.
  699. </para>
  700. <para>
  701. The use of pointcut parameters is relatively flexible. The most
  702. important rule is that all the pointcut parameters must be bound at
  703. every join point picked out by the pointcut. So, for example, the
  704. following pointcut definition will result in a compilation error:
  705. <programlisting><![CDATA[
  706. pointcut badPointcut(Point p1, Point p2):
  707. (target(p1) && call(void setX(int))) ||
  708. (target(p2) && call(void setY(int)));
  709. ]]></programlisting>
  710. because <literal>p1</literal> is only bound when calling
  711. <literal>setX</literal>, and <literal>p2</literal> is only bound
  712. when calling <literal>setY</literal>, but the pointcut picks out
  713. all of these join points and tries to bind both
  714. <literal>p1</literal> and <literal>p2</literal>.
  715. </para>
  716. </sect2>
  717. <!-- ============================== -->
  718. <sect2 id="example" xreflabel="example">
  719. <title>Example: <literal>HandleLiveness</literal></title>
  720. <para>
  721. The example below consists of two object classes (plus an exception
  722. class) and one aspect. Handle objects delegate their public,
  723. non-static operations to their <literal>Partner</literal>
  724. objects. The aspect <literal>HandleLiveness</literal> ensures that,
  725. before the delegations, the partner exists and is alive, or else it
  726. throws an exception.
  727. </para>
  728. <programlisting><![CDATA[
  729. class Handle {
  730. Partner partner = new Partner();
  731. public void foo() { partner.foo(); }
  732. public void bar(int x) { partner.bar(x); }
  733. public static void main(String[] args) {
  734. Handle h1 = new Handle();
  735. h1.foo();
  736. h1.bar(2);
  737. }
  738. }
  739. class Partner {
  740. boolean isAlive() { return true; }
  741. void foo() { System.out.println("foo"); }
  742. void bar(int x) { System.out.println("bar " + x); }
  743. }
  744. aspect HandleLiveness {
  745. before(Handle handle): target(handle) && call(public * *(..)) {
  746. if ( handle.partner == null || !handle.partner.isAlive() ) {
  747. throw new DeadPartnerException();
  748. }
  749. }
  750. }
  751. class DeadPartnerException extends RuntimeException {}
  752. ]]></programlisting>
  753. </sect2>
  754. </sect1>
  755. <!-- ============================== -->
  756. <sect1 id="language-advice">
  757. <title>Advice</title>
  758. <para>
  759. Advice defines pieces of aspect implementation that execute at
  760. well-defined points in the execution of the program. Those points can
  761. be given either by named pointcuts (like the ones you've seen above)
  762. or by anonymous pointcuts. Here is an example of an advice on a named
  763. pointcut:
  764. </para>
  765. <programlisting><![CDATA[
  766. pointcut setter(Point p1, int newval): target(p1) && args(newval)
  767. (call(void setX(int) ||
  768. call(void setY(int)));
  769. before(Point p1, int newval): setter(p1, newval) {
  770. System.out.println("About to set something in " + p1 +
  771. " to the new value " + newval);
  772. }
  773. ]]></programlisting>
  774. <para>
  775. And here is exactly the same example, but using an anonymous
  776. pointcut:
  777. </para>
  778. <programlisting><![CDATA[
  779. before(Point p1, int newval): target(p1) && args(newval)
  780. (call(void setX(int)) ||
  781. call(void setY(int))) {
  782. System.out.println("About to set something in " + p1 +
  783. " to the new value " + newval);
  784. }
  785. ]]></programlisting>
  786. <para>
  787. Here are examples of the different advice:
  788. </para>
  789. <para>
  790. This before advice runs just before the join points picked out by the
  791. (anonymous) pointcut:
  792. </para>
  793. <programlisting><![CDATA[
  794. before(Point p, int x): target(p) && args(x) && call(void setX(int)) {
  795. if (!p.assertX(x)) return;
  796. }
  797. ]]></programlisting>
  798. <para>
  799. This after advice runs just after each join point picked out by the
  800. (anonymous) pointcut, regardless of whether it returns normally or throws
  801. an exception:
  802. </para>
  803. <programlisting><![CDATA[
  804. after(Point p, int x): target(p) && args(x) && call(void setX(int)) {
  805. if (!p.assertX(x)) throw new PostConditionViolation();
  806. }
  807. ]]></programlisting>
  808. <para>
  809. This after returning advice runs just after each join point picked
  810. out by the (anonymous) pointcut, but only if it returns normally.
  811. The return value can be accessed, and is named <literal>x</literal>
  812. here. After the advice runs, the return value is returned:
  813. </para>
  814. <programlisting><![CDATA[
  815. after(Point p) returning(int x): target(p) && call(int getX()) {
  816. System.out.println("Returning int value " + x + " for p = " + p);
  817. }
  818. ]]></programlisting>
  819. <para>
  820. This after throwing advice runs just after each join point picked out by
  821. the (anonymous) pointcut, but only when it throws an exception of type
  822. <literal>Exception</literal>. Here the exception value can be accessed
  823. with the name <literal>e</literal>. The advice re-raises the exception
  824. after it's done:
  825. </para>
  826. <programlisting><![CDATA[
  827. after() throwing(Exception e): target(Point) && call(void setX(int)) {
  828. System.out.println(e);
  829. }
  830. ]]></programlisting>
  831. <para>
  832. This around advice traps the execution of the join point; it runs
  833. <emphasis>instead</emphasis> of the join point. The original action
  834. associated with the join point can be invoked through the special
  835. <literal>proceed</literal> call:
  836. </para>
  837. <programlisting><![CDATA[
  838. void around(Point p, int x): target(p)
  839. && args(x)
  840. && call(void setX(int)) {
  841. if (p.assertX(x)) proceed(p, x);
  842. p.releaseResources();
  843. }
  844. ]]></programlisting>
  845. </sect1>
  846. <!-- ============================== -->
  847. <sect1 id="language-interType">
  848. <title>Inter-type declarations</title>
  849. <para>
  850. Aspects can declare members (fields, methods, and constructors) that
  851. are owned by other types. These are called inter-type members.
  852. Aspects can also declare that other types implement new interfaces or
  853. extend a new class. Here are examples of some such inter-type
  854. declarations:
  855. </para>
  856. <para>
  857. This declares that each <literal>Server</literal> has a
  858. <literal>boolean</literal> field named <literal>disabled</literal>,
  859. initialized to <literal>false</literal>:
  860. <programlisting><![CDATA[
  861. private boolean Server.disabled = false;
  862. ]]></programlisting>
  863. It is declared <literal>private</literal>, which means that it is
  864. private <emphasis>to the aspect</emphasis>: only code in the aspect
  865. can see the field. And even if <literal>Server</literal> has
  866. another private field named <literal>disabled</literal> (declared in
  867. <literal>Server</literal> or in another aspect) there won't be a name
  868. collision, since no reference to <literal>disabled</literal> will be
  869. ambiguous.
  870. </para>
  871. <para>
  872. This declares that each <literal>Point</literal> has an
  873. <literal>int</literal> method named <literal>getX</literal> with no
  874. arguments that returns whatever <literal>this.x</literal> is:
  875. <programlisting><![CDATA[
  876. public int Point.getX() { return this.x; }
  877. ]]></programlisting>
  878. Inside the body, <literal>this</literal> is the
  879. <literal>Point</literal> object currently executing. Because the
  880. method is publically declared any code can call it, but if there is
  881. some other <literal>Point.getX()</literal> declared there will be a
  882. compile-time conflict.
  883. </para>
  884. <para>
  885. This publically declares a two-argument constructor for
  886. <literal>Point</literal>:
  887. <programlisting><![CDATA[
  888. public Point.new(int x, int y) { this.x = x; this.y = y; }
  889. ]]></programlisting>
  890. </para>
  891. <para>
  892. This publicly declares that each <literal>Point</literal> has an
  893. <literal>int</literal> field named <literal>x</literal>, initialized
  894. to zero:
  895. <programlisting><![CDATA[
  896. public int Point.x = 0;
  897. ]]></programlisting>
  898. Because this is publically declared, it is an error if
  899. <literal>Point</literal> already has a field named
  900. <literal>x</literal> (defined by <literal>Point</literal> or by
  901. another aspect).
  902. </para>
  903. <para>
  904. This declares that the <literal>Point</literal> class implements the
  905. <literal>Comparable</literal> interface:
  906. <programlisting><![CDATA[
  907. declare parents: Point implements Comparable;
  908. ]]></programlisting>
  909. Of course, this will be an error unless <literal>Point</literal>
  910. defines the methods required by <literal>Comparable</literal>.
  911. </para>
  912. <para>
  913. This declares that the <literal>Point</literal> class extends the
  914. <literal>GeometricObject</literal> class.
  915. <programlisting><![CDATA[
  916. declare parents: Point extends GeometricObject;
  917. ]]></programlisting>
  918. </para>
  919. <para>
  920. An aspect can have several inter-type declarations. For example, the
  921. following declarations
  922. <programlisting><![CDATA[
  923. public String Point.name;
  924. public void Point.setName(String name) { this.name = name; }
  925. ]]></programlisting>
  926. publicly declare that Point has both a String field
  927. <literal>name</literal> and a <literal>void</literal> method
  928. <literal>setName(String)</literal> (which refers to the
  929. <literal>name</literal> field declared by the aspect).
  930. </para>
  931. <para>
  932. An inter-type member can only have one target type, but often you may
  933. wish to declare the same member on more than one type. This can be
  934. done by using an inter-type member in combination with a private
  935. interface:
  936. <programlisting><![CDATA[
  937. aspect A {
  938. private interface HasName {}
  939. declare parents: (Point || Line || Square) implements HasName;
  940. private String HasName.name;
  941. public String HasName.getName() { return name; }
  942. }
  943. ]]></programlisting>
  944. This declares a marker interface <literal>HasName</literal>, and also declares that any
  945. type that is either <literal>Point</literal>,
  946. <literal>Line</literal>, or <literal>Square</literal> implements that
  947. interface. It also privately declares that all <literal>HasName</literal>
  948. object have a <literal>String</literal> field called
  949. <literal>name</literal>, and publically declares that all
  950. <literal>HasName</literal> objects have a <literal>String</literal>
  951. method <literal>getName()</literal> (which refers to the privately
  952. declared <literal>name</literal> field).
  953. </para>
  954. <para>
  955. As you can see from the above example, an aspect can declare that
  956. interfaces have fields and methods, even non-constant fields and
  957. methods with bodies.
  958. </para>
  959. <!-- ============================== -->
  960. <sect2 id="inter-type-scope" xreflabel="inter-type-scope">
  961. <title>Inter-type Scope</title>
  962. <para>
  963. AspectJ allows private and package-protected (default) inter-type declarations in
  964. addition to public inter-type declarations. Private means private in
  965. relation to the aspect, not necessarily the target type. So, if an
  966. aspect makes a private inter-type declaration of a field
  967. <programlisting><![CDATA[
  968. private int Foo.x;
  969. ]]></programlisting>
  970. Then code in the aspect can refer to <literal>Foo</literal>'s
  971. <literal>x</literal> field, but nobody else can. Similarly, if an
  972. aspect makes a package-protected introduction,
  973. </para>
  974. <programlisting><![CDATA[
  975. int Foo.x;
  976. ]]></programlisting>
  977. <para>
  978. then everything in the aspect's package (which may or may not be
  979. <literal>Foo</literal>'s package) can access <literal>x</literal>.
  980. </para>
  981. </sect2>
  982. <!-- ============================== -->
  983. <sect2 id="example-pointassertions" xreflabel="example-pointassertions">
  984. <title>Example: <literal>PointAssertions</literal></title>
  985. <para>
  986. The example below consists of one class and one aspect. The aspect
  987. privately declares the assertion methods of
  988. <literal>Point</literal>, <literal>assertX</literal> and
  989. <literal>assertY</literal>. It also guards calls to
  990. <literal>setX</literal> and <literal>setY</literal> with calls to
  991. these assertion methods. The assertion methods are declared
  992. privately because other parts of the program (including the code in
  993. <literal>Point</literal>) have no business accessing the assert
  994. methods. Only the code inside of the aspect can call those
  995. methods.
  996. </para>
  997. <programlisting><![CDATA[
  998. class Point {
  999. int x, y;
  1000. public void setX(int x) { this.x = x; }
  1001. public void setY(int y) { this.y = y; }
  1002. public static void main(String[] args) {
  1003. Point p = new Point();
  1004. p.setX(3); p.setY(333);
  1005. }
  1006. }
  1007. aspect PointAssertions {
  1008. private boolean Point.assertX(int x) {
  1009. return (x <= 100 && x >= 0);
  1010. }
  1011. private boolean Point.assertY(int y) {
  1012. return (y <= 100 && y >= 0);
  1013. }
  1014. before(Point p, int x): target(p) && args(x) && call(void setX(int)) {
  1015. if (!p.assertX(x)) {
  1016. System.out.println("Illegal value for x"); return;
  1017. }
  1018. }
  1019. before(Point p, int y): target(p) && args(y) && call(void setY(int)) {
  1020. if (!p.assertY(y)) {
  1021. System.out.println("Illegal value for y"); return;
  1022. }
  1023. }
  1024. }
  1025. ]]></programlisting>
  1026. </sect2>
  1027. </sect1>
  1028. <!-- ================================================== -->
  1029. <sect1 id="language-thisJoinPoint">
  1030. <title>thisJoinPoint</title>
  1031. <para>
  1032. AspectJ provides a special reference variable,
  1033. <literal>thisJoinPoint</literal>, that contains reflective
  1034. information about the current join point for the advice to use. The
  1035. <literal>thisJoinPoint</literal> variable can only be used in the
  1036. context of advice, just like <literal>this</literal> can only be used
  1037. in the context of non-static methods and variable initializers. In
  1038. advice, <literal>thisJoinPoint</literal> is an object of type <ulink
  1039. url="../api/org/aspectj/lang/JoinPoint.html"><literal>org.aspectj.lang.JoinPoint</literal></ulink>.
  1040. </para>
  1041. <para>
  1042. One way to use it is simply to print it out. Like all Java objects,
  1043. <literal>thisJoinPoint</literal> has a <literal>toString()</literal>
  1044. method that makes quick-and-dirty tracing easy:
  1045. </para>
  1046. <programlisting><![CDATA[
  1047. class TraceNonStaticMethods {
  1048. before(Point p): target(p) && call(* *(..)) {
  1049. System.out.println("Entering " + thisJoinPoint + " in " + p);
  1050. }
  1051. }
  1052. ]]></programlisting>
  1053. <para>
  1054. The type of <literal>thisJoinPoint</literal> includes a rich
  1055. reflective class hierarchy of signatures, and can be used to access
  1056. both static and dynamic information about join points such as the
  1057. arguments of the join point:
  1058. <programlisting><![CDATA[
  1059. thisJoinPoint.getArgs()
  1060. ]]></programlisting>
  1061. In addition, it holds an object consisting of all the static
  1062. information about the join point such as corresponding line number
  1063. and static signature:
  1064. <programlisting><![CDATA[
  1065. thisJoinPoint.getStaticPart()
  1066. ]]></programlisting>
  1067. If you only need the static information about the join point, you may
  1068. access the static part of the join point directly with the special
  1069. variable <literal>thisJoinPointStaticPart</literal>. Using
  1070. <literal>thisJoinPointStaticPart</literal> will avoid the run-time
  1071. creation of the join point object that may be necessary when using
  1072. <literal>thisJoinPoint</literal> directly.
  1073. </para>
  1074. <para>It is always the case that
  1075. </para>
  1076. <programlisting><![CDATA[
  1077. thisJoinPointStaticPart == thisJoinPoint.getStaticPart()
  1078. thisJoinPoint.getKind() == thisJoinPointStaticPart.getKind()
  1079. thisJoinPoint.getSignature() == thisJoinPointStaticPart.getSignature()
  1080. thisJoinPoint.getSourceLocation() == thisJoinPointStaticPart.getSourceLocation()
  1081. ]]></programlisting>
  1082. <para>
  1083. One more reflective variable is available:
  1084. <literal>thisEnclosingJoinPointStaticPart</literal>. This, like
  1085. <literal>thisJoinPointStaticPart</literal>, only holds the static
  1086. part of a join point, but it is not the current but the enclosing
  1087. join point. So, for example, it is possible to print out the calling
  1088. source location (if available) with
  1089. </para>
  1090. <programlisting><![CDATA[
  1091. before() : execution (* *(..)) {
  1092. System.err.println(thisEnclosingJoinPointStaticPart.getSourceLocation())
  1093. }
  1094. ]]></programlisting>
  1095. </sect1>
  1096. </chapter>