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

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