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

semantics.xml 109KB

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