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.

semantics.xml 102KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950
  1. <appendix id="semantics" xreflabel="Semantics">
  2. <title>Language Semantics</title>
  3. <sect1 id="semantics-intro">
  4. <title>Introduction</title>
  5. <para>
  6. AspectJ extends Java by overlaying a concept of join points onto the
  7. existing Java semantics and adding a few new program elements to Java:
  8. </para>
  9. <para>
  10. A join point is a well-defined point in the execution of a
  11. program. These include method and constructor calls, field accesses and
  12. others described below.
  13. </para>
  14. <para>
  15. A pointcut picks out join points, and exposes some of the values in the
  16. execution context of those join points. There are several primitive
  17. pointcut designators, and others can be named and defined by the
  18. <literal>pointcut</literal> declaration.
  19. </para>
  20. <para>
  21. A piece of advice is code that executes at each join point in a
  22. pointcut. Advice has access to the values exposed by the
  23. pointcut. Advice is defined by <literal>before</literal>,
  24. <literal>after</literal>, and <literal>around</literal> declarations.
  25. </para>
  26. <para>
  27. Inter-type declarations form AspectJ's static crosscutting features,
  28. that is, is code that may change the type structure of a program, by
  29. adding to or extending interfaces and classes with new fields,
  30. constructors, or methods. Some inter-type declarations are defined
  31. through an extension of usual method, field, and constructor
  32. declarations, and other declarations are made with a new
  33. <literal>declare</literal> keyword.
  34. </para>
  35. <para>
  36. An aspect is a crosscutting type that encapsulates pointcuts, advice,
  37. and static crosscutting features. By type, we mean Java's notion: a
  38. modular unit of code, with a well-defined interface, about which it is
  39. possible to do reasoning at compile time. Aspects are defined by the
  40. <literal>aspect</literal> declaration.
  41. </para>
  42. </sect1>
  43. <!-- ============================== -->
  44. <sect1 id="semantics-joinPoints">
  45. <title>Join Points</title>
  46. <para>
  47. While aspects define types that crosscut, the AspectJ system does not
  48. allow completely arbitrary crosscutting. Rather, aspects define types
  49. that cut across principled points in a program's execution. These
  50. principled points are called join points.
  51. </para>
  52. <para>
  53. A join point is a well-defined point in the execution of a
  54. program. The join points defined by AspectJ are:
  55. </para>
  56. <variablelist>
  57. <varlistentry>
  58. <term>Method call</term>
  59. <listitem>
  60. When a method is called, not including super calls of
  61. non-static methods.
  62. </listitem>
  63. </varlistentry>
  64. <varlistentry>
  65. <term>Method execution</term>
  66. <listitem>
  67. When the body of code for an actual method executes.
  68. </listitem>
  69. </varlistentry>
  70. <varlistentry>
  71. <term>Constructor call</term>
  72. <listitem>
  73. When an object is built and that object's initial constructor is
  74. called (i.e., not for "super" or "this" constructor calls). The
  75. object being constructed is returned at a constructor call join
  76. point, so its return type is considered to be the type of the
  77. object, and the object itself may be accessed with <literal>after
  78. returning</literal> advice.
  79. </listitem>
  80. </varlistentry>
  81. <varlistentry>
  82. <term>Constructor execution</term>
  83. <listitem>
  84. When the body of code for an actual constructor executes, after
  85. its this or super constructor call. The object being constructed
  86. is the currently executing object, and so may be accessed with
  87. the <literal>this</literal> pointcut. The constructor execution
  88. join point for a constructor that calls a super constructor also
  89. includes any non-static initializers of enclosing class. No
  90. value is returned from a constructor execution join point, so its
  91. return type is considered to be void.
  92. </listitem>
  93. </varlistentry>
  94. <varlistentry>
  95. <term>Static initializer execution</term>
  96. <listitem>
  97. When the static initializer for a class executes. No value is
  98. returned from a static initializer execution join point, so its
  99. return type is considered to be void.
  100. </listitem>
  101. </varlistentry>
  102. <varlistentry>
  103. <term>Object pre-initialization</term>
  104. <listitem>
  105. Before the object initialization code for a particular class runs.
  106. This encompasses the time between the start of its first called
  107. constructor and the start of its parent's constructor. Thus, the
  108. execution of these join points encompass the join points of the
  109. evaluation of the arguments of <literal>this()</literal> and
  110. <literal>super()</literal> constructor calls. No value is
  111. returned from an object pre-initialization join point, so its
  112. return type is considered to be void.
  113. </listitem>
  114. </varlistentry>
  115. <varlistentry>
  116. <term>Object initialization</term>
  117. <listitem>
  118. When the object initialization code for a particular class runs.
  119. This encompasses the time between the return of its parent's
  120. constructor and the return of its first called constructor. It
  121. includes all the dynamic initializers and constructors used to
  122. create the object. The object being constructed is the currently
  123. executing object, and so may be accessed with the
  124. <literal>this</literal> pointcut. No value is returned from a
  125. constructor execution join point, so its return type is
  126. considered to be void.
  127. </listitem>
  128. </varlistentry>
  129. <varlistentry>
  130. <term>Field reference</term>
  131. <listitem>
  132. When a non-constant field is referenced. [Note that references
  133. to constant fields (static final fields bound to a constant
  134. string object or primitive value) are not join points, since Java
  135. requires them to be inlined.]
  136. </listitem>
  137. </varlistentry>
  138. <varlistentry>
  139. <term>Field set</term>
  140. <listitem>
  141. When a field is assigned to.
  142. Field set join points are considered to have one argument,
  143. the value the field is being set to.
  144. No value is returned from a field set join point, so
  145. its return type is considered to be void.
  146. [Note that the initializations of constant fields (static
  147. final fields where the initializer is a constant string object or
  148. primitive value) are not join points, since Java requires their
  149. references to be inlined.]
  150. </listitem>
  151. </varlistentry>
  152. <varlistentry>
  153. <term>Handler execution</term>
  154. <listitem>
  155. When an exception handler executes.
  156. Handler execution join points are considered to have one argument,
  157. the exception being handled.
  158. No value is returned from a field set join point, so
  159. its return type is considered to be void.
  160. </listitem>
  161. </varlistentry>
  162. <varlistentry>
  163. <term>Advice execution</term>
  164. <listitem>
  165. When the body of code for a piece of advice executes.
  166. </listitem>
  167. </varlistentry>
  168. </variablelist>
  169. <para>
  170. Each join point potentially has three pieces of state associated
  171. with it: the currently executing object, the target object, and
  172. an object array of arguments. These are exposed by the three
  173. state-exposing pointcuts, <literal>this</literal>,
  174. <literal>target</literal>, and <literal>args</literal>,
  175. respectively.
  176. </para>
  177. <para>
  178. Informally, the currently executing object is the object that a
  179. <literal>this</literal> expression would pick out at the join
  180. point. The target object is where control or attention is
  181. transferred to by the join point. The arguments are those
  182. values passed for that transfer of control or attention.
  183. </para>
  184. <informaltable frame="1">
  185. <tgroup cols="4" align="left">
  186. <tbody valign="top">
  187. <row>
  188. <entry><emphasis role="bold">Join Point</emphasis></entry>
  189. <entry><emphasis role="bold">Current Object</emphasis></entry>
  190. <entry><emphasis role="bold">Target Object</emphasis></entry>
  191. <entry><emphasis role="bold">Arguments</emphasis></entry>
  192. </row>
  193. <row>
  194. <entry>Method Call</entry>
  195. <entry>executing object*</entry>
  196. <entry>target object**</entry>
  197. <entry>method arguments</entry>
  198. </row>
  199. <row>
  200. <entry>Method Execution</entry>
  201. <entry>executing object*</entry>
  202. <entry>executing object*</entry>
  203. <entry>method arguments</entry>
  204. </row>
  205. <row>
  206. <entry>Constructor Call</entry>
  207. <entry>executing object*</entry>
  208. <entry>None</entry>
  209. <entry>constructor arguments</entry>
  210. </row>
  211. <row>
  212. <entry>Constructor Execution</entry>
  213. <entry>executing object</entry>
  214. <entry>executing object</entry>
  215. <entry>constructor arguments</entry>
  216. </row>
  217. <row>
  218. <entry>Static initializer execution</entry>
  219. <entry>None</entry>
  220. <entry>None</entry>
  221. <entry>None</entry>
  222. </row>
  223. <row>
  224. <entry>Object pre-initialization</entry>
  225. <entry>None</entry>
  226. <entry>None</entry>
  227. <entry>constructor arguments</entry>
  228. </row>
  229. <row>
  230. <entry>Object initialization</entry>
  231. <entry>executing object</entry>
  232. <entry>executing object</entry>
  233. <entry>constructor arguments</entry>
  234. </row>
  235. <row>
  236. <entry>Field reference</entry>
  237. <entry>executing object*</entry>
  238. <entry>target object**</entry>
  239. <entry>None</entry>
  240. </row>
  241. <row>
  242. <entry>Field assignment</entry>
  243. <entry>executing object*</entry>
  244. <entry>target object**</entry>
  245. <entry>assigned value</entry>
  246. </row>
  247. <row>
  248. <entry>Handler execution</entry>
  249. <entry>executing object*</entry>
  250. <entry>executing object*</entry>
  251. <entry>caught exception</entry>
  252. </row>
  253. <row>
  254. <entry>Advice execution</entry>
  255. <entry>executing aspect</entry>
  256. <entry>executing aspect</entry>
  257. <entry>advice arguments</entry>
  258. </row>
  259. </tbody>
  260. </tgroup>
  261. </informaltable>
  262. <para>* There is no executing object in static contexts such as
  263. static method bodies or static initializers.
  264. </para>
  265. <para>** There is no target object for join points associated
  266. with static methods or fields.
  267. </para>
  268. </sect1>
  269. <!-- ============================== -->
  270. <sect1 id="semantics-pointcuts">
  271. <title>Pointcuts</title>
  272. <para>
  273. A pointcut is a program element that picks out join points and
  274. exposes data from the execution context of those join points.
  275. Pointcuts are used primarily by advice. They can be composed with
  276. boolean operators to build up other pointcuts. The primitive
  277. pointcuts and combinators provided by the language are:
  278. </para>
  279. <variablelist>
  280. <varlistentry>
  281. <term><literal>call(<replaceable>MethodPattern</replaceable>)</literal></term>
  282. <listitem>
  283. Picks out each method call join point whose signature matches
  284. <replaceable>MethodPattern</replaceable>.
  285. </listitem>
  286. </varlistentry>
  287. <varlistentry>
  288. <term><literal>execution(<replaceable>MethodPattern</replaceable>)</literal></term>
  289. <listitem>
  290. Picks out each method execution join point whose signature matches
  291. <replaceable>MethodPattern</replaceable>.
  292. </listitem>
  293. </varlistentry>
  294. <varlistentry>
  295. <term><literal>get(<replaceable>FieldPattern</replaceable>)</literal></term>
  296. <listitem>
  297. Picks out each field reference join point whose signature matches
  298. <replaceable>FieldPattern</replaceable>.
  299. [Note that references to constant fields (static final
  300. fields bound to a constant string object or primitive value) are not
  301. join points, since Java requires them to be inlined.]
  302. </listitem>
  303. </varlistentry>
  304. <varlistentry>
  305. <term><literal>set(<replaceable>FieldPattern</replaceable>)</literal></term>
  306. <listitem>
  307. Picks out each field set join point whose signature matches
  308. <replaceable>FieldPattern</replaceable>.
  309. [Note that the initializations of constant fields (static
  310. final fields where the initializer is a constant string object or
  311. primitive value) are not join points, since Java requires their
  312. references to be inlined.]
  313. </listitem>
  314. </varlistentry>
  315. <varlistentry>
  316. <term><literal>call(<replaceable>ConstructorPattern</replaceable>)</literal></term>
  317. <listitem>
  318. Picks out each constructor call join point whose signature matches
  319. <replaceable>ConstructorPattern</replaceable>.
  320. </listitem>
  321. </varlistentry>
  322. <varlistentry>
  323. <term><literal>execution(<replaceable>ConstructorPattern</replaceable>)</literal></term>
  324. <listitem>
  325. Picks out each constructor execution join point whose signature matches
  326. <replaceable>ConstructorPattern</replaceable>.
  327. </listitem>
  328. </varlistentry>
  329. <varlistentry>
  330. <term><literal>initialization(<replaceable>ConstructorPattern</replaceable>)</literal></term>
  331. <listitem>
  332. Picks out each object initialization join point whose signature matches
  333. <replaceable>ConstructorPattern</replaceable>.
  334. </listitem>
  335. </varlistentry>
  336. <varlistentry>
  337. <term><literal>preinitialization(<replaceable>ConstructorPattern</replaceable>)</literal></term>
  338. <listitem>
  339. Picks out each object pre-initialization join point whose signature matches
  340. <replaceable>ConstructorPattern</replaceable>.
  341. </listitem>
  342. </varlistentry>
  343. <varlistentry>
  344. <term><literal>staticinitialization(<replaceable>TypePattern</replaceable>)</literal></term>
  345. <listitem>
  346. Picks out each static initializer execution join point whose signature matches
  347. <replaceable>TypePattern</replaceable>.
  348. </listitem>
  349. </varlistentry>
  350. <varlistentry>
  351. <term><literal>handler(<replaceable>TypePattern</replaceable>)</literal></term>
  352. <listitem>
  353. Picks out each exception handler join point whose signature matches
  354. <replaceable>TypePattern</replaceable>.
  355. </listitem>
  356. </varlistentry>
  357. <varlistentry>
  358. <term><literal>adviceexecution()</literal></term>
  359. <listitem>
  360. Picks out all advice execution join points.
  361. </listitem>
  362. </varlistentry>
  363. <varlistentry>
  364. <term><literal>within(<replaceable>TypePattern</replaceable>)</literal></term>
  365. <listitem>
  366. Picks out each join point where the executing code is defined
  367. in a type matched by <replaceable>TypePattern</replaceable>.
  368. </listitem>
  369. </varlistentry>
  370. <varlistentry>
  371. <term><literal>withincode(<replaceable>MethodPattern</replaceable>)</literal></term>
  372. <listitem>
  373. Picks out each join point where the executing code is defined in
  374. a method whose signature matches
  375. <replaceable>MethodPattern</replaceable>.
  376. </listitem>
  377. </varlistentry>
  378. <varlistentry>
  379. <term><literal>withincode(<replaceable>ConstructorPattern</replaceable>)</literal></term>
  380. <listitem>
  381. Picks out each join point where the executing code is defined
  382. in a constructor whose signature matches
  383. <replaceable>ConstructorPattern</replaceable>.
  384. </listitem>
  385. </varlistentry>
  386. <varlistentry>
  387. <term><literal>cflow(<replaceable>Pointcut</replaceable>)</literal></term>
  388. <listitem>
  389. Picks out each join point in the control flow of any join point
  390. <replaceable>P</replaceable> picked out by
  391. <replaceable>Pointcut</replaceable>, including
  392. <replaceable>P</replaceable> itself.
  393. </listitem>
  394. </varlistentry>
  395. <varlistentry>
  396. <term><literal>cflowbelow(<replaceable>Pointcut</replaceable>)</literal></term>
  397. <listitem>
  398. Picks out each join point in the control flow of any join point
  399. <replaceable>P</replaceable> picked out by
  400. <replaceable>Pointcut</replaceable>, but not
  401. <replaceable>P</replaceable> itself.
  402. </listitem>
  403. </varlistentry>
  404. <varlistentry>
  405. <term><literal>this(<replaceable>Type</replaceable> or <replaceable>Id</replaceable>)</literal></term>
  406. <listitem>
  407. Picks out each join point where the currently executing object
  408. (the object bound to <literal>this</literal>) is an instance of
  409. <replaceable>Type</replaceable>, or of the type of the
  410. identifier <replaceable>Id</replaceable> (which must be bound in the enclosing
  411. advice or pointcut definition).
  412. Will not match any join points from static contexts.
  413. </listitem>
  414. </varlistentry>
  415. <varlistentry>
  416. <term><literal>target(<replaceable>Type</replaceable> or <replaceable>Id</replaceable>)</literal></term>
  417. <listitem>
  418. Picks out each join point where the target object (the object
  419. on which a call or field operation is applied to) is an instance of
  420. <replaceable>Type</replaceable>, or of the type of the identifier
  421. <replaceable>Id</replaceable> (which must be bound in the enclosing
  422. advice or pointcut definition).
  423. Will not match any calls, gets, or sets of static members.
  424. </listitem>
  425. </varlistentry>
  426. <varlistentry>
  427. <term><literal>args(<replaceable>Type</replaceable> or <replaceable>Id</replaceable>, ...)</literal></term>
  428. <listitem>
  429. Picks out each join point where the arguments are instances of
  430. a type of the appropriate type pattern or identifier.
  431. </listitem>
  432. </varlistentry>
  433. <varlistentry>
  434. <term><literal><replaceable>PointcutId</replaceable>(<replaceable>TypePattern</replaceable> or <replaceable>Id</replaceable>, ...)</literal></term>
  435. <listitem>
  436. Picks out each join point that is picked out by the
  437. user-defined pointcut designator named by
  438. <replaceable>PointcutId</replaceable>.
  439. </listitem>
  440. </varlistentry>
  441. <varlistentry>
  442. <term><literal>if(<replaceable>BooleanExpression</replaceable>)</literal></term>
  443. <listitem>
  444. Picks out each join point where the boolean expression
  445. evaluates to <literal>true</literal>. The boolean expression used
  446. can only access static members, variables exposed by teh enclosing
  447. pointcut or advice, and <literal>thisJoinPoint</literal> forms. In
  448. particular, it cannot call non-static methods on the aspect.
  449. </listitem>
  450. </varlistentry>
  451. <varlistentry>
  452. <term><literal>! <replaceable>Pointcut</replaceable></literal></term>
  453. <listitem>
  454. Picks out each join point that is not picked out by
  455. <replaceable>Pointcut</replaceable>.
  456. </listitem>
  457. </varlistentry>
  458. <varlistentry>
  459. <term><literal><replaceable>Pointcut0</replaceable> <![CDATA[&&]]> <replaceable>Pointcut1</replaceable></literal></term>
  460. <listitem>
  461. Picks out each join points that is picked out by both
  462. <replaceable>Pointcut0</replaceable> and
  463. <replaceable>Pointcut1</replaceable>.
  464. </listitem>
  465. </varlistentry>
  466. <varlistentry>
  467. <term><literal><replaceable>Pointcut0</replaceable> || <replaceable>Pointcut1</replaceable></literal></term>
  468. <listitem>
  469. Picks out each join point that is picked out by either
  470. pointcuts. <replaceable>Pointcut0</replaceable> or
  471. <replaceable>Pointcut1</replaceable>.
  472. </listitem>
  473. </varlistentry>
  474. <varlistentry>
  475. <term><literal>( <replaceable>Pointcut</replaceable> )</literal></term>
  476. <listitem>
  477. Picks out each join points picked out by
  478. <replaceable>Pointcut</replaceable>.
  479. </listitem>
  480. </varlistentry>
  481. </variablelist>
  482. <sect2>
  483. <title>Pointcut definition</title>
  484. <para>
  485. Pointcuts are defined and named by the programmer with the
  486. <literal>pointcut</literal> declaration.
  487. </para>
  488. <programlisting>
  489. pointcut publicIntCall(int i):
  490. call(public * *(int)) <![CDATA[&&]]> args(i);
  491. </programlisting>
  492. <para>
  493. A named pointcut may be defined in either a class or aspect, and is
  494. treated as a member of the class or aspect where it is found. As a
  495. member, it may have an access modifier such as
  496. <literal>public</literal> or <literal>private</literal>.
  497. </para>
  498. <programlisting>
  499. class C {
  500. pointcut publicCall(int i):
  501. call(public * *(int)) <![CDATA[&&]]> args(i);
  502. }
  503. class D {
  504. pointcut myPublicCall(int i):
  505. C.publicCall(i) <![CDATA[&&]]> within(SomeType);
  506. }
  507. </programlisting>
  508. <para>
  509. Pointcuts that are not final may be declared abstract, and defined
  510. without a body. Abstract pointcuts may only be declared within
  511. abstract aspects.
  512. </para>
  513. <programlisting>
  514. abstract aspect A {
  515. abstract pointcut publicCall(int i);
  516. }
  517. </programlisting>
  518. <para>
  519. In such a case, an extending aspect may override the abstract
  520. pointcut.
  521. </para>
  522. <programlisting>
  523. aspect B extends A {
  524. pointcut publicCall(int i): call(public Foo.m(int)) <![CDATA[&&]]> args(i);
  525. }
  526. </programlisting>
  527. <para>
  528. For completeness, a pointcut with a declaration may be declared
  529. <literal>final</literal>.
  530. </para>
  531. <para>
  532. Though named pointcut declarations appear somewhat like method
  533. declarations, and can be overridden in subaspects, they cannot be
  534. overloaded. It is an error for two pointcuts to be named with the
  535. same name in the same class or aspect declaration.
  536. </para>
  537. <para>
  538. The scope of a named pointcut is the enclosing class declaration.
  539. This is different than the scope of other members; the scope of
  540. other members is the enclosing class <emphasis>body</emphasis>.
  541. This means that the following code is legal:
  542. </para>
  543. <programlisting>
  544. aspect B percflow(publicCall()) {
  545. pointcut publicCall(): call(public Foo.m(int));
  546. }
  547. </programlisting>
  548. </sect2>
  549. <sect2>
  550. <title>Context exposure</title>
  551. <para>
  552. Pointcuts have an interface; they expose some parts of the
  553. execution context of the join points they pick out. For example,
  554. the PublicIntCall above exposes the first argument from the
  555. receptions of all public unary integer methods. This context is
  556. exposed by providing typed formal parameters to named pointcuts and
  557. advice, like the formal parameters of a Java method. These formal
  558. parameters are bound by name matching.
  559. </para>
  560. <para>
  561. On the right-hand side of advice or pointcut declarations, in
  562. certain pointcut designators, a Java identifier is allowed in place
  563. of a type or collection of types. The pointcut designators that
  564. allow this are <literal>this</literal>, <literal>target</literal>,
  565. and <literal>args</literal>. In all such cases, using an
  566. identifier rather than a type does two things. First, it selects
  567. join points as based on the type of the formal parameter. So the
  568. pointcut
  569. </para>
  570. <programlisting>
  571. pointcut intArg(int i): args(i);
  572. </programlisting>
  573. <para>
  574. picks out join points where an <literal>int</literal> is being
  575. passed as an argument. Second, though, it makes the value of that
  576. argument available to the enclosing advice or pointcut.
  577. </para>
  578. <para>
  579. Values can be exposed from named pointcuts as well, so
  580. </para>
  581. <programlisting>
  582. pointcut publicCall(int x): call(public *.*(int)) <![CDATA[&&]]> intArg(x);
  583. pointcut intArg(int i): args(i);
  584. </programlisting>
  585. <para>
  586. is a legal way to pick out all calls to public methods accepting an
  587. int argument, and exposing that argument.
  588. </para>
  589. <para>
  590. There is one special case for this kind of exposure. Exposing an
  591. argument of type Object will also match primitive typed arguments,
  592. and expose a "boxed" version of the primitive. So,
  593. </para>
  594. <programlisting>
  595. pointcut publicCall(): call(public *.*(..)) <![CDATA[&&]]> args(Object);
  596. </programlisting>
  597. <para>
  598. will pick out all unary methods that take, as their only argument,
  599. subtypes of Object (i.e., not primitive types like
  600. <literal>int</literal>), but
  601. </para>
  602. <programlisting>
  603. pointcut publicCall(Object o): call(public *.*(..)) <![CDATA[&&]]> args(o);
  604. </programlisting>
  605. <para>
  606. will pick out all unary methods that take any argument: And if the
  607. argument was an <literal>int</literal>, then the value passed to
  608. advice will be of type <literal>java.lang.Integer</literal>.
  609. </para>
  610. </sect2>
  611. <sect2>
  612. <title>Primitive pointcuts</title>
  613. <sect3>
  614. <title>Method-related pointcuts</title>
  615. <para>AspectJ provides two primitive pointcut designators designed to
  616. capture method call and execution join points. </para>
  617. <itemizedlist>
  618. <listitem><literal>call(<replaceable>MethodPattern</replaceable>)</literal></listitem>
  619. <listitem><literal>execution(<replaceable>MethodPattern</replaceable>)</literal></listitem>
  620. </itemizedlist>
  621. </sect3>
  622. <sect3>
  623. <title>Field-related pointcuts</title>
  624. <para>
  625. AspectJ provides two primitive pointcut designators designed to
  626. capture field reference and set join points:
  627. </para>
  628. <itemizedlist>
  629. <listitem><literal>get(<replaceable>FieldPattern</replaceable>)</literal></listitem>
  630. <listitem><literal>set(<replaceable>FieldPattern</replaceable>)</literal></listitem>
  631. </itemizedlist>
  632. <para>
  633. All set join points are treated as having one argument, the value the
  634. field is being set to, so at a set join point, that value can be
  635. accessed with an <literal>args</literal> pointcut. So an aspect
  636. guarding a static integer variable x declared in type T might be written as
  637. </para>
  638. <programlisting><![CDATA[
  639. aspect GuardedX {
  640. static final int MAX_CHANGE = 100;
  641. before(int newval): set(static int T.x) && args(newval) {
  642. if (Math.abs(newval - T.x) > MAX_CHANGE)
  643. throw new RuntimeException();
  644. }
  645. }
  646. ]]></programlisting>
  647. </sect3>
  648. <sect3>
  649. <title>Object creation-related pointcuts</title>
  650. <para>
  651. AspectJ provides primitive pointcut designators designed to
  652. capture the initializer execution join points of objects.
  653. </para>
  654. <itemizedlist>
  655. <listitem><literal>call(<replaceable>ConstructorPattern</replaceable>)</literal></listitem>
  656. <listitem><literal>execution(<replaceable>ConstructorPattern</replaceable>)</literal></listitem>
  657. <listitem><literal>initialization(<replaceable>ConstructorPattern</replaceable>)</literal></listitem>
  658. <listitem><literal>preinitialization(<replaceable>ConstructorPattern</replaceable>)</literal></listitem>
  659. </itemizedlist>
  660. </sect3>
  661. <sect3>
  662. <title>Class initialization-related pointcuts</title>
  663. <para>
  664. AspectJ provides one primitive pointcut designator to pick out
  665. static initializer execution join points.
  666. </para>
  667. <itemizedlist>
  668. <listitem><literal>staticinitialization(<replaceable>TypePattern</replaceable>)</literal></listitem>
  669. </itemizedlist>
  670. </sect3>
  671. <sect3>
  672. <title>Exception handler execution-related pointcuts</title>
  673. <para>
  674. AspectJ provides one primitive pointcut designator to capture
  675. execution of exception handlers:
  676. </para>
  677. <itemizedlist>
  678. <listitem><literal>handler(<replaceable>TypePattern</replaceable>)</literal></listitem>
  679. </itemizedlist>
  680. <para>
  681. All handler join points are treated as having one argument, the value
  682. of the exception being handled. That value can be accessed with an
  683. <literal>args</literal> pointcut. So an aspect used to put
  684. <literal>FooException</literal> objects into some normal form before
  685. they are handled could be written as
  686. </para>
  687. <programlisting>
  688. aspect NormalizeFooException {
  689. before(FooException e): handler(FooException) <![CDATA[&&]]> args(e) {
  690. e.normalize();
  691. }
  692. }
  693. </programlisting>
  694. </sect3>
  695. <sect3>
  696. <title>Advice execution-related pointcuts</title>
  697. <para>
  698. AspectJ provides one primitive pointcut designator to capture
  699. execution of advice
  700. </para>
  701. <itemizedlist>
  702. <listitem><literal>adviceexecution()</literal></listitem>
  703. </itemizedlist>
  704. <para>
  705. This can be used, for example, to filter out any join point in the
  706. control flow of advice from a particular aspect.
  707. </para>
  708. <programlisting>
  709. aspect TraceStuff {
  710. pointcut myAdvice(): adviceexecution() <![CDATA[&&]]> within(TraceStuff);
  711. before(): call(* *(..)) <![CDATA[&&]]> !cflow(myAdvice) {
  712. // do something
  713. }
  714. }
  715. </programlisting>
  716. </sect3>
  717. <sect3>
  718. <title>State-based pointcuts</title>
  719. <para>
  720. Many concerns cut across the dynamic times when an object of a
  721. particular type is executing, being operated on, or being passed
  722. around. AspectJ provides primitive pointcuts that capture join
  723. points at these times. These pointcuts use the dynamic types of
  724. their objects to pick out join points. They may also be used to
  725. expose the objects used for discrimination.
  726. </para>
  727. <itemizedlist>
  728. <listitem><literal>this(<replaceable>Type</replaceable> or <replaceable>Id</replaceable>)</literal></listitem>
  729. <listitem><literal>target(<replaceable>Type</replaceable> or <replaceable>Id</replaceable>)</literal></listitem>
  730. </itemizedlist>
  731. <para>
  732. The <literal>this</literal> pointcut picks out each join point where
  733. the currently executing object (the object bound to
  734. <literal>this</literal>) is an instance of a particular type. The
  735. <literal>target</literal> pointcut picks out each join point where
  736. the target object (the object on which a method is called or a field
  737. is accessed) is an instance of a particular type. Note that
  738. <literal>target</literal> should be understood to be the object the
  739. current join point is transfering control to. This means that the
  740. target object is the same as the current object at a method execution
  741. join point, for example, but may be different at a method call join
  742. point.
  743. </para>
  744. <itemizedlist>
  745. <listitem><literal>args(<replaceable>Type</replaceable> or <replaceable>Id</replaceable> or "..", ...)</literal></listitem>
  746. </itemizedlist>
  747. <para>
  748. The args pointcut picks out each join point where the arguments are
  749. instances of some types. Each element in the comma-separated list is
  750. one of four things. If it is a type name, then the argument in that
  751. position must be an instance of that type. If it is an identifier,
  752. then that identifier must be bound in the enclosing advice or
  753. pointcut declaration, and so the argument in that position must be an
  754. instance of the type of the identifier (or of any type if the
  755. identifier is typed to Object). If it is the "*" wildcard, then any
  756. argument will match, and if it is the special wildcard "..", then any
  757. number of arguments will match, just like in signature patterns. So the
  758. pointcut
  759. </para>
  760. <programlisting>
  761. args(int, .., String)
  762. </programlisting>
  763. <para>
  764. will pick out all join points where the first argument is an
  765. <literal>int</literal> and the last is a <literal>String</literal>.
  766. </para>
  767. </sect3>
  768. <sect3>
  769. <title>Control flow-based pointcuts</title>
  770. <para>
  771. Some concerns cut across the control flow of the program. The
  772. <literal>cflow</literal> and <literal>cflowbelow</literal> primitive
  773. pointcut designators capture join points based on control flow.
  774. </para>
  775. <itemizedlist>
  776. <listitem><literal>cflow(<replaceable>Pointcut</replaceable>)</literal></listitem>
  777. <listitem><literal>cflowbelow(<replaceable>Pointcut</replaceable>)</literal></listitem>
  778. </itemizedlist>
  779. <para>
  780. The <literal>cflow</literal> pointcut picks out all join points that
  781. occur between entry and exit of each join point
  782. <replaceable>P</replaceable> picked out by
  783. <replaceable>Pointcut</replaceable>, including
  784. <replaceable>P</replaceable> itself. Hence, it picks out the join
  785. points <emphasis>in</emphasis> the control flow of the join points
  786. picked out by <replaceable>Pointcut</replaceable>.
  787. </para>
  788. <para>
  789. The <literal>cflowbelow</literal> pointcut picks out all join points
  790. that occur between entry and exit of each join point
  791. <replaceable>P</replaceable> picked out by
  792. <replaceable>Pointcut</replaceable>, but not including
  793. <replaceable>P</replaceable> itself. Hence, it picks out the join
  794. points <emphasis>below</emphasis> the control flow of the join points
  795. picked out by <replaceable>Pointcut</replaceable>.
  796. </para>
  797. </sect3>
  798. <sect3>
  799. <title>Program text-based pointcuts</title>
  800. <para>
  801. While many concerns cut across the runtime structure of the program,
  802. some must deal with the lexical structure. AspectJ allows aspects to
  803. pick out join points based on where their associated code is defined.
  804. </para>
  805. <itemizedlist>
  806. <listitem><literal>within(<replaceable>TypePattern</replaceable>)</literal></listitem>
  807. <listitem><literal>withincode(<replaceable>MethodPattern</replaceable>)</literal></listitem>
  808. <listitem><literal>withincode(<replaceable>ConstructorPattern</replaceable>)</literal></listitem>
  809. </itemizedlist>
  810. <para>
  811. The <literal>within</literal> pointcut picks out each join point
  812. where the code executing is defined in the declaration of one of the
  813. types in <replaceable>TypePattern</replaceable>. This includes the
  814. class initialization, object initialization, and method and
  815. constructor execution join points for the type, as well as any join
  816. points associated with the statements and expressions of the type.
  817. It also includes any join points that are associated with code in a
  818. type's nested types, and that type's default constructor, if there is
  819. one.
  820. </para>
  821. <para>
  822. The <literal>withincode</literal> pointcuts picks out each join point
  823. where the code executing is defined in the declaration of a
  824. particular method or constructor. This includes the method or
  825. constructor execution join point as well as any join points
  826. associated with the statements and expressions of the method or
  827. constructor. It also includes any join points that are associated
  828. with code in a method or constructor's local or anonymous types.
  829. </para>
  830. </sect3>
  831. <sect3>
  832. <title>Expression-based pointcuts</title>
  833. <itemizedlist>
  834. <listitem><literal>if(<replaceable>BooleanExpression</replaceable>)</literal></listitem>
  835. </itemizedlist>
  836. <para>
  837. The if pointcut picks out join points based on a dynamic property.
  838. It's syntax takes an expression, which must evaluate to a boolean
  839. true or false. Within this expression, the
  840. <literal>thisJoinPoint</literal> object is available. So one
  841. (extremely inefficient) way of picking out all call join points would
  842. be to use the pointcut
  843. </para>
  844. <programlisting>
  845. if(thisJoinPoint.getKind().equals("call"))
  846. </programlisting>
  847. <para>
  848. Note that the order of evaluation for pointcut expression
  849. components at a join point is undefined. Writing <literal>if</literal>
  850. pointcuts that have side-effects is considered bad style and may also
  851. lead to potentially confusing or even changing behavior with regard
  852. to when or if the test code will run.
  853. </para>
  854. </sect3>
  855. </sect2>
  856. <sect2>
  857. <title>Signatures</title>
  858. <para>
  859. One very important property of a join point is its signature, which is
  860. used by many of AspectJ's pointcut designators to select particular
  861. join points.
  862. </para>
  863. <sect3>
  864. <title>Methods</title>
  865. <para>
  866. Join points associated with methods typically have method signatures,
  867. consisting of a method name, parameter types, return type, the types of
  868. the declared (checked) exceptions, and some type that the method could
  869. be called on (below called the "qualifying type").
  870. </para>
  871. <para>
  872. At a method call join point, the signature is a method signature whose
  873. qualifying type is the static type used to <emphasis>access</emphasis>
  874. the method. This means that the signature for the join point created
  875. from the call <literal>((Integer)i).toString()</literal> is different
  876. than that for the call <literal>((Object)i).toString()</literal>, even
  877. if <literal>i</literal> is the same variable.
  878. </para>
  879. <para>
  880. At a method execution join point, the signature a method signature
  881. whose qualifying type is the declaring type of the method.
  882. </para>
  883. </sect3>
  884. <sect3>
  885. <title>Fields</title>
  886. <para>
  887. Join points associated with fields typically have field signatures,
  888. consisting of a field name and a field type. A field reference join
  889. point has such a signature, and no parameters. A field set join point
  890. has such a signature, but has a has a single parameter whose type is
  891. the same as the field type.
  892. </para>
  893. </sect3>
  894. <sect3>
  895. <title>Constructors</title>
  896. <para>
  897. Join points associated with constructors typically have constructor
  898. signatures, consisting of a parameter types, the types of the declared
  899. (checked) exceptions, and the declaring type.
  900. </para>
  901. <para>
  902. At a constructor call join point, the signature is the constructor
  903. signature of the called constructor. At a constructor execution join
  904. point, the signature is the constructor signature of the currently
  905. executing constructor.
  906. </para>
  907. <para>
  908. At object initialization and pre-initialization join points, the
  909. signature is the constructor signature for the constructor that started
  910. this initialization: the first constructor entered during this type's
  911. initialization of this object.
  912. </para>
  913. </sect3>
  914. <sect3>
  915. <title>Others</title>
  916. <para>
  917. At a handler execution join point, the signature is composed of the
  918. exception type that the handler handles.
  919. </para>
  920. <para>
  921. At an advice execution join point, the signature is composed of the
  922. aspect type, the parameter types of the advice, the return type (void
  923. for all but around advice) and the types of the declared (checked)
  924. exceptions.
  925. </para>
  926. </sect3>
  927. </sect2>
  928. <!-- ============================== -->
  929. <sect2>
  930. <title>Matching</title>
  931. <para>
  932. The <literal>withincode</literal>, <literal>call</literal>,
  933. <literal>execution</literal>, <literal>get</literal>, and
  934. <literal>set</literal> primitive pointcut designators all use signature
  935. patterns to determine the join points they describe. A signature
  936. pattern is an abstract description of one or more join-point
  937. signatures. Signature patterns are intended to match very closely the
  938. same kind of things one would write when defining individual methods
  939. and constructors.
  940. </para>
  941. <para>
  942. Method definitions in Java include method names, method parameters,
  943. return types, modifiers like static or private, and throws clauses,
  944. while constructor definitions omit the return type and replace the
  945. method name with the class name. The start of a particular method
  946. definition, in class <literal>Test</literal>, for example, might be
  947. </para>
  948. <programlisting>
  949. class C {
  950. public final void foo() throws ArrayOutOfBoundsException { ... }
  951. }
  952. </programlisting>
  953. <para>
  954. In AspectJ, method signature patterns have all these, but most elements
  955. can be replaced by wildcards. So
  956. </para>
  957. <programlisting>
  958. call(public final void C.foo() throws ArrayOutOfBoundsException)
  959. </programlisting>
  960. <para>
  961. picks out call join points to that method, and the pointcut
  962. </para>
  963. <programlisting>
  964. call(public final void *.*() throws ArrayOutOfBoundsException)
  965. </programlisting>
  966. <para>
  967. picks out all call join points to methods, regardless of their name
  968. name or which class they are defined on, so long as they take no
  969. arguments, return no value, are both <literal>public</literal> and
  970. <literal>final</literal>, and are declared to throw
  971. <literal>ArrayOutOfBounds</literal> exceptions.
  972. </para>
  973. <para>
  974. The defining type name, if not present, defaults to *, so another way
  975. of writing that pointcut would be
  976. </para>
  977. <programlisting>
  978. call(public final void *() throws ArrayOutOfBoundsException)
  979. </programlisting>
  980. <para>
  981. Formal parameter lists can use the wildcard <literal>..</literal> to
  982. indicate zero or more arguments, so
  983. </para>
  984. <programlisting>
  985. execution(void m(..))
  986. </programlisting>
  987. <para>
  988. picks out execution join points for void methods named
  989. <literal>m</literal>, of any number of arguments, while
  990. </para>
  991. <programlisting>
  992. execution(void m(.., int))
  993. </programlisting>
  994. <para>
  995. picks out execution join points for void methods named
  996. <literal>m</literal> whose last parameter is of type
  997. <literal>int</literal>.
  998. </para>
  999. <para>
  1000. The modifiers also form part of the signature pattern. If an AspectJ
  1001. signature pattern should match methods without a particular modifier,
  1002. such as all non-public methods, the appropriate modifier should be
  1003. negated with the <literal>!</literal> operator. So,
  1004. </para>
  1005. <programlisting>
  1006. withincode(!public void foo())
  1007. </programlisting>
  1008. <para>
  1009. picks out all join points associated with code in null non-public
  1010. void methods named <literal>foo</literal>, while
  1011. </para>
  1012. <programlisting>
  1013. withincode(void foo())
  1014. </programlisting>
  1015. <para>
  1016. picks out all join points associated with code in null void methods
  1017. named <literal>foo</literal>, regardless of access modifier.
  1018. </para>
  1019. <para>
  1020. Method names may contain the * wildcard, indicating any number of
  1021. characters in the method name. So
  1022. </para>
  1023. <programlisting>
  1024. call(int *())
  1025. </programlisting>
  1026. <para>
  1027. picks out all call join points to <literal>int</literal> methods
  1028. regardless of name, but
  1029. </para>
  1030. <programlisting>
  1031. call(int get*())
  1032. </programlisting>
  1033. <para>
  1034. picks out all call join points to <literal>int</literal> methods
  1035. where the method name starts with the characters "get".
  1036. </para>
  1037. <para>
  1038. AspectJ uses the <literal>new</literal> keyword for constructor
  1039. signature patterns rather than using a particular class name. So the
  1040. execution join points of private null constructor of a class C
  1041. defined to throw an ArithmeticException can be picked out with
  1042. </para>
  1043. <programlisting>
  1044. execution(private C.new() throws ArithmeticException)
  1045. </programlisting>
  1046. <sect3>
  1047. <title>Matching based on the throws clause</title>
  1048. <para>
  1049. Type patterns may be used to pick out methods and constructors
  1050. based on their throws clauses. This allows the following two
  1051. kinds of extremely wildcarded pointcuts:
  1052. </para>
  1053. <programlisting>
  1054. pointcut throwsMathlike():
  1055. // each call to a method with a throws clause containing at least
  1056. // one exception exception with "Math" in its name.
  1057. call(* *(..) throws *..*Math*);
  1058. pointcut doesNotThrowMathlike():
  1059. // each call to a method with a throws clause containing no
  1060. // exceptions with "Math" in its name.
  1061. call(* *(..) throws !*..*Math*);
  1062. </programlisting>
  1063. <para>
  1064. A <replaceable>ThrowsClausePattern</replaceable> is a comma-separated list of
  1065. <replaceable>ThrowsClausePatternItem</replaceable>s, where
  1066. <variablelist>
  1067. <varlistentry>
  1068. <term><replaceable>ThrowsClausePatternItem</replaceable> :</term>
  1069. <listitem>
  1070. <literal>[ ! ]
  1071. <replaceable>TypeNamePattern</replaceable></literal>
  1072. </listitem>
  1073. </varlistentry>
  1074. </variablelist>
  1075. </para>
  1076. <para>
  1077. A <replaceable>ThrowsClausePattern</replaceable> matches the
  1078. throws clause of any code member signature. To match, each
  1079. <literal>ThrowsClausePatternItem</literal> must
  1080. match the throws clause of the member in question. If any item
  1081. doesn't match, then the whole pattern doesn't match.
  1082. </para>
  1083. <para>
  1084. If a ThrowsClausePatternItem begins with "!", then it matches a
  1085. particular throws clause if and only if <emphasis>none</emphasis>
  1086. of the types named in the throws clause is matched by the
  1087. <literal>TypeNamePattern</literal>.
  1088. </para>
  1089. <para>
  1090. If a <replaceable>ThrowsClausePatternItem</replaceable> does not
  1091. begin with "!", then it matches a throws clause if and only if
  1092. <emphasis>any</emphasis> of the types named in the throws clause
  1093. is matched by the <emphasis>TypeNamePattern</emphasis>.
  1094. </para>
  1095. <para>
  1096. The rule for "!" matching has one potentially surprising
  1097. property, in that these two pointcuts
  1098. <itemizedlist>
  1099. <listitem> call(* *(..) throws !IOException) </listitem>
  1100. <listitem> call(* *(..) throws (!IOException)) </listitem>
  1101. </itemizedlist>
  1102. will match differently on calls to
  1103. <blockquote>
  1104. <literal>
  1105. void m() throws RuntimeException, IOException {}
  1106. </literal>
  1107. </blockquote>
  1108. </para>
  1109. <para>
  1110. [1] will NOT match the method m(), because method m's throws
  1111. clause declares that it throws IOException. [2] WILL match the
  1112. method m(), because method m's throws clause declares the it
  1113. throws some exception which does not match IOException,
  1114. i.e. RuntimeException.
  1115. </para>
  1116. </sect3>
  1117. </sect2>
  1118. <sect2>
  1119. <title>Type patterns</title>
  1120. <para>
  1121. Type patterns are a way to pick out collections of types and use them
  1122. in places where you would otherwise use only one type. The rules for
  1123. using type patterns are simple.
  1124. </para>
  1125. <sect3>
  1126. <title>Type name patterns</title>
  1127. <para>
  1128. First, all type names are also type patterns. So
  1129. <literal>Object</literal>, <literal>java.util.HashMap</literal>,
  1130. <literal>Map.Entry</literal>, <literal>int</literal> are all type
  1131. patterns.
  1132. </para>
  1133. <para>
  1134. There is a special type name, *, which is also a type pattern. * picks out all
  1135. types, including primitive types. So
  1136. </para>
  1137. <programlisting>
  1138. call(void foo(*))
  1139. </programlisting>
  1140. <para>
  1141. picks out all call join points to void methods named foo, taking one
  1142. argument of any type.
  1143. </para>
  1144. <para>
  1145. Type names that contain the two wildcards "*" and
  1146. "<literal>..</literal>" are also type patterns. The * wildcard matches
  1147. zero or more characters characters except for ".", so it can be used
  1148. when types have a certain naming convention. So
  1149. </para>
  1150. <programlisting>
  1151. handler(java.util.*Map)
  1152. </programlisting>
  1153. <para>
  1154. picks out the types java.util.Map and java.util.java.util.HashMap,
  1155. among others, and
  1156. </para>
  1157. <programlisting>
  1158. handler(java.util.*)
  1159. </programlisting>
  1160. <para>
  1161. picks out all types that start with "<literal>java.util.</literal>" and
  1162. don't have any more "."s, that is, the types in the
  1163. <literal>java.util</literal> package, but not inner types
  1164. (such as java.util.Map.Entry).
  1165. </para>
  1166. <para>
  1167. The "<literal>..</literal>" wildcard matches any sequence of
  1168. characters that start and end with a ".", so it can be used
  1169. to pick out all types in any subpackage, or all inner types. So
  1170. </para>
  1171. <programlisting>
  1172. within(com.xerox..*)
  1173. </programlisting>
  1174. <para>
  1175. picks out all join points where the code is in any type
  1176. definition of a type whose name begins with "<literal>com.xerox.</literal>".
  1177. </para>
  1178. </sect3>
  1179. <sect3>
  1180. <title>Subtype patterns</title>
  1181. <para>
  1182. It is possible to pick out all subtypes of a type (or a collection of
  1183. types) with the "+" wildcard. The "+" wildcard follows immediately a
  1184. type name pattern. So, while
  1185. </para>
  1186. <programlisting>
  1187. call(Foo.new())
  1188. </programlisting>
  1189. <para>
  1190. picks out all constructor call join points where an instance of exactly
  1191. type Foo is constructed,
  1192. </para>
  1193. <programlisting>
  1194. call(Foo+.new())
  1195. </programlisting>
  1196. <para>
  1197. picks out all constructor call join points where an instance of any
  1198. subtype of Foo (including Foo itself) is constructed, and the unlikely
  1199. </para>
  1200. <programlisting>
  1201. call(*Handler+.new())
  1202. </programlisting>
  1203. <para>
  1204. picks out all constructor call join points where an instance of any
  1205. subtype of any type whose name ends in "Handler" is constructed.
  1206. </para>
  1207. </sect3>
  1208. <sect3>
  1209. <title>Array type patterns</title>
  1210. <para>
  1211. A type name pattern or subtype pattern can be followed by one or more
  1212. sets of square brackets to make array type patterns. So
  1213. <literal>Object[]</literal> is an array type pattern, and so is
  1214. <literal>com.xerox..*[][]</literal>, and so is
  1215. <literal>Object+[]</literal>.
  1216. </para>
  1217. </sect3>
  1218. <sect3>
  1219. <title>Type patterns</title>
  1220. <para>
  1221. Type patterns are built up out of type name patterns, subtype patterns,
  1222. and array type patterns, and constructed with boolean operators
  1223. <literal><![CDATA[&&]]></literal>, <literal>||</literal>, and
  1224. <literal>!</literal>. So
  1225. </para>
  1226. <programlisting>
  1227. staticinitialization(Foo || Bar)
  1228. </programlisting>
  1229. <para>
  1230. picks out the static initializer execution join points of either Foo or Bar,
  1231. and
  1232. </para>
  1233. <programlisting>
  1234. call((Foo+ <![CDATA[&&]]> ! Foo).new(..))
  1235. </programlisting>
  1236. <para>
  1237. picks out the constructor call join points when a subtype of Foo, but
  1238. not Foo itself, is constructed.
  1239. </para>
  1240. </sect3>
  1241. </sect2>
  1242. <sect2>
  1243. <title>Pattern Summary</title>
  1244. <para>
  1245. Here is a summary of the pattern syntax used in AspectJ:
  1246. </para>
  1247. <programlisting>
  1248. MethodPattern =
  1249. [ModifiersPattern] TypePattern
  1250. [TypePattern . ] IdPattern (TypePattern | ".." , ... )
  1251. [ throws ThrowsPattern ]
  1252. ConstructorPattern =
  1253. [ModifiersPattern ]
  1254. [TypePattern . ] new (TypePattern | ".." , ...)
  1255. [ throws ThrowsPattern ]
  1256. FieldPattern =
  1257. [ModifiersPattern] TypePattern [TypePattern . ] IdPattern
  1258. ThrowsPattern =
  1259. [ ! ] TypePattern , ...
  1260. TypePattern =
  1261. IdPattern [ + ] [ [] ... ]
  1262. | ! TypePattern
  1263. | TypePattern <![CDATA[&&]]> TypePattern
  1264. | TypePattern || TypePattern
  1265. | ( TypePattern )
  1266. IdPattern =
  1267. Sequence of characters, possibly with special * and .. wildcards
  1268. ModifiersPattern =
  1269. [ ! ] JavaModifier ...
  1270. </programlisting>
  1271. </sect2>
  1272. </sect1>
  1273. <!-- ============================== -->
  1274. <sect1 id="semantics-advice">
  1275. <title>Advice</title>
  1276. <para>
  1277. Each piece of advice is of the form
  1278. <blockquote>
  1279. <literal>[ strictfp ] <replaceable>AdviceSpec</replaceable> [
  1280. throws <replaceable>TypeList</replaceable> ] :
  1281. <replaceable>Pointcut</replaceable> {
  1282. <replaceable>Body</replaceable> } </literal>
  1283. </blockquote>
  1284. where <replaceable>AdviceSpec</replaceable> is one of
  1285. </para>
  1286. <itemizedlist>
  1287. <listitem>
  1288. <literal>before( <replaceable>Formals</replaceable> ) </literal>
  1289. </listitem>
  1290. <listitem>
  1291. <literal>after( <replaceable>Formals</replaceable> ) returning
  1292. [ ( <replaceable>Formal</replaceable> ) ] </literal>
  1293. </listitem>
  1294. <listitem>
  1295. <literal>after( <replaceable>Formals</replaceable> ) throwing [
  1296. ( <replaceable>Formal</replaceable> ) ] </literal>
  1297. </listitem>
  1298. <listitem>
  1299. <literal>after( <replaceable>Formals</replaceable> ) </literal>
  1300. </listitem>
  1301. <listitem>
  1302. <literal><replaceable>Type</replaceable>
  1303. around( <replaceable>Formals</replaceable> )</literal>
  1304. </listitem>
  1305. </itemizedlist>
  1306. <para>
  1307. Advice defines crosscutting behavior. It is defined in terms of
  1308. pointcuts. The code of a piece of advice runs at every join point
  1309. picked out by its pointcut. Exactly how the code runs depends on the
  1310. kind of advice.
  1311. </para>
  1312. <para>
  1313. AspectJ supports three kinds of advice. The kind of advice determines how
  1314. it interacts with the join points it is defined over. Thus AspectJ
  1315. divides advice into that which runs before its join points, that which
  1316. runs after its join points, and that which runs in place of (or "around")
  1317. its join points.
  1318. </para>
  1319. <para>
  1320. While before advice is relatively unproblematic, there can be three
  1321. interpretations of after advice: After the execution of a join point
  1322. completes normally, after it throws an exception, or after it does either
  1323. one. AspectJ allows after advice for any of these situations.
  1324. </para>
  1325. <programlisting>
  1326. aspect A {
  1327. pointcut publicCall(): call(public Object *(..));
  1328. after() returning (Object o): publicCall() {
  1329. System.out.println("Returned normally with " + o);
  1330. }
  1331. after() throwing (Exception e): publicCall() {
  1332. System.out.println("Threw an exception: " + e);
  1333. }
  1334. after(): publicCall(){
  1335. System.out.println("Returned or threw an Exception");
  1336. }
  1337. }
  1338. </programlisting>
  1339. <para>
  1340. After returning advice may not care about its returned object, in which
  1341. case it may be written
  1342. </para>
  1343. <programlisting>
  1344. after() returning: call(public Object *(..)) {
  1345. System.out.println("Returned normally");
  1346. }
  1347. </programlisting>
  1348. <para>
  1349. If after returning does expose its returned object, then the
  1350. type of the parameter is considered to be an
  1351. <literal>instanceof</literal>-like constraint on the advice: it
  1352. will run only when the return value is of the appropriate type.
  1353. </para>
  1354. <para>
  1355. A value is of the appropriate type if it would be assignable to
  1356. a variable of that type, in the Java sense. That is, a
  1357. <literal>byte</literal> value is assignable to a
  1358. <literal>short</literal> parameter but not vice-versa, an
  1359. <literal>int</literal> is assignable to a
  1360. <literal>float</literal> parameter, <literal>boolean</literal>
  1361. values are only assignable to <literal>boolean</literal>
  1362. parameters, and reference types work by instanceof.
  1363. </para>
  1364. <para>
  1365. There are two special cases: If the exposed value is typed to
  1366. <literal>Object</literal>, then the advice is not constrained by
  1367. that type: the actual return value is converted to an object
  1368. type for the body of the advice: <literal>int</literal> values
  1369. are represented as <literal>java.lang.Integer</literal> objects,
  1370. etc, and no value (from void methods, for example) is
  1371. represented as <literal>null</literal>.
  1372. </para>
  1373. <para>
  1374. Secondly, the <literal>null</literal> value is assignable to a
  1375. parameter <literal>T</literal> if the join point
  1376. <emphasis>could</emphasis> return something of type
  1377. <literal>T</literal>.
  1378. </para>
  1379. <para>
  1380. Around advice runs in place of the join point it operates over, rather
  1381. than before or after it. Because around is allowed to return a value, it
  1382. must be declared with a return type, like a method.
  1383. </para>
  1384. <para>
  1385. Thus, a simple use of around advice is to make a particular method
  1386. constant:
  1387. </para>
  1388. <programlisting>
  1389. aspect A {
  1390. int around(): call(int C.foo()) {
  1391. return 3;
  1392. }
  1393. }
  1394. </programlisting>
  1395. <para>
  1396. Within the body of around advice, though, the computation of the original
  1397. join point can be executed with the special syntax
  1398. </para>
  1399. <programlisting>
  1400. proceed( ... )
  1401. </programlisting>
  1402. <para>
  1403. The proceed form takes as arguments the context exposed by the around's
  1404. pointcut, and returns whatever the around is declared to return. So the
  1405. following around advice will double the second argument to
  1406. <literal>foo</literal> whenever it is called, and then halve its result:
  1407. </para>
  1408. <programlisting>
  1409. aspect A {
  1410. int around(int i): call(int C.foo(Object, int)) <![CDATA[&&]]> args(i) {
  1411. int newi = proceed(i*2)
  1412. return newi/2;
  1413. }
  1414. }
  1415. </programlisting>
  1416. <para>
  1417. If the return value of around advice is typed to
  1418. <literal>Object</literal>, then the result of proceed is converted to an
  1419. object representation, even if it is originally a primitive value. And
  1420. when the advice returns an Object value, that value is converted back to
  1421. whatever representation it was originally. So another way to write the
  1422. doubling and halving advice is:
  1423. </para>
  1424. <programlisting>
  1425. aspect A {
  1426. Object around(int i): call(int C.foo(Object, int)) <![CDATA[&&]]> args(i) {
  1427. Integer newi = (Integer) proceed(i*2)
  1428. return new Integer(newi.intValue() / 2);
  1429. }
  1430. }
  1431. </programlisting>
  1432. <para>
  1433. Any occurence of <literal>proceed(..)</literal> within the body of
  1434. around advice is treated as the special proceed form (even if the
  1435. aspect defines a method named <literal>proceed</literal>) unless a
  1436. target other than the aspect instance is specified as the recipient of
  1437. the call.
  1438. For example, in the following program the first
  1439. call to proceed will be treated as a method call to
  1440. the <literal>ICanProceed</literal> instance, whereas the second call to
  1441. proceed is treated as the special proceed form.
  1442. </para>
  1443. <programlisting>
  1444. aspect A {
  1445. Object around(ICanProceed canProceed) : execution(* *(..)) <![CDATA[&&]]> this(canProceed) {
  1446. canProceed.proceed(); // a method call
  1447. return proceed(canProceed); // the special proceed form
  1448. }
  1449. private Object proceed(ICanProceed canProceed) {
  1450. // this method cannot be called from inside the body of around advice in
  1451. // the aspect
  1452. }
  1453. }
  1454. </programlisting>
  1455. <para>
  1456. In all kinds of advice, the parameters of the advice behave exactly like
  1457. method parameters. In particular, assigning to any parameter affects
  1458. only the value of the parameter, not the value that it came from. This
  1459. means that
  1460. </para>
  1461. <programlisting>
  1462. aspect A {
  1463. after() returning (int i): call(int C.foo()) {
  1464. i = i * 2;
  1465. }
  1466. }
  1467. </programlisting>
  1468. <para>
  1469. will <emphasis>not</emphasis> double the returned value of the advice.
  1470. Rather, it will double the local parameter. Changing the values of
  1471. parameters or return values of join points can be done by using around
  1472. advice.
  1473. </para>
  1474. <sect2>
  1475. <title>Advice modifiers</title>
  1476. <para>
  1477. The <literal>strictfp</literal> modifier is the only modifier allowed
  1478. on advice, and it has the effect of making all floating-point
  1479. expressions within the advice be FP-strict.
  1480. </para>
  1481. </sect2>
  1482. <sect2>
  1483. <title>Advice and checked exceptions</title>
  1484. <para>
  1485. An advice declaration must include a <literal>throws</literal> clause
  1486. listing the checked exceptions the body may throw. This list of
  1487. checked exceptions must be compatible with each target join point
  1488. of the advice, or an error is signalled by the compiler.
  1489. </para>
  1490. <para>
  1491. For example, in the following declarations:
  1492. </para>
  1493. <programlisting>
  1494. import java.io.FileNotFoundException;
  1495. class C {
  1496. int i;
  1497. int getI() { return i; }
  1498. }
  1499. aspect A {
  1500. before(): get(int C.i) {
  1501. throw new FileNotFoundException();
  1502. }
  1503. before() throws FileNotFoundException: get(int C.i) {
  1504. throw new FileNotFoundException();
  1505. }
  1506. }
  1507. </programlisting>
  1508. <para>
  1509. both pieces of advice are illegal. The first because the body throws
  1510. an undeclared checked exception, and the second because field get join
  1511. points cannot throw <literal>FileNotFoundException</literal>s.
  1512. </para>
  1513. <para> The exceptions that each kind of join point in AspectJ may throw are:
  1514. </para>
  1515. <variablelist>
  1516. <varlistentry>
  1517. <term>method call and execution</term>
  1518. <listitem>
  1519. the checked exceptions declared by the target method's
  1520. <literal>throws</literal> clause.
  1521. </listitem>
  1522. </varlistentry>
  1523. <varlistentry>
  1524. <term>constructor call and execution</term>
  1525. <listitem>
  1526. the checked exceptions declared by the target constructor's
  1527. <literal>throws</literal> clause.
  1528. </listitem>
  1529. </varlistentry>
  1530. <varlistentry>
  1531. <term>field get and set</term>
  1532. <listitem>
  1533. no checked exceptions can be thrown from these join points.
  1534. </listitem>
  1535. </varlistentry>
  1536. <varlistentry>
  1537. <term>exception handler execution</term>
  1538. <listitem>
  1539. the exceptions that can be thrown by the target exception handler.
  1540. </listitem>
  1541. </varlistentry>
  1542. <varlistentry>
  1543. <term>static initializer execution</term>
  1544. <listitem>
  1545. no checked exceptions can be thrown from these join points.
  1546. </listitem>
  1547. </varlistentry>
  1548. <varlistentry>
  1549. <term>pre-initialization and initialization</term>
  1550. <listitem>
  1551. any exception that is in the throws clause of
  1552. <emphasis>all</emphasis> constructors of the initialized class.
  1553. </listitem>
  1554. </varlistentry>
  1555. <varlistentry>
  1556. <term>advice execution</term>
  1557. <listitem>
  1558. any exception that is in the throws clause of the advice.
  1559. </listitem>
  1560. </varlistentry>
  1561. </variablelist>
  1562. </sect2>
  1563. <sect2>
  1564. <title>Advice precedence</title>
  1565. <para>
  1566. Multiple pieces of advice may apply to the same join point. In such
  1567. cases, the resolution order of the advice is based on advice
  1568. precedence.
  1569. </para>
  1570. <sect3>
  1571. <title>Determining precedence</title>
  1572. <para>There are a number of rules that determine whether a particular
  1573. piece of advice has precedence over another when they advise the same
  1574. join point. </para>
  1575. <para>If the two pieces of advice are defined in different aspects,
  1576. then there are three cases: </para>
  1577. <itemizedlist>
  1578. <listitem>If aspect A is matched earlier than aspect B in some
  1579. <literal>declare precedence</literal> form, then all advice in
  1580. concrete aspect A has precedence over all advice in concrete aspect B
  1581. when they are on the same join point. </listitem>
  1582. <listitem>
  1583. Otherwise, if aspect A is a subaspect of aspect B, then all advice
  1584. defined in A has precedence over all advice defined in
  1585. B. So, unless otherwise specified with
  1586. <literal>declare precedence</literal>, advice in a subaspect
  1587. has precedence over advice in a superaspect.
  1588. </listitem>
  1589. <listitem>
  1590. Otherwise, if two pieces of advice are defined in two different
  1591. aspects, it is undefined which one has precedence.
  1592. </listitem>
  1593. </itemizedlist>
  1594. <para>If the two pieces of advice are defined in the same aspect, then
  1595. there are two cases: </para>
  1596. <itemizedlist>
  1597. <listitem>If either are <literal>after</literal> advice, then the one that
  1598. appears later in the aspect has precedence over the one that appears
  1599. earlier. </listitem>
  1600. <listitem>Otherwise, then the one that appears earlier in the aspect
  1601. has precedence over the one that appears later.
  1602. </listitem>
  1603. </itemizedlist>
  1604. <para>These rules can lead to circularity, such as</para>
  1605. <programlisting>
  1606. aspect A {
  1607. before(): execution(void main(String[] args)) {}
  1608. after(): execution(void main(String[] args)) {}
  1609. before(): execution(void main(String[] args)) {}
  1610. }
  1611. </programlisting>
  1612. <para>such circularities will result in errors signalled by the compiler. </para>
  1613. </sect3>
  1614. <sect3>
  1615. <title>Effects of precedence</title>
  1616. <para>At a particular join point, advice is ordered by precedence.</para>
  1617. <para>A piece of <literal>around</literal> advice controls whether
  1618. advice of lower precedence will run by calling
  1619. <literal>proceed</literal>. The call to <literal>proceed</literal>
  1620. will run the advice with next precedence, or the computation under the
  1621. join point if there is no further advice. </para>
  1622. <para>A piece of <literal>before</literal> advice can prevent advice of
  1623. lower precedence from running by throwing an exception. If it returns
  1624. normally, however, then the advice of the next precedence, or the
  1625. computation under the join pint if there is no further advice, will run.
  1626. </para>
  1627. <para>Running <literal>after returning</literal> advice will run the
  1628. advice of next precedence, or the computation under the join point if
  1629. there is no further advice. Then, if that computation returned
  1630. normally, the body of the advice will run. </para>
  1631. <para>Running <literal>after throwing</literal> advice will run the
  1632. advice of next precedence, or the computation under the join
  1633. point if there is no further advice. Then, if that computation threw
  1634. an exception of an appropriate type, the body of the advice will
  1635. run. </para>
  1636. <para>Running <literal>after</literal> advice will run the advice of
  1637. next precedence, or the computation under the join point if
  1638. there is no further advice. Then the body of the advice will
  1639. run. </para>
  1640. </sect3>
  1641. </sect2>
  1642. <sect2>
  1643. <title>Reflective access to the join point</title>
  1644. <para>
  1645. Three special variables are visible within bodies of advice:
  1646. <literal>thisJoinPoint</literal>,
  1647. <literal>thisJoinPointStaticPart</literal>, and
  1648. <literal>thisEnclosingJoinPointStaticPart</literal>. Each is bound to
  1649. an object that encapsulates some of the context of the advice's current
  1650. or enclosing join point. These variables exist because some pointcuts
  1651. may pick out very large collections of join points. For example, the
  1652. pointcut
  1653. </para>
  1654. <programlisting>
  1655. pointcut publicCall(): call(public * *(..));
  1656. </programlisting>
  1657. <para>
  1658. picks out calls to many methods. Yet the body of advice over this
  1659. pointcut may wish to have access to the method name or parameters of a
  1660. particular join point.
  1661. </para>
  1662. <para>
  1663. <literal>thisJoinPoint</literal> is bound to a complete join point
  1664. object.
  1665. </para>
  1666. <para>
  1667. <literal>thisJoinPointStaticPart</literal> is bound to a part of the
  1668. join point object that includes less information, but for which no
  1669. memory allocation is required on each execution of the advice. It is
  1670. equivalent to <literal>thisJoinPoint.getStaticPart()</literal>.
  1671. </para>
  1672. <para>
  1673. <literal>thisEnclosingJoinPointStaticPart</literal> is bound to the
  1674. static part of the join point enclosing the current join point. Only
  1675. the static part of this enclosing join point is available through this
  1676. mechanism.
  1677. </para>
  1678. <para>
  1679. Standard Java reflection uses objects from the
  1680. <literal>java.lang.reflect</literal> hierarchy to build up its
  1681. reflective objects. Similarly, AspectJ join point objects have types
  1682. in a type hierarchy. The type of objects bound to
  1683. <literal>thisJoinPoint</literal> is
  1684. <literal>org.aspectj.lang.JoinPoint</literal>, while
  1685. <literal>thisStaticJoinPoint</literal> is bound to objects of interface
  1686. type <literal>org.aspectj.lang.JoinPoint.StaticPart</literal>.
  1687. </para>
  1688. </sect2>
  1689. </sect1>
  1690. <sect1 id="semantics-declare">
  1691. <title>Static crosscutting</title>
  1692. <para>
  1693. Advice declarations change the behavior of classes they crosscut, but do
  1694. not change their static type structure. For crosscutting concerns that do
  1695. operate over the static structure of type hierarchies, AspectJ provides
  1696. inter-type member declarations and other <literal>declare</literal> forms.
  1697. </para>
  1698. <sect2>
  1699. <title>Inter-type member declarations</title>
  1700. <para>
  1701. AspectJ allows the declaration of members by aspects that are
  1702. associated with other types.
  1703. </para>
  1704. <para>
  1705. An inter-type method declaration looks like
  1706. </para>
  1707. <itemizedlist>
  1708. <listitem><literal>
  1709. [ <replaceable>Modifiers</replaceable> ]
  1710. <replaceable>Type</replaceable> <replaceable>OnType</replaceable>
  1711. .
  1712. <replaceable>Id</replaceable>(<replaceable>Formals</replaceable>)
  1713. [ <replaceable>ThrowsClause</replaceable> ]
  1714. { <replaceable>Body</replaceable> }</literal></listitem>
  1715. <listitem><literal>abstract
  1716. [ <replaceable>Modifiers</replaceable> ]
  1717. <replaceable>Type</replaceable> <replaceable>OnType</replaceable>
  1718. . <replaceable>Id</replaceable>(<replaceable>Formals</replaceable>)
  1719. [ <replaceable>ThrowsClause</replaceable> ]
  1720. ;
  1721. </literal></listitem>
  1722. </itemizedlist>
  1723. <para>
  1724. The effect of such a declaration is to make <replaceable>OnType</replaceable>
  1725. support the new method. Even if <replaceable>OnType</replaceable> is
  1726. an interface. Even if the method is neither public nor abstract. So the
  1727. following is legal AspectJ code:
  1728. </para>
  1729. <programlisting>
  1730. interface Iface {}
  1731. aspect A {
  1732. private void Iface.m() {
  1733. System.err.println("I'm a private method on an interface");
  1734. }
  1735. void worksOnI(Iface iface) {
  1736. // calling a private method on an interface
  1737. iface.m();
  1738. }
  1739. }
  1740. </programlisting>
  1741. <para>
  1742. An inter-type constructor declaration looks like
  1743. </para>
  1744. <itemizedlist>
  1745. <listitem><literal>
  1746. [ <replaceable>Modifiers</replaceable> ]
  1747. <replaceable>OnType</replaceable> . new (
  1748. <replaceable>Formals</replaceable> )
  1749. [ <replaceable>ThrowsClause</replaceable> ]
  1750. { <replaceable>Body</replaceable> }</literal></listitem>
  1751. </itemizedlist>
  1752. <para>
  1753. The effect of such a declaration is to make
  1754. <replaceable>OnType</replaceable> support the new constructor. It is
  1755. an error for <replaceable>OnType</replaceable> to be an interface.
  1756. </para>
  1757. <para>
  1758. Inter-type declared constructors cannot be used to assign a
  1759. value to a final variable declared in <replaceable>OnType</replaceable>.
  1760. This limitation significantly increases the ability to both understand
  1761. and compile the <replaceable>OnType</replaceable> class and the
  1762. declaring aspect separately.
  1763. </para>
  1764. <para>
  1765. Note that in the Java language, classes that define no constructors
  1766. have an implicit no-argument constructor that just calls
  1767. <literal>super()</literal>. This means that attempting to declare
  1768. a no-argument inter-type constructor on such a class may result in
  1769. a conflict, even though it <emphasis>looks</emphasis> like no
  1770. constructor is defined.
  1771. </para>
  1772. <para>
  1773. An inter-type field declaration looks like one of
  1774. </para>
  1775. <itemizedlist>
  1776. <listitem><literal>
  1777. [ <replaceable>Modifiers</replaceable> ]
  1778. <replaceable>Type</replaceable>
  1779. <replaceable>OnType</replaceable> . <replaceable>Id</replaceable>
  1780. = <replaceable>Expression</replaceable>;</literal></listitem>
  1781. <listitem><literal>
  1782. [ <replaceable>Modifiers</replaceable> ]
  1783. <replaceable>Type</replaceable>
  1784. <replaceable>OnType</replaceable> . <replaceable>Id</replaceable>;</literal></listitem>
  1785. </itemizedlist>
  1786. <para>
  1787. The effect of such a declaration is to make
  1788. <replaceable>OnType</replaceable> support the new field. Even if
  1789. <replaceable>OnType</replaceable> is an interface. Even if the field is
  1790. neither public, nor static, nor final.
  1791. </para>
  1792. <para>
  1793. The initializer, if any, of an inter-type field definition runs
  1794. before the class-local initializers defined in its target class.
  1795. </para>
  1796. </sect2>
  1797. <para>
  1798. Any occurrence of the identifier <literal>this</literal> in the body of
  1799. an inter-type constructor or method declaration, or in the initializer
  1800. of an inter-type field declaration, refers to the
  1801. <replaceable>OnType</replaceable> object rather than to the aspect
  1802. type; it is an error to access <literal>this</literal> in such a
  1803. position from a <literal>static</literal> inter-type member
  1804. declaration.
  1805. </para>
  1806. <sect2>
  1807. <title>Access modifiers</title>
  1808. <para>
  1809. Inter-type member declarations may be public or private, or have
  1810. default (package-protected) visibility. AspectJ does not provide
  1811. protected inter-type members.
  1812. </para>
  1813. <para>
  1814. The access modifier applies in relation to the aspect, not in relation
  1815. to the target type. So a private inter-type member is visible only from
  1816. code that is defined within the declaring aspect. A default-visibility
  1817. inter-type member is visible only from code that is defined within the
  1818. declaring aspect's package.
  1819. </para>
  1820. <para>
  1821. Note that a declaring a private inter-type method (which AspectJ
  1822. supports) is very different from inserting a private method declaration
  1823. into another class. The former allows access only from the declaring
  1824. aspect, while the latter would allow access only from the target type.
  1825. Java serialization, for example, uses the presense of a private method
  1826. <literal>void writeObject(ObjectOutputStream)</literal> for the
  1827. implementation of <literal>java.io.Serializable</literal>. A private
  1828. inter-type declaration of that method would not fulfill this
  1829. requirement, since it would be private to the aspect, not private to
  1830. the target type.
  1831. </para>
  1832. </sect2>
  1833. <sect2>
  1834. <title>Conflicts</title>
  1835. <para>
  1836. Inter-type declarations raise the possibility of conflicts among
  1837. locally declared members and inter-type members. For example, assuming
  1838. <literal>otherPackage</literal> is not the package containing the
  1839. aspect <classname>A</classname>, the code
  1840. </para>
  1841. <programlisting>
  1842. aspect A {
  1843. private Registry otherPackage.onType.r;
  1844. public void otherPackage.onType.register(Registry r) {
  1845. r.register(this);
  1846. this.r = r;
  1847. }
  1848. }
  1849. </programlisting>
  1850. <para>
  1851. declares that <literal>onType</literal> in <literal>otherPackage</literal> has a field
  1852. <literal>r</literal>. This field, however, is only accessible from the
  1853. code inside of aspect <literal>A</literal>. The aspect also declares
  1854. that <literal>onType</literal> has a method
  1855. "<literal>register</literal>", but makes this method accessible from
  1856. everywhere.
  1857. </para>
  1858. <para>
  1859. If <literal>onType</literal> already defines a
  1860. private or package-protected field "<literal>r</literal>", there is no
  1861. conflict: The aspect cannot see such a field, and no code in
  1862. <literal>otherPackage</literal> can see the inter-type
  1863. "<literal>r</literal>".
  1864. </para>
  1865. <para>
  1866. If <literal>onType</literal> defines a public field
  1867. "<literal>r</literal>", there is a conflict: The expression
  1868. </para>
  1869. <programlisting>
  1870. this.r = r
  1871. </programlisting>
  1872. <para>
  1873. is an error, since it is ambiguous whether the private inter-type
  1874. "<literal>r</literal>" or the public locally-defined
  1875. "<literal>r</literal>" should be used.
  1876. </para>
  1877. <para>
  1878. If <literal>onType</literal> defines a method
  1879. "<literal>register(Registry)</literal>" there is a conflict, since it
  1880. would be ambiguous to any code that could see such a defined method
  1881. which "<literal>register(Registry)</literal>" method was applicable.
  1882. </para>
  1883. <para>
  1884. Conflicts are resolved as much as possible as per Java's conflict
  1885. resolution rules:
  1886. </para>
  1887. <itemizedlist>
  1888. <listitem>A subclass can inherit multiple <emphasis>fields</emphasis> from its superclasses,
  1889. all with the same name and type. However, it is an error to have an ambiguous
  1890. <emphasis>reference</emphasis> to a field.</listitem>
  1891. <listitem>A subclass can only inherit multiple
  1892. <emphasis>methods</emphasis> with the same name and argument types from
  1893. its superclasses if only zero or one of them is concrete (i.e., all but
  1894. one is abstract, or all are abstract).
  1895. </listitem>
  1896. </itemizedlist>
  1897. <para>
  1898. Given a potential conflict between inter-type member declarations in
  1899. different aspects, if one aspect has precedence over the other its
  1900. declaration will take effect without any conflict notice from compiler.
  1901. This is true both when the precedence is declared explicitly with
  1902. <literal>declare precedence</literal> as well as when when sub-aspects
  1903. implicitly have precedence over their super-aspect.
  1904. </para>
  1905. </sect2>
  1906. <sect2>
  1907. <title>Extension and Implementation</title>
  1908. <para>
  1909. An aspect may change the inheritance hierarchy of a system by changing
  1910. the superclass of a type or adding a superinterface onto a type, with
  1911. the <literal>declare parents</literal> form.
  1912. </para>
  1913. <itemizedlist>
  1914. <listitem><literal>declare parents: <replaceable>TypePattern</replaceable> extends <replaceable>Type</replaceable>;</literal></listitem>
  1915. <listitem><literal>declare parents: <replaceable>TypePattern</replaceable> implements <replaceable>TypeList</replaceable>;</literal></listitem>
  1916. </itemizedlist>
  1917. <para>
  1918. For example, if an aspect wished to make a particular class runnable,
  1919. it might define appropriate inter-type <literal>void
  1920. run()</literal> method, but it should also declare that the class
  1921. fulfills the <literal>Runnable</literal> interface. In order to
  1922. implement the methods in the <literal>Runnable</literal> interface, the
  1923. inter-type <literal>run()</literal> method must be public:
  1924. </para>
  1925. <programlisting>
  1926. aspect A {
  1927. declare parents: SomeClass implements Runnable;
  1928. public void SomeClass.run() { ... }
  1929. }
  1930. </programlisting>
  1931. </sect2>
  1932. <sect2>
  1933. <title>Interfaces with members</title>
  1934. <para>
  1935. Through the use of inter-type members, interfaces may now carry
  1936. (non-public-static-final) fields and (non-public-abstract) methods that
  1937. classes can inherit. Conflicts may occur from ambiguously inheriting
  1938. members from a superclass and multiple superinterfaces.
  1939. </para>
  1940. <para>
  1941. Because interfaces may carry non-static initializers, each interface
  1942. behaves as if it has a zero-argument constructor containing its
  1943. initializers. The order of super-interface instantiation is
  1944. observable. We fix this order with the following properties: A
  1945. supertype is initialized before a subtype, initialized code runs only
  1946. once, and the initializers for a type's superclass are run before the
  1947. initializers for its superinterfaces. Consider the following hierarchy
  1948. where {<literal>Object</literal>, <literal>C</literal>,
  1949. <literal>D</literal>, <literal>E</literal>} are classes,
  1950. {<literal>M</literal>, <literal>N</literal>, <literal>O</literal>,
  1951. <literal>P</literal>, <literal>Q</literal>} are interfaces.
  1952. </para>
  1953. <programlisting>
  1954. Object M O
  1955. \ / \ /
  1956. C N Q
  1957. \ / /
  1958. D P
  1959. \ /
  1960. E
  1961. </programlisting>
  1962. <para>
  1963. when a new <literal>E</literal> is instantiated, the initializers run in this order:
  1964. </para>
  1965. <programlisting>
  1966. Object M C O N D Q P E
  1967. </programlisting>
  1968. </sect2>
  1969. <!-- ============================== -->
  1970. <sect2>
  1971. <title>Warnings and Errors</title>
  1972. <para>An aspect may specify that a particular join point should never be
  1973. reached. </para>
  1974. <itemizedlist>
  1975. <listitem><literal>declare error: <replaceable>Pointcut</replaceable>: <replaceable>String</replaceable>;</literal></listitem>
  1976. <listitem><literal>declare warning: <replaceable>Pointcut</replaceable>: <replaceable>String</replaceable>;</literal></listitem>
  1977. </itemizedlist>
  1978. <para>If the compiler determines that a join point in
  1979. <replaceable>Pointcut</replaceable> could possibly be reached, then it
  1980. will signal either an error or warning, as declared, using the
  1981. <replaceable>String</replaceable> for its message. </para>
  1982. </sect2>
  1983. <sect2>
  1984. <title>Softened exceptions</title>
  1985. <para>An aspect may specify that a particular kind of exception, if
  1986. thrown at a join point, should bypass Java's usual static exception
  1987. checking system and instead be thrown as a
  1988. <literal>org.aspectj.lang.SoftException</literal>, which is subtype of
  1989. <literal>RuntimeException</literal> and thus does not need to be
  1990. declared. </para>
  1991. <itemizedlist>
  1992. <listitem><literal>declare soft: <replaceable>Type</replaceable>: <replaceable>Pointcut</replaceable>;</literal></listitem>
  1993. </itemizedlist>
  1994. <para>For example, the aspect</para>
  1995. <programlisting>
  1996. aspect A {
  1997. declare soft: Exception: execution(void main(String[] args));
  1998. }
  1999. </programlisting>
  2000. <para>Would, at the execution join point, catch any
  2001. <literal>Exception</literal> and rethrow a
  2002. <literal>org.aspectj.lang.SoftException</literal> containing
  2003. original exception. </para>
  2004. <para>This is similar to what the following advice would do</para>
  2005. <programlisting>
  2006. aspect A {
  2007. void around() execution(void main(String[] args)) {
  2008. try { proceed(); }
  2009. catch (Exception e) {
  2010. throw new org.aspectj.lang.SoftException(e);
  2011. }
  2012. }
  2013. }
  2014. </programlisting>
  2015. <para>except, in addition to wrapping the exception, it also affects
  2016. Java's static exception checking mechanism. </para>
  2017. <para> Like advice, the declare soft form has no effect in an
  2018. abstract aspect that is not extended by a concreate aspect. So
  2019. the following code will not compile unless it is compiled with an
  2020. extending concrete aspect:</para>
  2021. <programlisting>
  2022. abstract aspect A {
  2023. abstract pointcut softeningPC();
  2024. before() : softeningPC() {
  2025. Class.forName("FooClass"); // error: uncaught ClassNotFoundException
  2026. }
  2027. declare soft : ClassNotFoundException : call(* Class.*(..));
  2028. }
  2029. </programlisting>
  2030. </sect2>
  2031. <sect2>
  2032. <title>Advice Precedence</title>
  2033. <para>
  2034. An aspect may declare a precedence relationship between concrete
  2035. aspects with the <literal>declare precedence</literal> form:
  2036. </para>
  2037. <itemizedlist>
  2038. <listitem><literal>declare precedence :
  2039. <replaceable>TypePatternList</replaceable> ; </literal></listitem>
  2040. </itemizedlist>
  2041. <para>This signifies that if any join point has advice from two
  2042. concrete aspects matched by some pattern in
  2043. <replaceable>TypePatternList</replaceable>, then the precedence of
  2044. the advice will be the order of in the list. </para>
  2045. <para>In <replaceable>TypePatternList</replaceable>, the wildcard "*" can
  2046. appear at most once, and it means "any type not matched by any other
  2047. pattern in the list". </para>
  2048. <para>For example, the constraints that (1) aspects that have
  2049. Security as part of their name should have precedence over all other
  2050. aspects, and (2) the Logging aspect (and any aspect that extends it)
  2051. should have precedence over all non-security aspects, can be
  2052. expressed by:</para>
  2053. <programlisting>
  2054. declare precedence: *..*Security*, Logging+, *;
  2055. </programlisting>
  2056. <para>
  2057. For another example, the CountEntry aspect might want to count the
  2058. entry to methods in the current package accepting a Type object as
  2059. its first argument. However, it should count all entries, even
  2060. those that the aspect DisallowNulls causes to throw exceptions.
  2061. This can be accomplished by stating that CountEntry has precedence
  2062. over DisallowNulls. This declaration could be in either aspect, or
  2063. in another, ordering aspect:
  2064. </para>
  2065. <programlisting>
  2066. aspect Ordering {
  2067. declare precedence: CountEntry, DisallowNulls;
  2068. }
  2069. aspect DisallowNulls {
  2070. pointcut allTypeMethods(Type obj): call(* *(..)) <![CDATA[&&]]> args(obj, ..);
  2071. before(Type obj): allTypeMethods(obj) {
  2072. if (obj == null) throw new RuntimeException();
  2073. }
  2074. }
  2075. aspect CountEntry {
  2076. pointcut allTypeMethods(Type obj): call(* *(..)) <![CDATA[&&]]> args(obj, ..);
  2077. static int count = 0;
  2078. before(): allTypeMethods(Type) {
  2079. count++;
  2080. }
  2081. }
  2082. </programlisting>
  2083. <sect3>
  2084. <title>Various cycles</title>
  2085. <para>
  2086. It is an error for any aspect to be matched by more than one
  2087. TypePattern in a single decare precedence, so:
  2088. </para>
  2089. <programlisting>
  2090. declare precedence: A, B, A ; // error
  2091. </programlisting>
  2092. <para>
  2093. However, multiple declare precedence forms may legally have this
  2094. kind of circularity. For example, each of these declare
  2095. precedence is perfectly legal:
  2096. </para>
  2097. <programlisting>
  2098. declare precedence: B, A;
  2099. declare precedence: A, B;
  2100. </programlisting>
  2101. <para>
  2102. And a system in which both constraints are active may also be
  2103. legal, so long as advice from A and B don't share a join
  2104. point. So this is an idiom that can be used to enforce that A and
  2105. B are strongly independent.
  2106. </para>
  2107. </sect3>
  2108. <sect3>
  2109. <title>Applies to concrete aspects</title>
  2110. <para>
  2111. Consider the following library aspects:
  2112. </para>
  2113. <programlisting>
  2114. abstract aspect Logging {
  2115. abstract pointcut logged();
  2116. before(): logged() {
  2117. System.err.println("thisJoinPoint: " + thisJoinPoint);
  2118. }
  2119. }
  2120. abstract aspect MyProfiling {
  2121. abstract pointcut profiled();
  2122. Object around(): profiled() {
  2123. long beforeTime = System.currentTimeMillis();
  2124. try {
  2125. return proceed();
  2126. } finally {
  2127. long afterTime = System.currentTimeMillis();
  2128. addToProfile(thisJoinPointStaticPart,
  2129. afterTime - beforeTime);
  2130. }
  2131. }
  2132. abstract void addToProfile(
  2133. org.aspectj.JoinPoint.StaticPart jp,
  2134. long elapsed);
  2135. }
  2136. </programlisting>
  2137. <para>
  2138. In order to use either aspect, they must be extended with
  2139. concrete aspects, say, MyLogging and MyProfiling. Because advice
  2140. only applies from concrete aspects, the declare precedence form
  2141. only matters when declaring precedence with concrete aspects. So
  2142. </para>
  2143. <programlisting>
  2144. declare precedence: Logging, Profiling;
  2145. </programlisting>
  2146. <para>
  2147. has no effect, but both
  2148. </para>
  2149. <programlisting>
  2150. declare precedence: MyLogging, MyProfiling;
  2151. declare precedence: Logging+, Profiling+;
  2152. </programlisting>
  2153. <para>
  2154. are meaningful.
  2155. </para>
  2156. </sect3>
  2157. </sect2>
  2158. <sect2>
  2159. <title>Statically determinable pointcuts</title>
  2160. <para>Pointcuts that appear inside of <literal>declare</literal> forms
  2161. have certain restrictions. Like other pointcuts, these pick out join
  2162. points, but they do so in a way that is statically determinable. </para>
  2163. <para>Consequently, such pointcuts may not include, directly or
  2164. indirectly (through user-defined pointcut declarations) pointcuts that
  2165. discriminate based on dynamic (runtime) context. Therefore, such
  2166. pointcuts may not be defined in terms of</para>
  2167. <itemizedlist>
  2168. <listitem>cflow</listitem>
  2169. <listitem>cflowbelow</listitem>
  2170. <listitem>this</listitem>
  2171. <listitem>target</listitem>
  2172. <listitem>args</listitem>
  2173. <listitem>if</listitem>
  2174. </itemizedlist>
  2175. <para> all of which can discriminate on runtime information. </para>
  2176. </sect2>
  2177. </sect1>
  2178. <sect1 id="semantics-aspects">
  2179. <title>Aspects</title>
  2180. <para>
  2181. An aspect is a crosscutting type defined by the <literal>aspect</literal>
  2182. declaration.
  2183. </para>
  2184. <sect2>
  2185. <title>Aspect Declaration</title>
  2186. <para>
  2187. The <literal>aspect</literal> declaration is similar to the
  2188. <literal>class</literal> declaration in that it defines a type and an
  2189. implementation for that type. It differs in a number of
  2190. ways:
  2191. </para>
  2192. <sect3>
  2193. <title>Aspect implementation can cut across other types</title>
  2194. <para> In addition to normal Java class declarations such as
  2195. methods and fields, aspect declarations can include AspectJ
  2196. declarations such as advice, pointcuts, and inter-type
  2197. declarations. Thus, aspects contain implementation
  2198. declarations that can can cut across other types (including those defined by
  2199. other aspect declarations).
  2200. </para>
  2201. </sect3>
  2202. <sect3>
  2203. <title>Aspects are not directly instantiated</title>
  2204. <para> Aspects are not directly instantiated with a new
  2205. expression, with cloning, or with serialization. Aspects may
  2206. have one constructor definition, but if so it must be of a
  2207. constructor taking no arguments and throwing no checked
  2208. exceptions.
  2209. </para>
  2210. </sect3>
  2211. <sect3>
  2212. <title>Nested aspects must be <literal>static</literal></title>
  2213. <para>
  2214. Aspects may be defined either at the package level, or as a static nested
  2215. aspect -- that is, a static member of a class, interface, or aspect. If it
  2216. is not at the package level, the aspect <emphasis>must</emphasis> be
  2217. defined with the static keyword. Local and anonymous aspects are not
  2218. allowed.
  2219. </para>
  2220. </sect3>
  2221. </sect2>
  2222. <sect2>
  2223. <title>Aspect Extension</title>
  2224. <para>
  2225. To support abstraction and composition of crosscutting concerns,
  2226. aspects can be extended in much the same way that classes can. Aspect
  2227. extension adds some new rules, though.
  2228. </para>
  2229. <sect3>
  2230. <title>Aspects may extend classes and implement interfaces</title>
  2231. <para>
  2232. An aspect, abstract or concrete, may extend a class and may implement
  2233. a set of interfaces. Extending a class does not provide the ability
  2234. to instantiate the aspect with a new expression: The aspect may still
  2235. only define a null constructor.
  2236. </para>
  2237. </sect3>
  2238. <sect3>
  2239. <title>Classes may not extend aspects</title>
  2240. <para>
  2241. It is an error for a class to extend or implement an aspect.
  2242. </para>
  2243. </sect3>
  2244. <sect3>
  2245. <title>Aspects extending aspects
  2246. </title>
  2247. <para>
  2248. Aspects may extend other aspects, in which case not only are fields
  2249. and methods inherited but so are pointcuts. However, aspects may only
  2250. extend abstract aspects. It is an error for a concrete aspect to
  2251. extend another concrete aspect.
  2252. </para>
  2253. </sect3>
  2254. </sect2>
  2255. <sect2>
  2256. <title>Aspect instantiation</title>
  2257. <para>
  2258. Unlike class expressions, aspects are not instantiated with
  2259. <literal>new</literal> expressions. Rather, aspect instances are
  2260. automatically created to cut across programs.
  2261. </para>
  2262. <para>
  2263. Because advice only runs in the context of an aspect instance, aspect
  2264. instantiation indirectly controls when advice runs.
  2265. </para>
  2266. <para>
  2267. The criteria used to determine how an aspect is instantiated
  2268. is inherited from its parent aspect. If the aspect has no parent
  2269. aspect, then by default the aspect is a singleton aspect.
  2270. </para>
  2271. <sect3>
  2272. <title>Singleton Aspects</title>
  2273. <itemizedlist>
  2274. <listitem><literal>aspect <replaceable>Id</replaceable> { ... }</literal></listitem>
  2275. <listitem><literal>aspect <replaceable>Id</replaceable> issingleton { ... }</literal></listitem>
  2276. </itemizedlist>
  2277. <para>
  2278. By default (or by using the modifier <literal>issingleton</literal>)
  2279. an aspect has exactly one instance that cuts across the entire
  2280. program. That instance is available at any time during program
  2281. execution with the static method <literal>aspectOf()</literal>
  2282. defined on the aspect
  2283. -- so, in the above examples, <literal>A.aspectOf()</literal> will
  2284. return A's instance. This aspect instance is created as the aspect's
  2285. classfile is loaded.
  2286. </para>
  2287. <para>
  2288. Because the an instance of the aspect exists at all join points in
  2289. the running of a program (once its class is loaded), its advice will
  2290. have a chance to run at all such join points.
  2291. </para>
  2292. <para>
  2293. (In actuality, one instance of the aspect A is made for each version
  2294. of the aspect A, so there will be one instantiation for each time A
  2295. is loaded by a different classloader.)
  2296. </para>
  2297. </sect3>
  2298. <sect3>
  2299. <title>Per-object aspects</title>
  2300. <itemizedlist>
  2301. <listitem><literal>aspect <replaceable>Id</replaceable> perthis(<replaceable>Pointcut</replaceable>) { ... }</literal></listitem>
  2302. <listitem><literal>aspect <replaceable>Id</replaceable> pertarget(<replaceable>Pointcut</replaceable>) { ... }</literal></listitem>
  2303. </itemizedlist>
  2304. <para>
  2305. If an aspect A is defined
  2306. <literal>perthis(<replaceable>Pointcut</replaceable>)</literal>, then
  2307. one object of type A is created for every object that is the
  2308. executing object (i.e., "this") at any of the join points picked out
  2309. by <replaceable>Pointcut</replaceable>.
  2310. The advice defined in A may then run at any join point where the
  2311. currently executing object has been associated with an instance of
  2312. A.
  2313. </para>
  2314. <para> Similarly, if an aspect A is defined
  2315. <literal>pertarget(<replaceable>Pointcut</replaceable>)</literal>,
  2316. then one object of type A is created for every object that is the
  2317. target object of the join points picked out by
  2318. <replaceable>Pointcut</replaceable>.
  2319. The advice defined in A may then run at any join point where the
  2320. target object has been associated with an instance of
  2321. A.
  2322. </para>
  2323. <para>
  2324. In either case, the static method call
  2325. <literal>A.aspectOf(Object)</literal> can be used to get the aspect
  2326. instance (of type A) registered with the object. Each aspect
  2327. instance is created as early as possible, but not before reaching a
  2328. join point picked out by <replaceable>Pointcut</replaceable> where
  2329. there is no associated aspect of type A.
  2330. </para>
  2331. <para> Both <literal>perthis</literal> and <literal>pertarget</literal>
  2332. aspects may be affected by code the AspectJ compiler controls, as
  2333. discussed in the <xref linkend="implementation"/> appendix. </para>
  2334. </sect3>
  2335. <sect3>
  2336. <title>Per-control-flow aspects</title>
  2337. <itemizedlist>
  2338. <listitem><literal>aspect <replaceable>Id</replaceable> percflow(<replaceable>Pointcut</replaceable>) { ... }</literal></listitem>
  2339. <listitem><literal>aspect <replaceable>Id</replaceable> percflowbelow(<replaceable>Pointcut</replaceable>) { ... }</literal></listitem>
  2340. </itemizedlist>
  2341. <para>
  2342. If an aspect A is defined
  2343. <literal>percflow(<replaceable>Pointcut</replaceable>)</literal> or
  2344. <literal>percflowbelow(<replaceable>Pointcut</replaceable>)</literal>,
  2345. then one object of type A is created for each flow of control of the
  2346. join points picked out by <replaceable>Pointcut</replaceable>, either
  2347. as the flow of control is entered, or below the flow of control,
  2348. respectively. The advice defined in A may run at any join point in
  2349. or under that control flow. During each such flow of control, the
  2350. static method <literal>A.aspectOf()</literal> will return an object
  2351. of type
  2352. A. An instance of the aspect is created upon entry into each such
  2353. control flow.
  2354. </para>
  2355. </sect3>
  2356. <sect3>
  2357. <title>Aspect instantiation and advice</title>
  2358. <para>
  2359. All advice runs in the context of an aspect instance,
  2360. but it is possible to write a piece of advice with a pointcut
  2361. that picks out a join point that must occur before asopect
  2362. instantiation. For example:
  2363. </para>
  2364. <programlisting>
  2365. public class Client
  2366. {
  2367. public static void main(String[] args) {
  2368. Client c = new Client();
  2369. }
  2370. }
  2371. aspect Watchcall {
  2372. pointcut myConstructor(): execution(new(..));
  2373. before(): myConstructor() {
  2374. System.err.println("Entering Constructor");
  2375. }
  2376. }
  2377. </programlisting>
  2378. <para>
  2379. The before advice should run before the execution of all
  2380. constructors in the system. It must run in the context of an
  2381. instance of the Watchcall aspect. The only way to get such an
  2382. instance is to have Watchcall's default constructor execute. But
  2383. before that executes, we need to run the before advice...
  2384. </para>
  2385. <para>
  2386. There is no general way to detect these kinds of circularities at
  2387. compile time. If advice runs before its aspect is instantiated,
  2388. AspectJ will throw a <ulink
  2389. url="../api/org/aspectj/lang/NoAspectBoundException.html">
  2390. <literal>org.aspectj.lang.NoAspectBoundException</literal></ulink>.
  2391. </para>
  2392. </sect3>
  2393. </sect2>
  2394. <sect2>
  2395. <title>Aspect privilege</title>
  2396. <itemizedlist>
  2397. <listitem><literal>privileged aspect <replaceable>Id</replaceable> { ... }</literal></listitem>
  2398. </itemizedlist>
  2399. <para>
  2400. Code written in aspects is subject to the same access control rules as
  2401. Java code when referring to members of classes or aspects. So, for
  2402. example, code written in an aspect may not refer to members with
  2403. default (package-protected) visibility unless the aspect is defined in
  2404. the same package.
  2405. </para>
  2406. <para>
  2407. While these restrictions are suitable for many aspects, there may be
  2408. some aspects in which advice or inter-type members needs to access private
  2409. or protected resources of other types. To allow this, aspects may be
  2410. declared <literal>privileged</literal>. Code in priviliged aspects has
  2411. access to all members, even private ones.
  2412. </para>
  2413. <programlisting>
  2414. class C {
  2415. private int i = 0;
  2416. void incI(int x) { i = i+x; }
  2417. }
  2418. privileged aspect A {
  2419. static final int MAX = 1000;
  2420. before(int x, C c): call(void C.incI(int)) <![CDATA[&&]]> target(c) <![CDATA[&&]]> args(x) {
  2421. if (c.i+x &gt; MAX) throw new RuntimeException();
  2422. }
  2423. }
  2424. </programlisting>
  2425. <para>
  2426. In this case, if A had not been declared privileged, the field reference
  2427. c.i would have resulted in an error signaled by the compiler.
  2428. </para>
  2429. <para>
  2430. If a privileged aspect can access multiple versions of a particular
  2431. member, then those that it could see if it were not privileged take
  2432. precedence. For example, in the code
  2433. </para>
  2434. <programlisting>
  2435. class C {
  2436. private int i = 0;
  2437. void foo() { }
  2438. }
  2439. privileged aspect A {
  2440. private int C.i = 999;
  2441. before(C c): call(void C.foo()) target(c) {
  2442. System.out.println(c.i);
  2443. }
  2444. }
  2445. </programlisting>
  2446. <para>
  2447. A's private inter-type field C.i, initially bound to 999, will be
  2448. referenced in the body of the advice in preference to C's privately
  2449. declared field, since the A would have access to its own inter-type
  2450. fields even if it were not privileged.
  2451. </para>
  2452. <para>
  2453. Note that a privileged aspect can access private inter-type
  2454. declarations made by other aspects, since they are simply
  2455. considered private members of that other aspect.
  2456. </para>
  2457. </sect2>
  2458. </sect1>
  2459. </appendix>