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.

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