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.

porting.html 63KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html> <head>
  3. <title>AspectJ Reference - Porting Notes</title>
  4. </head>
  5. <body>
  6. <div align=right><small>
  7. &copy; Copyright 1998-2002 Palo Alto Research Center Incorporated,
  8. 2003-2004 Contributors.
  9. All rights reserved.
  10. </small></div>
  11. <h1>AspectJ Porting Notes</h1>
  12. <ul>
  13. <li><a href="#pre-1.2">Pre-1.2 code</a></li>
  14. <li><a href="#pre-1.1">Pre-1.1 code</a></li>
  15. <li><a href="#pre-1.0.4">Pre-1.0.4 code</a></li>
  16. <li><a href="#pre-1.0rc1">Pre-1.0rc1 code</a></li>
  17. <li><a href="#pre-1.0beta1">Pre-1.0beta1 code</a></li>
  18. <li><a href="#pre-1.0alpha1">Pre-1.0alpha1 code</a>
  19. </li>
  20. <li><a href="#pre08b3">Pre-0.8beta3 code</a></li>
  21. <li><a href="#pre08b1">Pre-0.8beta1 code</a></li>
  22. <li><a href="#pre07b11">Pre-0.7beta11 code</a></li>
  23. <li><a href="#pre07b10">Pre-0.7beta10 code</a></li>
  24. </ul>
  25. <h2><a name="pre-1.2">Porting pre-1.2 code to AspectJ 1.2</a></h2>
  26. <a href="README-12.html">README-12.html</a> contains a discussion
  27. of the changes between 1.1 and 1.2. The key points are:
  28. <p><b>The default AspectJ compiler compliance level is now 1.4</b> (whereas in
  29. previous releases the default compliance level was 1.3). This has a number
  30. of implications:
  31. </p>
  32. <ul>
  33. <li> class files generated by the compiler are now JRE v1.2 and upwards
  34. compatible. (At compliance level 1.3, AspectJ generated class files that
  35. were compatible with JRE 1.1 also).</li>
  36. <li> <code>call</code> pointcuts may match more join points than in the same
  37. program compiled at compliance level 1.3.</li>
  38. </ul>
  39. <p>
  40. The AspectJ compiler can be restored to 1.3 compliance settings by specifying the
  41. "-1.3" option on the command-line.
  42. </p>
  43. <p>The following example program illustrates the differences in join point matching
  44. with the <code>call</code> pointcut designator between 1.4 and 1.3 compliance levels.
  45. </p>
  46. <pre>
  47. <code>
  48. 01 class A {
  49. 02 public void doIt() {...};
  50. 03 }
  51. 04
  52. 05 class B extends A {
  53. 06 public void doThisToo() {...};
  54. 07 }
  55. 08
  56. 09
  57. 10 public class CallsAandB {
  58. 11
  59. 12 public static void main(String[] args) {
  60. 13 B b = new B();
  61. 14 A bInDisguise = new B();
  62. 15
  63. 16 b.doIt(); // AspectJ 1.2 matches here
  64. 17 bInDisguise.doIt(); // this is never matched
  65. 18 }
  66. 19
  67. 20 }
  68. 21
  69. 22 aspect CallPCDMatchingExample {
  70. 23
  71. 24 before() : call(* B.doIt(..)) {
  72. 25 System.out.println("About to call B.doIt(...)");
  73. 26 }
  74. 27
  75. 28 }
  76. </code>
  77. </pre>
  78. <p>
  79. When this program is compiled with AspectJ 1.2 using the default compiler options,
  80. it will produce one line of output when it is executed:
  81. </p>
  82. <p><code>About to call B.doIt(...)</code></p>
  83. <p>The same program compiled under AspectJ 1.1 (or using AspectJ 1.2 with the -1.3 flag specified)
  84. does not produce any output when it is run.
  85. </p>
  86. <p>
  87. The reason for the additional call pcd match is that prior to compliance level 1.4,
  88. Java compilers produced bytecodes that call A.doIt() (the defining type of the method),
  89. rather than B.doIt() (the declared type in the program text). The generated call to
  90. A.doIt() is not matched by the call pcd used in the before advice. At
  91. compliance level 1.4, the bytecodes retain the declared type of the receiver in the
  92. program source, generating a call to B.doIt(), which <i>is</i> matched by the call pcd.
  93. </p>
  94. <p>This is a good example of why the recommended style is to use <code>call(* doIt(..)) && target(B)</code>,
  95. which always matches based on the actual type of the receiver.
  96. </p>
  97. <p><b>New warnings emitted by the compiler for unmatched call pcds.</b> Because users have found
  98. the static type matching used for a type pattern specified in a <code>call</code> pcd confusing
  99. (as evidenced by the example above), AspectJ 1.2 has a new Xlint warning which is enable by default.
  100. The compiler will now produce a warning whenever a call pointcut designator does not match at a
  101. join point, and a user may have expected it to. Compiling the above program using AspectJ 1.2
  102. produces the following compiler output:
  103. </p>
  104. <pre>
  105. <code>
  106. <font color="red">
  107. CallsAandB.java:24 warning does not match because declaring type is A, if match desired use target(B) [Xlint:unmatchedSuperTypeInCall]
  108. before() : call(* B.doIt(..)) {
  109. ^^^^^^^^^^^^^^^
  110. see also: CallsAandB.java:17
  111. </font>
  112. <font color="blue">
  113. 1 warning
  114. </font>
  115. </code>
  116. </pre>
  117. The warning is telling us that the call pointcut associated with the before advice on line 24 of the source file
  118. does not match at a join point where the user may have expected it to. The source location
  119. corresponding to the unmatched join point is indicated by the "see also" line - in this case line 17 of the
  120. source file. At line 17 we find a call to <code>bInDisguise.doIt()</code>. Since the static type of
  121. <code>bInDisguise</code> is <code>A</code>, this call will never be matched. The warning also tells us
  122. a possible solution if we intended the pointcut to match at this join point: use
  123. <code>call(* doIt(..) && target(B)</code>.
  124. <p>If you find warnings of this kind coming out when you use the AspectJ 1.2 compiler, the recommended fix is to
  125. switch to using the <code>target</code> designator in place of a type pattern in the <code>call</code> pointcut
  126. expression. Note that there is no loss of runtime efficiency here - runtime tests are only added in the cases
  127. where it cannot be determined at compile time whether the type of the receiver will match the type specified in
  128. the <code>target</code> expression. Note that <code>target</code> cannot be used in <code>declare</code> statements.
  129. </p>
  130. <p><b>Use of non-statically determinable pointcut expressions in declare statements</b> has always been forbidden,
  131. but prior to 1.2 the AspectJ compiler did not raise an error if they were used. The AspectJ Language
  132. Semantics appendix states that <code>cflow, cflowbelow, this, target, args</code> and <code>if</code> pointcut
  133. designators cannot be used directly or indirectly (through a user-defined pointcut) inside of a <code>declare</code>
  134. statment. When moving code from 1.1 to 1.2, additional errors may be raised due to the stricter policing of this
  135. rule. The solution is to recode the declare statement avoiding pointcut expressions that may require a run-time test.
  136. </p>
  137. <p><b>Interface constructors no longer supported</b>.
  138. Declaring a constructor on an interface is now (correctly) prohibited,
  139. and there will no longer be a constructor-execution join point for the interface.
  140. To initialize a field declared on an interface, use initialization, e.g.,
  141. </p>
  142. <pre>int I.i;
  143. after(I i) returning: initialization(I) && this(i) { i.i = 2; }</pre>
  144. <p>To pick out the constructor-execution for any implementation of I, try
  145. </p>
  146. <pre>execution(I+.new(..))</pre>
  147. <p>For more information, see bug
  148. <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=49295">49295</a>.
  149. </p>
  150. <p><b>Declaring a static method on an interface</b> is now (correctly) prohibited.
  151. One workaround is to define a static method on the aspect instead.
  152. For more information, see bug
  153. <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=47754">47754</a>.
  154. </p>
  155. <p><b>Watch for problems due to incompatible BCEL versions.</b>
  156. AspectJ 1.2 includes a different version of BCEL than AspectJ 1.1. If you have the older
  157. version of BCEL available earlier on your classpath than the version included in the
  158. 1.2 aspectjtools.jar then you will see errors like:
  159. <pre>
  160. C:\work\test\TestAspect.aj error Internal compiler error
  161. java.lang.NoSuchMethodError: org.apache.bcel.generic.InstructionFactory.
  162. createNewArray(Lorg/apache/bcel/generic/Type;S)Lorg/apache/bcel/generic/Instruction;
  163. </pre>
  164. This typically happens because the old version of BCEL has been included as a standard
  165. extension in your JVM configuration. Ensure you have removed it from jre/lib/ext
  166. under your JDK installation.
  167. <p>For more information, see bugs including
  168. <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=60389">60389</a>,
  169. <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=59921">59921</a>.
  170. </p>
  171. <h2><a name="pre-1.1">Porting pre-1.1 code to AspectJ 1.1</a></h2>
  172. <a href="README-11.html">README-11.html</a> contains a discussion
  173. of the language changes from 1.0 to 1.1. The high points:
  174. <p>
  175. The <code>call(..)</code> pointcut designator is now implemented
  176. only at the call site; by contrast, the AspectJ 1.0 compiler could
  177. also implement it on the callee side. So in 1.0 if you
  178. compiled a pointcut using <code>call(..)</code> but only passed
  179. the compiler the code for the target of the call, the pointcut
  180. could be implemented. This is not true for 1.1. To fix this,
  181. use <code>execution(..)</code> in place of <code>call(..)</code>,
  182. or include all calling clients in the compile.
  183. (<a href="README-11.html#NO_CALLEE_SIDE_CALL">more info</a>)
  184. <p>
  185. Type-patterns are no longer permitted for the defining
  186. type of inter-type declarations. Replace the pattern with a
  187. type.
  188. In many cases, you can declare members on an interface type,
  189. and then declare that the types picked out by the type-pattern
  190. implement have the interface as their parent.
  191. (<a href="README-11.html#SINGLE_INTERCLASS_TARGET">more info</a>)
  192. <!-- todo code -->
  193. <p>
  194. Type-patterns are no longer permitted when specifying
  195. <code>declare soft</code>.
  196. Replace the pattern with a literal type.
  197. <!-- todo code -->
  198. <p>
  199. Wildcards patterns (<code>foo..*</code>) are no longer
  200. permitted for
  201. <code>this()</code>,
  202. <code>target()</code>, or
  203. <code>args()</code>.
  204. Replace the pattern with a literal type or
  205. with a subtype wildcard (<code>Type+</code>).
  206. (<a href="README-11.html#INSTANCEOF_ON_WILD">more info</a>)
  207. <!-- todo code -->
  208. <p>
  209. Conflicts will be reported for no-argument constructors
  210. generated by compilers when no constructor is defined
  211. for a class. That means the following code will compile
  212. in 1.0 but not in 1.1:
  213. <pre>
  214. class C {}
  215. aspect A {
  216. C.new() {} // permitted in 1.0; conflict in 1.1
  217. }
  218. </pre>
  219. One fix is to declare a non-conflicting constructor
  220. by adding arguments (or defining a constructor in the
  221. target class); a better fix might be to do the work of the
  222. declared constructor in advice on the initialization
  223. join point for the object.
  224. (<a href="README-11.html#DEFAULT_CONSTRUCTOR_CONFLICT">more info</a>)
  225. <p>
  226. The pointcut designators
  227. <code>within()</code> and <code>withincode()</code>
  228. will not pick out
  229. code within the lexical extent of method-local
  230. and anonymous inner types (because these are not
  231. represented as such in bytecode form). Because
  232. <code>within</code> forms specify staticly-determinable pointcuts,
  233. they might be used in declare error or declare warning
  234. statements, which might produce different results.
  235. (<a href="README-11.html#WITHIN_MEMBER_TYPES">more info</a>)
  236. <p>
  237. The compiler will report an error that
  238. the form <code>aspect {name} dominates {list}...</code>
  239. is no longer supported. It has
  240. been replaced by a new declare statement:
  241. <pre>
  242. declare precedence : {name} {list}...
  243. </pre>
  244. (<a href="README-11.html#ASPECT_PRECEDENCE">more info</a>)
  245. <p>
  246. The field set join point now has a return type of <code>void</code>.
  247. Compiling programs using around advice on these join points might
  248. cause errors unless the return type of the around advice
  249. and the result of any proceed() call is
  250. <code>Object</code> or <code>void</code>.
  251. (<a href="README-11.html#VOID_FIELD_SET">more info</a>)
  252. <p>
  253. The compiler cannot implement after or around advice for
  254. the handler PCD because the end of exception handlers is
  255. ambiguous in bytecode. Try to use before advice.
  256. (<a href="README-11.html#AFTER_HANDLER">more info</a>)
  257. <p>
  258. <h2><a name="pre-1.0.4">Porting pre-1.0.4 code</a></h2>
  259. <p>In versions of AspectJ prior to 1.0.4, the compiler was not
  260. correctly implementing the AspectJ-1.0 language design for some uses
  261. of after returning advice.
  262. </p>
  263. <p> The main change that was made was of after returning advice for
  264. constructor execution join points. Previously, this advice was legal:
  265. </p>
  266. <PRE>
  267. after() returning (Foo f): execution(Foo.new(..)) { ... }
  268. </PRE>
  269. <p> However, it has always been a part of the 1.0 language design (and
  270. of Java's language design) that constructors themselves (as opposed to
  271. constructor calls) do not return the value of the new object. Rather,
  272. <code>this</code> is bound to the new object, and the constructor
  273. behaves like a void method. With that in mind, any code like the
  274. above should be conveted to the form. </p>
  275. <PRE>
  276. after(Foo f) returning: this(f) &amp;&amp; execution(Foo.new(..)) { ... }
  277. </PRE>
  278. <p> In compilers prior to 1.0.4, the following advice could pick out
  279. join points
  280. </p>
  281. <PRE>
  282. after() returning (String s): call(void foo()) { ... }
  283. </PRE>
  284. <p> This is no longer picked out. This pattern was most commonly used
  285. in highly polymorphic contexts, such as
  286. </p>
  287. <PRE>
  288. after() returning (String s): call(* foo()) { ... }
  289. </PRE>
  290. <p> If you want to capture all calls, binding null objects for those
  291. that would otherwise have no value, you must use the
  292. <code>Object</code> type.
  293. </p>
  294. <PRE>
  295. after() returning (Object o): call(* foo()) { ... }
  296. </PRE>
  297. <p> Uses of both of these forms are highleted with compiler warnings
  298. in the 1.0.4 compiler.
  299. </p>
  300. <hr />
  301. <h2><a name="pre-1.0rc1">Porting pre-1.0rc1 code</a></h2>
  302. <p> Aspects can no longer be declared to implement the
  303. <code>Serializable</code> or <code>Cloneable</code> interfaces. If
  304. you previously used serializable or cloneable aspects, you should
  305. refactor your code to keep the state you need to serialize or clone in
  306. objects associated with the aspects.
  307. </p>
  308. <hr />
  309. <h2><a name="pre-1.0beta1">Porting pre-1.0beta1 code</a></h2>
  310. <p> The <code>static</code> modifier is no longer allowed on pointcut
  311. declarations anywhere. Porting is simple; just remove the static
  312. declarations when you find them.
  313. </p>
  314. <p> Also, though the <code>returns</code> modifier on pointcuts has
  315. not been part of the language since 1.0alpha1, the compiler still
  316. accepted them until now. If you used this feature, now is the right
  317. time to remove the <code>returns</code> modifier when the compiler
  318. complains about it.
  319. </p>
  320. <hr />
  321. <h2><a name="pre-1.0alpha1">Porting pre-1.0alpha1 code </a></h2>
  322. <p> The release of AspectJ 1.0alpha1 involved sweeping cleanups of the
  323. language to bring it to 1.0 status. </p>
  324. <ul>
  325. <li><a href="#1.0a1-pointcuts">Pointcuts</a></li>
  326. <li><a href="#1.0a1-type-patterns">Type patterns</a></li>
  327. <li><a href="#1.0a1-advice">Advice</a></li>
  328. <li><a href="#1.0a1-introduction-and-static">Introduction and
  329. static crosscutting</a></li>
  330. <li><a href="#1.0a1-aspects">Aspects</a></li>
  331. </ul>
  332. <h3><a name="1.0a1-pointcuts">Pointcuts</a></h3>
  333. <h4><a name="1.0a1-plural-to-singular">Removing the "s" from pointcuts</a></h4>
  334. <p> One of the most pervasive changes in porting code written before
  335. 1.0alpha1 is the change in some of the pointcut names from plural to
  336. singular, that is, they lose an "s". In one sense, making this change
  337. in your programs is easy: just go through and whever you see uses of
  338. the pointcuts
  339. </p>
  340. <blockquote>calls executions gets sets handlers initializations
  341. staticinitializations</blockquote>
  342. <p> Just take off the final "s", to make one of
  343. </p>
  344. <blockquote>call execution get set handler initialization
  345. staticinitialization</blockquote>
  346. <p> Often, there will be other changes you should make for each of
  347. these pointcuts, but as for the name, just take off the "s". </p>
  348. <p> One risk you will have when doing this is creating name conflicts.
  349. If, for example, you named a parameter of a pointcut "set", you should
  350. (for your own sanity -- the compiler doesn't require it) rename it in
  351. the rewritten pointcut. </p>
  352. <PRE>
  353. pointcut sort(Collection set): calls(void addAll(set));
  354. ==&gt;
  355. pointcut sort(Collection mySet): call(void addAll(mySet));
  356. </PRE>
  357. <p> While converting to use singular nouns for the primitive
  358. pointcuts, you may also want to remove the "s" from your user-defined
  359. pointcuts. </p>
  360. <PRE>
  361. pointcut publicCalls(): calls(public * *(..));
  362. ==&gt;
  363. pointcut publicCall(): call(public * *(..));
  364. </PRE>
  365. <p> Of course, your naming conventions are your own, but throughout
  366. these porting notes we will be making these changes in our example
  367. ports. </p>
  368. <h4><a name="1.0a1-remove-receptions">Removing the receptions pointcut</a></h4>
  369. <p> Perhaps the largest semantic change in the 1.0 language is the
  370. removal of receptions join points. They have been merged with call
  371. join points in AspectJ 1.0, so now a call join point doesn't represent
  372. the "caller-side" of a call, but the call itself, both caller and
  373. receiver. </p>
  374. <p> Changing code that used the <code>receptions</code> pointcut should be
  375. fairly straightforward, depending on whether the pointcut exposed state or
  376. not. </p>
  377. <h5>Not exposing state</h5>
  378. <p> Receptions pointcuts that did not expose state can simply be
  379. replaced by the new <code>call</code> and <code>target</code> pointcuts:</p>
  380. <PRE>
  381. receptions(void Foo.m())
  382. ==&gt;
  383. target(Foo) && call(void m())
  384. </PRE>
  385. <h5>Exposing state</h5>
  386. <p> Some receptions pointcuts exposed the receiving object by
  387. replacing the receiving type with a pointcut formal. These PCDs
  388. should be rewritten to use the new <code>target</code> pointcut to expose
  389. the receiving object. </p>
  390. <PRE>
  391. pointcut fooCallees(Foo f): receptions(void f.m());
  392. ==&gt;
  393. pointcut fooCallee(Foo f): target(f) &amp;&amp; call(void m());
  394. </PRE>
  395. <p> Like <a href="#1.0a1-fixing-state-access">other pointcuts</a>,
  396. receptions pointcuts that exposed one or more arguments should be
  397. rewritten to use the <code>args</code> pointcut: </p>
  398. <PRE>
  399. pointcut intPassers(int i, int j): receptions(void Foo.m(i, j));
  400. ==&gt;
  401. pointcut intPasser(int i, int j):
  402. args(i, j) &amp;&amp; target(Foo) &amp;&amp; call(void m(int, int));
  403. </PRE>
  404. <h5>Constructor receptions</h5>
  405. <p> There are two issues with constructor receptions in
  406. particular. </p>
  407. <p>Like <a href="#1.0a1-constructor-calls">constructor calls</a>,
  408. constructor receptions pointcuts had a dynamic character, in that
  409. <code>receptions(C.new())</code> would capture constructions of not
  410. only C classes, but also of classes that extended C. </p>
  411. <p> If you want this behaviour, then you need to use the new subtypes
  412. operator, +, on the type name in question. So,
  413. </p>
  414. <PRE>
  415. receptions(C.new())
  416. ==&gt;
  417. call(C+.new())
  418. </PRE>
  419. <p>Also like <a href="#1.0a1-constructor-calls">constructor calls</a>,
  420. constructor receptions allowed access to the constructed object in the
  421. same way as any other object. Since the only advice possible on
  422. constructor receptions join points was <code>after returning</code>
  423. advice, the object was always guaranteed to be there. But since
  424. constructor call join points allow all kinds of advice it may be that
  425. the object isn't constructed yet (say, in before or around advice).
  426. This is a benefit, in that it allows caching constructed objects </p>
  427. <PRE>
  428. aspect Singleton {
  429. private C theC = null;
  430. C around(): call(C.new(..)) {
  431. if (c == null) theC = proceed();
  432. return theC;
  433. }
  434. }
  435. </PRE>
  436. <p> but it does require some rewriting. The new object can be
  437. accessed as the return value in after returning advice. So, </p>
  438. <PRE>
  439. after(Point p) returning (): receptions(p.new(int, int)) { ... }
  440. ==&gt;
  441. after() returning (Point p): call(Point+.new(int, int)) { ... }
  442. </PRE>
  443. <h4><a name="1.0a1-fixing-state-access">Fixing state access</a></h4>
  444. <p> In previous versions of AspectJ, state such as the currently
  445. executing object or a particular argument of a method call could be
  446. accessed from the signatures of many pointcuts, leading to
  447. difficult-to-read forms. In AspectJ 1.0, all state accesses now use
  448. only three pointcuts </p>
  449. <blockquote>args this target</blockquote>
  450. <p> which pick out argument values, the currently executing object,
  451. and the target object of a method call or field operation,
  452. respectively. </p>
  453. <h5>Using args</h5>
  454. <p> Any time you have a pointcut that has a signature where one of the
  455. arguments was a pointcut or advice formal, just replace that formal
  456. with its type and add an <code>args</code> pointcut.
  457. </p>
  458. <PRE>
  459. pointcut intPassers(int i, int j): calls(void Foo.m(i, j));
  460. ==&gt;
  461. pointcut intPasser(int i, int j): args(i, j) &amp;&amp; call(void Foo.m(int, int));
  462. </PRE>
  463. <PRE>
  464. pointcut stringPassers(String s): receptions(void Foo.m(s, ..));
  465. ==&gt;
  466. pointcut stringPasser(String s): args(s, ..) &amp;&amp; call(void Foo.m(String, ..));
  467. </PRE>
  468. <h5>Rewriting calls</h5>
  469. <p> If a calls pointcut exposed the the receiving object, such as </p>
  470. <PRE>
  471. pointcut fooCallees(Foo f): calls(void f.m());
  472. </PRE>
  473. <p> then the new version should use the <code>target</code> pointcut
  474. to get at that object
  475. </p>
  476. <PRE>
  477. pointcut fooCallee(Foo f): target(f) &amp;&amp; call(void Foo.m());
  478. </PRE>
  479. <p> AspectJ's calls pointcut previously allowed the new object to be
  480. exposed, even though it may not have been constructed yet. AspectJ
  481. 1.0 no longer allows this; you can access the new instance only in
  482. after returning advice, when it is guaranteed that the object was
  483. successfully constructed. So instead of using the <code>target</code>
  484. pointcut to expose the value, you should use the normal <code>after
  485. returning</code> mechanism:
  486. </p>
  487. <PRE>
  488. after(Point p) returning (): calls(p.new(int, int)) { ... }
  489. ==&gt;
  490. after() returning (Point p): call(Point+.new(int, int)) { ... }
  491. </PRE>
  492. <h5>Rewriting gets and sets</h5>
  493. <p> Exposing the target object of a <code>gets</code> or
  494. <code>sets</code> pointcut should be done the same way it was for
  495. <code>calls</code> pointcuts, with the new <code>target</code>
  496. pointcut. </p>
  497. <PRE>
  498. before(Frame f): gets(Color f.color) { ... }
  499. ==&gt;
  500. before(Frame f): target(f) &amp;&amp; get(Color Frame.color) { ... }
  501. </PRE>
  502. <PRE>
  503. before(Frame f): sets(Color f.color) { ... }
  504. ==&gt;
  505. before(Frame f): target(f) &amp;&amp; set(Color Frame.color) { ... }
  506. </PRE>
  507. <p> In addition, the clumsy syntax for getting the old value of the
  508. field has been eliminated. For before advice, the port is simple;
  509. just access the field yourself in the body. Depending on the rest of
  510. your system, you may need to restrict the advice from the aspect body
  511. to eliminiate the circularity. </p>
  512. <PRE>
  513. aspect A {
  514. before(Frame f, Color c): gets(Color f.color)[c] { ... }
  515. }
  516. ==&gt;
  517. aspect A {
  518. before(Frame f):
  519. target(f) &amp;&amp; get(Color Frame.color) &amp;&amp; !within(A) {
  520. Color c = f.color;
  521. ...
  522. }
  523. }
  524. </PRE>
  525. <p> The same can be done for <code>around</code> advice. However, the
  526. only way to port after advice that needs the old value is to convert
  527. it to around advice.
  528. </p>
  529. <PRE>
  530. aspect A {
  531. after(Frame f, Color c) returning (): gets(Color f.color)[c] { ... }
  532. }
  533. ==&gt;
  534. aspect A {
  535. void around(Frame f):
  536. target(f) &amp;&amp; get(Color Frame.color) &amp;&amp; !within(A) {
  537. Color c = f.color;
  538. proceed(f);
  539. ...
  540. }
  541. }
  542. </PRE>
  543. <p> When porting <code>sets</code> pointcuts, the new value of a field
  544. is still available, but not the way it was previously. Instead of
  545. using the square bracket syntax, we use an <code>args</code> pointcut.
  546. All set join points are assumed to have exactly one argument, which
  547. holds the new value. So, </p>
  548. <PRE>
  549. after(Color newColor): sets(Color Frame.color)[][newColor] { ... }
  550. ==&gt;
  551. after(Color newColor): args(newColor) &amp;&amp; set(Color Frame.color) { ... }
  552. </PRE>
  553. <p> Also, if the field was declared private, in order to get at its
  554. old value the aspect must be declared <code>privileged</code>.
  555. </p>
  556. <h5>Rewriting handlers</h5>
  557. <p> The value of the exception at an exception handler join point is
  558. now accessed through the <code>args</code> pointcut; all exception
  559. handler join points are treated as having exactly one argument, the
  560. exception value. So,
  561. </p>
  562. <PRE>
  563. before(NotFoundException e): handlers(e) { ... }
  564. ==&gt;
  565. before(NotFoundException e): args(e) &amp;&amp; handler(NotFoundException) { ... }
  566. </PRE>
  567. <h5>Rewriting within</h5>
  568. <p> The <code>within</code> pointcut was not typically used to export
  569. context. Though it was accidentally possible to do so in versions of
  570. AspectJ before 1.0, it often didn't do what users expected it to.
  571. This loophole has now been closed, and within can only take type
  572. patterns, not pointcut or advice formals. A use of the
  573. <code>this</code> pointcut will capture what previous implementations
  574. did: </p>
  575. <PRE>
  576. pointcut usesFoo(Foo f): within(f);
  577. ==&gt;
  578. pointcut usesFoo(Foo f): this(f) &amp;&amp; within(Foo);
  579. </PRE>
  580. <h4><a name="1.0a1-no-subs-in-sigs">Understanding signatures</a></h4>
  581. <p> Now that we have <code>this</code>, <code>target</code>, and
  582. <code>args</code> pointcuts, all of our signatures are composed of
  583. just types, names, and wildcards; there are no more parameters.
  584. </p>
  585. <p> Also, now that we have the <code>+</code> wildcard to pick out
  586. <a href="#1.0a1-subtypes-to-plus">subtypes</a>, we can make signature
  587. matching much more uniform.</p>
  588. <p> Previously, some signatures matched based on subtypes, some based
  589. on instanceof, and some exactly. Now, we have made all signatures
  590. match exactly.
  591. </p>
  592. <p> What does this mean for your program? Well, it means that you
  593. may have to add <code>+</code> to some of your signatures, depending
  594. on what you meant them to match.
  595. </p>
  596. <p> For example, the pointcut
  597. </p>
  598. <pre>
  599. calls(void m(Object))
  600. </pre>
  601. <p> previously picked out all method calls to a method named m that
  602. took one argument, which was a subtype of Object. Now, however, it
  603. will only pick out method calls to methods that are defined to take
  604. exactly the type Object, which may be a lot fewer join points. If you
  605. want the old behaviour, simply convert to </p>
  606. <pre>
  607. call(void m(Object+))
  608. </pre>
  609. <h4><a name="1.0a1-fixing-instanceof">Removing the instanceof pointcut</a></h4>
  610. <p> The intanceof pointcut has been split into two different
  611. pointcuts, <code>this</code> and <code>target</code>. </p>
  612. <p> Typically, the instanceof pointcut would only exist in a compound
  613. pointcut, composed (with <CODE>&amp;&amp;</CODE>) with another
  614. pointcut. If the other pointcut was a <code>receptions</code>
  615. pointcut, then <code>instanceof</code> should be converted to
  616. <code>target</code> (and <code>receptions</code> converted to
  617. <code>call</code>). So, </p>
  618. <PRE>
  619. pointcut stateChanges(Subject s):
  620. instanceof(s) &amp;&amp; receptions(void Button.click());
  621. ==&gt;
  622. pointcut stateChange(Subject s):
  623. target(s) &amp;&amp; call(void Button.click());
  624. </PRE>
  625. <p> In all other cases, <code>instanceof</code> referred to the
  626. currently executing object, and so should be converted into
  627. <code>this</code></p>
  628. <PRE>
  629. before(Point p): instanceof(p) &amp;&amp; executions(* makePolar(..)) { ... }
  630. ==&gt;
  631. before(Point p): this(p) &amp;&amp; execution(* makePolar(..)) { ... }
  632. </PRE>
  633. <PRE>
  634. pointcut setup(Client c): instanceof(c) &amp;&amp; calls(Remote Naming.lookup(String));
  635. ==&gt;
  636. pointcut setup(Client c): this(c) &amp;&amp; calls(Remote Naming.lookup(String));
  637. </PRE>
  638. <h4><a name="1.0a1-initializations">Rewriting the initializations pointcut</a></h4>
  639. <p> Object initialization join points are now more complicated, and
  640. more true to Java's execution model. Now they bracket all of the
  641. initialization that a class can do, after the return of its super
  642. constructor call (before which no initialization can happen). Previous
  643. versions of AspectJ had object initialization join points that only
  644. included initialization that was made in dynamic initializers and
  645. fields. </p>
  646. <p> The old behaviour can be recovered with a simple rewrite.
  647. </p>
  648. <PRE>
  649. initializations(A)
  650. ==&gt;
  651. initialization(A.new(..)) &amp;&amp; !execution(A.new(..))
  652. </PRE>
  653. <h4><a name="1.0a1-constructor-calls">Understanding constructor calls</a></h4>
  654. <p> Previously, constructor call join points were matched by subtypes,
  655. so <code>calls(Foo.new())</code> would match both calls to create new
  656. <code>Foo</code> objects, and new <code>SubFoo</code> objects. The
  657. new <code>call</code> pointcut designator matches types exactly, so if
  658. you want the old behaviour, you should write
  659. <code>call(Foo+.new())</code>. </p>
  660. <p> Similarly, constructor execution join points were matched by
  661. subtypes. So the old <code>executions(Foo.new())</code> is now
  662. represented by <code>execution(Foo+.new())</code>.
  663. </p>
  664. <p> In both of these cases, think before using the + operator; it may
  665. be that you didn't intend subtype matching in the first place. </p>
  666. <h4><a name="1.0a1-hasaspect">Removing the hasaspect pointcut</a></h4>
  667. <p> The <code>hasaspect</code> pointcut is no longer defined, but you
  668. can get the same behaviour using the new <code>if</code> pointcut.
  669. </p>
  670. <p> If the aspect whose presense you are checking for was defined
  671. <code>of eachcflow</code>, <code>of eachcflowbelow</code>, or, more
  672. unlikely, <code>of eachJVM()</code>, then the conversion is simple:
  673. </p>
  674. <PRE>
  675. hasaspect(A)
  676. ==&gt;
  677. if(A.hasAspect())
  678. </PRE>
  679. <p> If the aspect was defined <code>of eachobject</code>, then you
  680. will have to expose the current object in your pointcut or advice
  681. parameters: </p>
  682. <PRE>
  683. pointcut cut(): hasaspect(A) ... ;
  684. ==&gt;
  685. pointcut cut(Object o): this(o) &amp;&amp; if(A.hasAspect(o)) ... ;
  686. or
  687. pointcut cut(Object o): target(o) &amp;&amp; if(A.hasAspect(o)) ... ;
  688. </PRE>
  689. <p> If you were using the <code>hasaspect</code> pointcut to expose
  690. the state of the aspect, then you can get the same state by using
  691. <code>A.aspectOf()</code> in the body of the advice. For example, if
  692. the aspect A were defined <code>of eachcflow</code>, then
  693. </p>
  694. <PRE>
  695. before(A myA): hasaspect(myA) {
  696. myA.checkStatus();
  697. }
  698. ==&gt;
  699. before(): if(A.hasAspect()) {
  700. A myA = A.aspectOf();
  701. myA.checkStatus();
  702. }
  703. </PRE>
  704. <h4><a name="1.0a1-withinall">Removing the withinall pointcut</a></h4>
  705. <p> The withinall poinctut is no longer defined. You can use a
  706. combination of within and the <a href="#1.0a1-subtypes-to-plus">new
  707. subtypes operator</a>, +, instead. You'll save two characters and be
  708. using a simpler and more orthogonal language. </p>
  709. <PRE>
  710. withinall(Foo)
  711. ==&gt;
  712. within(Foo+)
  713. </PRE>
  714. <h4><a name="1.0a1-user-defined-returns">Removing returns modifier from pointcuts</a></h4>
  715. <p>The returns keyword is no longer necessary for user-defined
  716. pointcuts. Simply remove it when you find it. </p>
  717. <PRE>
  718. pointcut publicIntCalls() returns int: calls(public int *(..));
  719. ==&gt;
  720. pointcut publicIntCall(): call(public int *(..));
  721. </PRE>
  722. <h4><a name="1.0a1-static-pointcuts">Making some pointcuts static</a></h4>
  723. <p> In Java, only static members may be accessed by their declaring
  724. type name, like the static method <code>Math.max()</code> can be
  725. accessed. </p>
  726. <p> Pointcuts now have that property too. Pointcuts may be declared
  727. to be static, in which case they can be accessed like
  728. <code>MyAspect.move()</code>, or they can be left non-static, in which
  729. case they can be overridden by a subaspect. </p>
  730. <p> In addition, while pointcuts can still be defined in classes, only
  731. <code>static</code> pointcuts can be defined in classes. </p>
  732. <p> Porting should be straightforward; just make all your pointcuts in
  733. classes <code>static</code>, and make any pointcut with a qualified
  734. reference static.
  735. </p>
  736. <h3><a name="1.0a1-type-patterns">Type patterns</a></h3>
  737. <h4><a name="1.0a1-new-wildcards">Understanding * and .. in type patterns</a></h4>
  738. <p> Previous versions of AspectJ treated * and .. too cleverly in type
  739. patterns, placing restrictions based on what is a package and what is
  740. a type, and basing their meanings on the definition of a package
  741. hierarchy. </p>
  742. <p> In AspectJ 1.0, both of these wildcards are defined simply, and
  743. textually:
  744. </p>
  745. <ul>
  746. <li> The * wildcard alone matches all types. </li>
  747. <li> The * wildcard in a pattern matches zero or more characters,
  748. but will not match "." </li>
  749. <li> The .. wildcard matches any sequence of characters that begins
  750. and ends with "." </li>
  751. </ul>
  752. <p> That's it.
  753. </p>
  754. <p> This change won't affect most programs, but it will make
  755. understanding programs easier. There is one ugly idiom, however, that
  756. this change disposes of. If your program includes the type pattern
  757. <code>*..*</code>, which used to match all types, you can replace it with the
  758. much simpler *. </p>
  759. <PRE>
  760. pointcut unaryVoidMethods(): call(void *(*..*));
  761. ==&gt;
  762. pointcut unaryVoidMethod(): call(void *(*));
  763. </PRE>
  764. <h4><a name="1.0a1-subtypes-to-plus">Fixing subtypes in introduction</a></h4>
  765. <p> The new + operator is used to normalize the many places you want
  766. to use subtypes of some types.
  767. </p>
  768. <p> In introduction forms, you will need to replace
  769. <code>subtypes(<var>TypePattern</var>)</code> type patterns with the
  770. new subtype operator, +. In the case where you wrote
  771. <code>subtypes(Foo)</code>, i.e., the subtypes of a single type,
  772. simply replace this with <code>Foo+</code>. Otherwise, use the
  773. + operator as appropriate in <var>TypePattern</var>. </p>
  774. <PRE>
  775. public void (subtypes(Target0 || Target1)).accept(Visitor v) {
  776. v.visit(this);
  777. }
  778. ==&gt;
  779. public void (Target0+ || Target1+).accept(Visitor v) {
  780. v.visit(this);
  781. }
  782. </PRE>
  783. <h3><a name="1.0a1-advice">Advice</a></h3>
  784. <h4><a name="1.0a1-around-returns">Moving the return type of around</a></h4>
  785. <p> The returns keyword is no longer used for around advice. Instead,
  786. the return type is declared as it is for methods. So, </p>
  787. <PRE>
  788. around(Point p) returns void: setters(p) { ... }
  789. ==&gt;
  790. void around(Point p): setter(p) { ... }
  791. </PRE>
  792. <h4><a name="1.0a1-around-throws">Adding a throws clause to around</a></h4>
  793. <p> Around advice must now declare the checked exceptions it throws
  794. with a <code>throws</code> clause, much like a method.
  795. </p>
  796. <PRE>
  797. char around(char c) throws java.io.CharConversionException: converter(c) {
  798. char result;
  799. try { result = proceed(); }
  800. catch (Exception e) {
  801. throw new java.io.CharConversionException();
  802. }
  803. if (result == 0) throw new java.io.CharConversionException();
  804. return result;
  805. }
  806. </PRE>
  807. <h4><a name="1.0a1-advice-precedence">Understanding advice precedence</a></h4>
  808. <p> In previous versions of AspectJ, advice precedence within an
  809. aspect was simple: if a piece of advice appeared before another piece,
  810. it was more precedent. This made perfect sense for
  811. <code>before</code> and <code>around</code> advice, but was the cause
  812. of confusion (even among the AspectJ designers, more than once) for
  813. <code>after</code> advice, as it seemed backward. </p>
  814. <p> In addition, advice was ordered by kind, in that around advice
  815. always surrounded before and after advice.
  816. </p>
  817. <p> AspectJ 1.0 has changed this; precedence for <code>after</code>
  818. advice is inverted, and advice is no longer ordered by kind.
  819. </p>
  820. <p>This won't matter to you unless you write pieces of advice in the
  821. same aspect that apply to the same join point. </p>
  822. <p>If you do, here's what to think about: If you're looking at two
  823. pieces of advice and want to know which has precedence, if either is
  824. <code>after</code> advice, then the second one has precedence.
  825. Otherwise, the first does. </p>
  826. <p> This allows interesting advice interaction. In the following
  827. advice, for example, the <code>after throwing</code> advice will catch
  828. the exception thrown by the <code>before</code> advice </p>
  829. <PRE>
  830. aspect A {
  831. before(): call(void main(..)) {
  832. throw new RuntimeException();
  833. }
  834. after() throwing(RuntimeException e): call(void main(..)) {
  835. System.err.println("caught you!");
  836. }
  837. }
  838. </PRE>
  839. <p> But reversing the order will give the <code>before</code> advice
  840. more precedence, making its exception uncatchable by the <code>after
  841. throwing</code> advice
  842. </p>
  843. <PRE>
  844. aspect A {
  845. after() throwing(RuntimeException e): call(void main(..)) {
  846. System.err.println("missed you!");
  847. }
  848. before(): call(void main(..)) {
  849. throw new RuntimeException();
  850. }
  851. }
  852. </PRE>
  853. <p> Advice in <em>different</em> aspects is ordered by the normal aspect
  854. precedence rules of subtyping and the <code>dominates</code> modifier.
  855. </p>
  856. <h4><a name="1.0a1-after-returning">Fixing after returning</a></h4>
  857. <p> If you use after returning advice and do not need to expose the
  858. return value, you no longer need to write an empty set of parentheses
  859. to indicate that fact. So, </p>
  860. <pre>
  861. after(<var>Formals</var>) returning (): <var>Pointcut</var> { ... }
  862. ==&gt;
  863. after(<var>Formals</var>) returning: <var>Pointcut</var> { ... }
  864. </pre>
  865. <p> The same syntax is now available for after throwing advice, in
  866. case you do not care what <code>Throwable</code> is thrown.
  867. </p>
  868. <pre>
  869. after(<var>Formals</var>) throwing: <var>Pointcut</var> { ... }
  870. </pre>
  871. <h4><a name="1.0a1-this-static-join-point">Renaming thisStaticJoinPoint</a></h4>
  872. <p> <code>thisStaticJoinPoint</code> has been renamed
  873. <code>thisJoinPointStaticPart</code>, to reflect that it is now
  874. exactly the static part of <code>thisJoinPoint</code>: It will return
  875. the same object as <code>thisJoinPoint.getStaticPart()</code>. </p>
  876. <h4><a name="1.0a1-this-join-point">Converting access to thisJoinPoint</a></h4>
  877. <p> The <code>JoinPoint</code> object hierarchy has been folded into a
  878. single class, <code>org.aspectj.lang.JoinPoint</code>. A common
  879. pattern in logging, for example, was </p>
  880. <pre>
  881. before() executions(* myMethod()) {
  882. ExecutionJoinPoint jp = (ExecutionJoinPoint)thisJoinPoint;
  883. CodeSignature jp = (CodeSignature)jp.getSignature();
  884. System.err.println(jp.getParameters());
  885. System.err.println(jp.getParameterNames());
  886. }
  887. </pre>
  888. <p> While there is still a rich hierarchy for signatures, there is
  889. only one <code>JoinPoint</code> type, so this can be rewritten as:
  890. </p>
  891. <pre>
  892. before() executions(* myMethod()) {
  893. JoinPoint jp = thisJoinPoint;
  894. CodeSignature jp = (CodeSignature)jp.getSignature();
  895. System.err.println(jp.getArgs());
  896. System.err.println(jp.getParameterNames());
  897. }
  898. </pre>
  899. <p> Some of the method names of <code>JoinPoint</code> have been
  900. reorganized, as well. </p>
  901. <h3><a name="1.0a1-introduction-and-static">Introduction and static crosscutting</a></h3>
  902. <h4><a name="1.0a1-plus-implements-extends">Removing +implements and +extends</a></h4>
  903. <p> The keywords <code>+implements</code> and <code>+extends</code> no
  904. longer exist. Instead, AspectJ uses the <code>declare</code>
  905. form for exactly the same functionality. </p>
  906. <PRE>
  907. Point +implements Serializable;
  908. =&gt;
  909. declare parents: Point implements Serializable;
  910. </PRE>
  911. <PRE>
  912. MyButton +extends ButtonAdaptor;
  913. =&gt;
  914. declare parents: MyButton extends ButtonAdaptor;
  915. </PRE>
  916. <h4><a name="1.0a1-now-use-soft">Using declare soft</a></h4>
  917. <p> Around advice advice no longer effects the static exception
  918. checking of Java. This means that the following code previously
  919. compiled: </p>
  920. <PRE>
  921. class C {
  922. void noExceptionDeclared() {
  923. exceptionDeclared();
  924. }
  925. void exceptionDeclared() throws IOException {}
  926. }
  927. aspect A {
  928. around(): call(void C.exceptionDeclared()) {
  929. try { proceed(); }
  930. catch (IOException e) {}
  931. }
  932. }
  933. </PRE>
  934. <p> even though the class C is not compilable on its own (because
  935. noExceptionDeclared actually throws an Exception).
  936. </p>
  937. <p> AspectJ now firmly places everything that affects the type system
  938. of Java, including the declared-exception checking system, into the
  939. space of introduction and declare. So, in order to state that the
  940. call to exceptionDeclared() will not, actually, throw an exception, we
  941. now "soften" that exception, that is, take it out of the space of
  942. declared exceptions. </p>
  943. <pre>
  944. declare soft: <var>ExceptionType</var>: <var>Pointcut</var>;
  945. </pre>
  946. <p> The pointcuts allowed here are limited; you cannot use pointcuts
  947. that would require runtime information. But picking out method calls
  948. is just fine. So in order to make the above example work, one new
  949. declaration is needed:
  950. </p>
  951. <PRE>
  952. declare soft: IOException:
  953. call(void C.exceptionDeclared()) &amp;&amp;
  954. withincode(void noExceptionDeclared());
  955. </PRE>
  956. <h3><a name="1.0a1-aspects">Aspects</a></h3>
  957. <p> The syntax of "of each" modifiers has changed. For <code>of
  958. eachcflow</code> and <code>of eachcflowbelow</code>, you can simply
  959. replace "of each" with "per". So, </p>
  960. <PRE>
  961. aspect A of eachcflow(...) { ... }
  962. ==&gt;
  963. aspect A percflow(...) { ... }
  964. </PRE>
  965. <p> If you have any aspects defined <code>of eachJVM()</code>, then
  966. you should either remove that declaration entirely (because this is
  967. the default behaviour), or replace the <code>of eachJVM()</code>
  968. declaration with an <code>issingleton</code> declaration.
  969. </p>
  970. <PRE>
  971. aspect of eachJVM() { ... }
  972. ==&gt;
  973. aspect A { ... }
  974. or
  975. aspect A issingleton { ... }
  976. </PRE>
  977. <p> The <code>of eachobject(<var>Pointcut</var>)</code> modifier has
  978. been split into two different forms, <code>of
  979. perthis(<var>Pointcut</var>)</code> and <code>of
  980. pertarget(<var>Pointcut</var>)</code>. Which one you replace with
  981. depends on the <var>Pointcut</var> you use.
  982. </p>
  983. <p> If you use a pointcut that picked out reception join points, then
  984. use <code>pertarget</code>, and rewrite the pointcut to pick out call
  985. join points. So
  986. </p>
  987. <PRE>
  988. aspect Shadow
  989. of eachobject(receptions(void Point.setX(int)) ||
  990. receptions(void Point.setY(int))) {
  991. ...
  992. }
  993. ==&gt;
  994. aspect Shadow pertarget(call(void Point.setX(int)) ||
  995. call(void Point.setY(int))) {
  996. ...
  997. }
  998. </PRE>
  999. <p> Otherwise, in most cases, use <code>perthis</code>. When you
  1000. convert, remember the meaning of each of these modifiers.
  1001. <code>perthis(<var>Pointcut</var>)</code> indicates that an instance
  1002. of the aspect should be associated with every object that is
  1003. <code>this</code> at each of the join points picked out by
  1004. <var>Pointcut</var>, while <code>pertarget(<var>Pointcut</var>)</code>
  1005. associates with every object that is the target object at such join
  1006. points. </p>
  1007. <!-- ==================================== -->
  1008. <!-- ==================================== -->
  1009. <!-- ==================================== -->
  1010. <hr />
  1011. <h2><a name="pre08b3">Porting pre-0.8beta3 code</a></h2>
  1012. <ul>
  1013. <li><a href="#cflowTerminology">Changing cflow terminology</a></li>
  1014. <li><a href="#abstractPointcuts">Overriding abstract pointcuts</a></li>
  1015. <li><a href="#recursiveAdvice">Limiting recursive advice</a></li>
  1016. </ul>
  1017. <p>The following changes are only required when porting code written
  1018. prior to the 0.8beta3 release of AspectJ.</p>
  1019. <h3><a name="cflowTerminology">Changing cflow terminology</a></h3>
  1020. <p> Changing pre-0.8beta3 code that uses AspectJ's control-flow-based
  1021. features only requires rewriting occurrences of
  1022. <code>eachcflowroot</code>, <code>cflow</code>, and
  1023. <code>cflowtop</code>. No editing of other aspect code is
  1024. necessary.</p>
  1025. <h4>eachcflowroot</h4>
  1026. <p> The aspect modifier "<code>of
  1027. eachcflowroot(<var>Pointcut</var>)</code>" should now be written more
  1028. as "<code>percflow(<var>Pointcut</var>)</code>". </p>
  1029. <h4>cflow</h4>
  1030. <p> In previous versions of AspectJ, the pointcut
  1031. <code>cflow(<var>Pointcut</var>)</code> picked out all join points in
  1032. the cflow below the join points of <var>Pointcut</var>. That is, it
  1033. did not include the join points of <var>Pointcut</var>, only the join
  1034. points in their control flow.
  1035. </p>
  1036. <p> As of version 0.8beta3,
  1037. <code>cflowbelow(<var>Pointcut</var>)</code> has that behavior.
  1038. <code>cflow(<var>Pointcut</var>)</code> includes the join points of
  1039. <var>Pointcut</var>. </p>
  1040. <p> In many cases, you may not care whether the points of
  1041. <var>Pointcut</var> are included or not, and so can safely leave
  1042. <code>cflow(<var>Pointcut</var>)</code> pointcut designators alone.
  1043. However, if you use the idiom
  1044. </p>
  1045. <pre class="codeindent">
  1046. <var>Pointcut</var> && ! cflow(<var>Pointcut</var>)
  1047. </pre>
  1048. <p> to capture the non-recursive entries to a particular pointcut, you
  1049. will definitely want to rewrite that as
  1050. </p>
  1051. <pre class="codeindent">
  1052. <var>Pointcut</var> && ! cflowbelow(<var>Pointcut</var>)
  1053. </pre>
  1054. <h4>cflowtop</h4>
  1055. <p> The primitive pointcut designator
  1056. <code>cflowtop(<var>Pointcut</var>)</code> has been removed from the
  1057. language, as it is expressible with <code>cflow</code> or
  1058. <code>cflowbelow</code>. All uses of
  1059. <code>cflowtop(<var>Pointcut</var>)</code> can be rewritten as:
  1060. </p>
  1061. <pre class="codeindent">
  1062. cflowbelow(<var>Pointcut</var> && ! cflowbelow(<var>Pointcut</var>))
  1063. </pre>
  1064. <p> Though in most cases the following is sufficient
  1065. </p>
  1066. <pre class="codeindent">
  1067. cflow(<var>Pointcut</var> && ! cflowbelow(<var>Pointcut</var>))
  1068. </pre>
  1069. <h3><a name="abstractPointcuts">Overriding abstract pointcuts</a></h3>
  1070. <p> In previous versions of AspectJ, a concrete aspect would
  1071. implicitly override all of its abstract pointcuts with an empty
  1072. pointcut. AspectJ 0.8beta3 enforces the restriction that a concrete
  1073. aspect may not have any abstract pointcuts. Thus the following
  1074. extension:</p>
  1075. <pre class="codeindent">
  1076. abstract aspect A {
  1077. abstract pointcut pc();
  1078. }
  1079. aspect B {}
  1080. </pre>
  1081. <p> will no longer compile.
  1082. </p>
  1083. <p> Adding the new empty pointcut designator
  1084. </p>
  1085. <pre class="codeindent">
  1086. pointcut <var>Id</var>();
  1087. </pre>
  1088. <p> in the declaration of the concrete aspect fixes this problem.
  1089. </p>
  1090. <pre class="codeindent">
  1091. abstract aspect A {
  1092. abstract pointcut pc();
  1093. }
  1094. aspect B {
  1095. pointcut pc();
  1096. }
  1097. </pre>
  1098. <h3><a name="recursiveAdvice">Limiting recursive advice</a></h3>
  1099. <p> Previously, the compiler silently refrained from applying a piece
  1100. of advice to join points within its own advice body. So, for example,
  1101. in </p>
  1102. <pre class="codeindent">
  1103. class C {
  1104. static int i;
  1105. }
  1106. aspect A {
  1107. before(): gets(int C.i) {
  1108. System.err.println("C.i was " + C.i)
  1109. }
  1110. }
  1111. </pre>
  1112. <p> The advice would trace all references of the static field
  1113. <code>C.i</code> except those in the body of the before. </p>
  1114. <p> The compiler has now removed this special case, and so running the
  1115. above example will now cause a <code>StackOverflowException</code> to
  1116. be thrown. </p>
  1117. <p> Most cases of this error can be fixed by correctly specifying the
  1118. desired pointcut: In the above example, the intention is clearly not
  1119. to trace <em>all</em> references of <code>C.i</code>, just those
  1120. outside the aspect.
  1121. </p>
  1122. <pre class="codeindent">
  1123. class C {
  1124. static int i;
  1125. }
  1126. aspect A {
  1127. before(): get(int C.i) && ! within(A) {
  1128. System.err.println("C.i was " + C.i)
  1129. }
  1130. }
  1131. </pre>
  1132. <p> In a very few cases, you may want the advice to be applicable to
  1133. other code in the aspect, but not in the particular piece of advice.
  1134. In such cases, you can pull the body of the advice into a method and
  1135. restrict away from that method (and away from calls to that method):
  1136. </p>
  1137. <pre class="codeindent">
  1138. class C {
  1139. static int i;
  1140. }
  1141. aspect A {
  1142. public static int getCi() {
  1143. return C.i; // will be traced
  1144. }
  1145. before(): get(int C.i) &&
  1146. ! withincode(void A.traceCi())
  1147. ! call(void A.traceCi()) {
  1148. traceCi();
  1149. }
  1150. private void traceCi() {
  1151. System.err.println("C.i was " + C.i) // will not be traced
  1152. }
  1153. }
  1154. </pre>
  1155. <!-- ============================== -->
  1156. <hr />
  1157. <h2><a name="pre08b1">Porting pre-0.8beta1 code</a></h2>
  1158. <ul>
  1159. <li><a href="#introSyntax">Rewriting introductions</a></li>
  1160. <li><a href="#staticAdvice">Removing static advice</a></li>
  1161. <li><a href="#aspect-aspect">Fixing aspect-aspect inheritance</a></li>
  1162. <li><a href="#usingPrivateIntroduction">Using private introduction</a></li>
  1163. </ul>
  1164. <p>The following changes are only required when porting code written
  1165. prior to the 0.8beta1 release of AspectJ.</p>
  1166. <h3><a name="introSyntax">Rewriting introductions</a></h3>
  1167. <h4>Syntax</h4>
  1168. <p> The syntax of introduction has changed. Porting most programs
  1169. should require some simple editing. Anywhere you have an introduction
  1170. block</p>
  1171. <pre class="codeindent">
  1172. introduction <var>GTN</var> {
  1173. ...
  1174. }
  1175. </pre>
  1176. <p> simply move the <var>GTN</var> down into the introduction
  1177. declarations and remove the block.</p>
  1178. <p>For method introduction, place the <var>GTN</var> in front of the
  1179. method name, For field introduction, place the <var>GTN</var> in front
  1180. of the field name, and for constructor introduction, place the
  1181. <var>GTN</var> in front of the <code>new</code> identifier. </p>
  1182. <pre class="codeindent">
  1183. introduction Foo {
  1184. public void doStuff() { this.doStuffLater(); }
  1185. public int calorieCount = 3;
  1186. public new(int x) { super(); calorieCount = x; }
  1187. }
  1188. ==&gt;
  1189. public void Foo.doStuff() { this.doStuffLater(); }
  1190. public int Foo.calorieCount= 3;
  1191. public Foo.new(int x) { super(); calorieCount = x; }
  1192. </pre>
  1193. <p> For implements and extends introduction, move the <var>GTN</var>
  1194. in front of the new identifiers <code>implements</code> or
  1195. <code>extends</code>, and place that in a <code>declare parents</code>
  1196. form.
  1197. </p>
  1198. <pre class="codeindent">
  1199. introduction Foo {
  1200. implements Comparable;
  1201. extends Goo;
  1202. }
  1203. ==&gt;
  1204. declare parents: Foo implements Comparable;
  1205. declare parents: Foo extends Goo;
  1206. </pre>
  1207. <p> In all cases, if the <var>GTN</var> is just a type name, it can be
  1208. moved down on its own. However, if the <var>GTN</var> uses any of
  1209. <CODE>&amp;&amp;</CODE>, <code>||</code>, and <code>!</code>, it must
  1210. be parenthesized. </p>
  1211. <pre class="codeindent">
  1212. introduction subtypes(Foo) &amp;&amp; !Goo {
  1213. int x;
  1214. }
  1215. ==&gt;
  1216. int (Foo+ &amp;&amp; !Goo).x;
  1217. </pre>
  1218. <h4>Access</h4>
  1219. <p>If you had an introduction that was referring to private or
  1220. protected members of the target class, this will no longer work. You
  1221. will either need to modify your code to avoid this accessibility
  1222. issue, or you will need to use the <code>privileged</code> modifier on
  1223. the aspect that contains the introduction.</p>
  1224. <pre class="codeindent">
  1225. class Counter {
  1226. private int count = 2;
  1227. }
  1228. aspect ExposeCountersPrivates {
  1229. introduction Counter {
  1230. public int getCount() { return count; }
  1231. }
  1232. }
  1233. ==&gt;
  1234. // in 0.8, only privileged aspects can expose a class's privates
  1235. privileged aspect ExposeCountersPrivates {
  1236. public int Counter.getCount() { return count; }
  1237. }
  1238. </pre>
  1239. <p> If you have introduced private or package-protected members, you
  1240. will probably have to re-write some code. Most previous uses of
  1241. introducing privates can be improved by using private introduction
  1242. instead.</p>
  1243. <pre class="codeindent">
  1244. class C {
  1245. }
  1246. aspect AddCounter {
  1247. introduction C {
  1248. private int count;
  1249. public int getCount() { return count; }
  1250. }
  1251. }
  1252. ==&gt;
  1253. aspect AddCounter {
  1254. private int Counter.count;
  1255. public int Counter.getCount() { return count; }
  1256. }
  1257. </pre>
  1258. <p> There is one case that we know of where the inability to perform
  1259. the introduction of private members makes 0.7 code difficult to
  1260. port to 0.8. If you were using the introduction of a <code>private
  1261. void writeObject(..)</code> or a <code>private void
  1262. readObject(..)</code> method to interact with Java's serialization
  1263. API, you will need to come up with an alternative design. Using some
  1264. combination of <code>Externalizable</code>,
  1265. <code>writeReplace(..)</code> and/or <code>readResolve(..)</code>
  1266. methods should allow you to port your code. If you find this isn't
  1267. the case, we'd like to hear about it.
  1268. <p> If you were introducing either a protected member or a
  1269. package-private member onto a class in order to override a protected
  1270. member that was inherited from a superclass, you will have to make
  1271. this introduction public. <p>
  1272. <h3><a name="staticAdvice">Removing static advice</a></h3>
  1273. <p> Static advice has been removed from the language. Now, every
  1274. piece of advice is non-static, meaning that it will run in the context
  1275. of an aspect instance.
  1276. </p>
  1277. <p> If you have an aspect that only contains static advice, has no
  1278. "of" clause or is declared "of eachJVM()", and is not extended by
  1279. another aspect, simply remove the keyword "static" from all pieces of
  1280. advice, and make sure the aspect is not defined with the "abstract"
  1281. modifier. </p>
  1282. <pre class="codeindent">
  1283. aspect Tracing {
  1284. static before(): executions(* *(..)) {
  1285. System.out.println("Got Here! " + thisJoinPoint);
  1286. }
  1287. }
  1288. ==&gt;
  1289. aspect Tracing {
  1290. before(): execution(* *(..)) {
  1291. System.out.println("Got Here! " + thisJoinPoint);
  1292. }
  1293. }
  1294. </pre>
  1295. <p> Otherwise, if you have an aspect contains both static and
  1296. non-static advice, is extended, or is "of eachObject(...)" or "of
  1297. eachcflowroot(...)", you should group your static advice together and
  1298. put it in a new aspect, possibly even an inner aspect. </p>
  1299. <pre class="codeindent">
  1300. aspect ComplexTracing of eachobject(cflow(executions(void Main.main(..)))) {
  1301. static before(): executions(* *(..)) {
  1302. System.out.println("Got Here! " + thisJoinPoint);
  1303. }
  1304. static after(): executions(* *(..)) {
  1305. System.out.println("Returned! " + thisJoinPoint);
  1306. }
  1307. // some other dynamic advice, fields, etc
  1308. }
  1309. ==&gt;
  1310. aspect ComplexTracing of eachobject(cflow(executions(void Main.main(..)))) {
  1311. static aspect AlwaysTracing {
  1312. before(): execution(* *(..)) {
  1313. System.out.println("Got Here! " + thisJoinPoint);
  1314. }
  1315. after(): execution(* *(..)) {
  1316. System.out.println("Returned! " + thisJoinPoint);
  1317. }
  1318. }
  1319. // some other dynamic advice, fields, etc
  1320. }
  1321. </pre>
  1322. <h3><a name="aspect-aspect">Fixing aspect-aspect inheritance</a></h3>
  1323. <p> Aspects can now only extend abstract aspects. This restriction
  1324. may cause some redesign of aspect hierarchies. You will probably find
  1325. that for the majority of your code the most serious change this
  1326. requires is to add an explicit <code>abstract</code> modifier to a
  1327. super-aspect that was already implicitly abstract.</p>
  1328. <pre class="codeindent">
  1329. aspect BaseTracing {
  1330. abstract pointcut traced();
  1331. before(): traced() {
  1332. System.out.println("Got Here! " + thisJoinPoint);
  1333. }
  1334. }
  1335. ==&gt;
  1336. // make this abstract aspect explicitly abstract
  1337. abstract aspect BaseTracing {
  1338. ...
  1339. }
  1340. </pre>
  1341. <p> This change has also affected the <code>getAspect</code> static
  1342. method. Now, <code>getAspect</code> is only defined on non-abstract
  1343. aspects. Previously, you could call <code>getAspect</code> on an
  1344. abstract superaspect and (sometimes) get an instance of a subaspect
  1345. back. </p>
  1346. <p>This pattern was used in the Spacewar example in the AspectJ
  1347. distribution. We had the class hierarchy </p>
  1348. <pre>
  1349. SpaceObject (abstract)
  1350. |- Ship
  1351. |- Bullet
  1352. |- EnergyPellet
  1353. </pre>
  1354. <p> And the aspect hierarchy
  1355. </p>
  1356. <pre>
  1357. SpaceObjectDA (abstract)
  1358. |- ShipDA of eachobject(instanceof(Ship))
  1359. |- BulletDA of eachobject(instanceof(Ship))
  1360. |- EnergyPacketDA of eachobject(instanceof(Ship))
  1361. </pre>
  1362. <p> And we would call <code>SpaceObjectDA.getAspect(SpaceObject)</code> to access
  1363. the aspect associated with a ship, bullet, or energy pellet. This
  1364. pattern depended on the <code>SpaceObjectDA</code> aspect hierarchy
  1365. exactly mirroring the <code>SpaceObject</code> hierarchy, and being
  1366. maintained that way. </p>
  1367. <p> A better way to implement this kind of design aspect is to use
  1368. private introduction, a new feature of AspectJ.
  1369. </p>
  1370. <h3><a name="usingPrivateIntroduction">Using private introduction</a></h3>
  1371. <p> A common pattern for AspectJ programs that need to associate some
  1372. state with every object of a particular type has been to use aspects
  1373. that are defined <code>of eachobject(instanceof(...))</code>. A prime
  1374. example of this was the <code>BoundPoint</code> aspect of the bean
  1375. example: which needed to associate each point with a
  1376. <code>PropertyChangeSupport</code> object. </p>
  1377. <pre class="codeindent">
  1378. aspect BoundPoint of eachobject(instanceof(Point)) {
  1379. java.beans.PropertyChangeSupport support = null;
  1380. after() returning(Point p): receptions(p.new(..)){
  1381. support = new PropertyChangeSupport(myPoint);
  1382. }
  1383. around(Point p) returns void: receptions(void p.set*(*)) {
  1384. // code that uses support
  1385. }
  1386. }
  1387. </pre>
  1388. <p> In the new version of AspectJ, a better way of accomplishing many
  1389. of these state association is to use privately introduced fields.
  1390. Instead of creating an aspect instance for every <code>Point</code>
  1391. object, store the <code>PropertyChagneSupport</code> object in the
  1392. <code>Point</code> objects themselves.
  1393. </p>
  1394. <pre class="codeindent">
  1395. aspect BoundPoint {
  1396. private PropertyChangeSupport Point.support = new PropertyChangeSupport(this);
  1397. void around(Point p): setters(p) {
  1398. // code that uses p.support
  1399. }
  1400. }
  1401. </pre>
  1402. <p> Just as in the past, the PropertyChangeSupport object is not
  1403. accessable to anyone but the aspect, but now less mechanism is needed.
  1404. </p>
  1405. <p> There are times when changing aspects that are defined <code>of
  1406. eachobject(instanceof(...))</code> may not be reasonable. If the
  1407. aspect instance is stored or passed to other methods, then having a
  1408. real <code>of eachobject(instanceof(...))</code>, now written
  1409. <code>perthis(this(...))</code>, association may capture the
  1410. crosscutting concern best. </p>
  1411. <!-- ============================== -->
  1412. <hr />
  1413. <h2><a name="pre07b11">Porting pre-0.7beta11 code</a></h2>
  1414. <ul>
  1415. <li><a href="#twoArgumentCalls">Removing two-argument calls</a></li>
  1416. <li><a href="#adviceInClasses">Removing advice from Class declarations</a></li>
  1417. </ul>
  1418. <p>The following changes are only required when porting code written
  1419. prior to the 0.7beta11 release of AspectJ.</p>
  1420. <h3><a name="twoArgumentCalls">Removing two-argument calls</a></h3>
  1421. <p> In AspectJ 0.7beta11, the two-argument <code>calls</code>
  1422. primitive pointcut designator was deprecated. Removing these
  1423. designators will require different cases depending on what the
  1424. original pointcut did. </p>
  1425. <h4>Calls to static methods</h4>
  1426. <p> For pointcuts denoting calls to particular static methods, such as
  1427. </p>
  1428. <blockquote><pre>
  1429. calls(String, static String valueOf(int)) // deprecated
  1430. </pre></blockquote>
  1431. <p> the transformation is easy. Simply make the desired signature
  1432. explicit. Instead of catching all calls to any static method that
  1433. happens to have the signature <code>String valueOf(int)</code>, catch
  1434. calls to that exact method defined in the String class. </p>
  1435. <blockquote><pre>
  1436. call(static String String.valueOf(int))
  1437. </pre></blockquote>
  1438. <p> Pointcuts denoting calls to classes of static methods can also be
  1439. rewritten with these rules. For example, </p>
  1440. <blockquote><pre>
  1441. calls(my.package.*, static * get*(..)) // deprecated
  1442. </pre></blockquote>
  1443. <p> should now be written </p>
  1444. <blockquote><pre>
  1445. call(static * my.package.*.get*(..))
  1446. </pre></blockquote>
  1447. <h4>Calls to non-static methods</h4>
  1448. <p> Many pointcuts denoting calls to non-static methods can be
  1449. fixed the same way that those pointcuts denoting calls to static
  1450. methods are fixed. So,
  1451. </p>
  1452. <blockquote><pre>
  1453. calls(Thread, int getPriority()) // deprecated
  1454. </pre></blockquote>
  1455. <p> which denotes all calls to nullary int methods named <code>getPriority</code>
  1456. when the called object is an instance of the <code>Thread</code> type,
  1457. can almost always be rewritten </p>
  1458. <blockquote><pre>
  1459. call(int Thread.getPriority())
  1460. </pre></blockquote>
  1461. <p> which denotes all calls to the nullary int <code>Thread.getPriority()</code>
  1462. method.
  1463. </p>
  1464. <p> Expanding the signature picks out slightly different join points
  1465. than the original two-argument form. This won't matter for most
  1466. programs, but in some cases the differences may be noticable. In
  1467. particular, the expanded-signature form only picks out those calls
  1468. where the called object is statically typed to <code>Thread</code>
  1469. when its <code>int getPriority()</code> method is called. If you want
  1470. to capture calls to the <code>int Thread.getPriority()</code> method,
  1471. regardless of how the called object is statically typed, you shoud use
  1472. the different translation: </p>
  1473. <blockquote><PRE>
  1474. call(int getPriority()) &amp;&amp; target(Thread)
  1475. </PRE></blockquote>
  1476. <p> This will capture all call join points of methods with signature
  1477. <code>int Thread.getPriority()</code>. </p>
  1478. <p> It will also denote any join points if the Thread type does not
  1479. define (possibly abstractly) some <code>int getPriority()</code>
  1480. method, though. </p>
  1481. <h3><a name="adviceInClasses">Removing advice from Class declarations</a></h3>
  1482. <p> The simplest way to remove an advice declaration from a class is
  1483. to simply define the advice declaration in an inner aspect. So,
  1484. instead of </p>
  1485. <blockquote><pre>
  1486. class C {
  1487. static before(): executions(C.new()) { ... } // deprecated
  1488. }
  1489. </pre></blockquote>
  1490. <p> write </p>
  1491. <blockquote><pre>
  1492. class C {
  1493. static aspect ConstructionProtocol {
  1494. static before(): executions(C.new()) { ... }
  1495. }
  1496. }
  1497. </pre></blockquote>
  1498. <p> If your advice doesn't refer to any inner classes or interfaces of
  1499. C, you can move the inner aspect out of the class entirely. </p>
  1500. <blockquote><pre>
  1501. class C { ... }
  1502. aspect ConstructionProtocol {
  1503. static before(): execution(C.new()) { ... }
  1504. }
  1505. </pre></blockquote>
  1506. <p> Your code will be clearer if you consider the purpose of each
  1507. piece of advice when you make this change. It may be that some of the
  1508. advice naturally belongs to another aspect, perhaps already existing.
  1509. Or it may be that some pieces of advice in a class are associated to
  1510. one concern and some to another; in which case more than aspect would
  1511. be appropriate. </p>
  1512. <!-- ============================== -->
  1513. <hr />
  1514. <h2><a name="pre07b10">Porting pre-0.7beta10 code</a></h2>
  1515. <ul>
  1516. <li><a href="#joinPoints">Changing access to thisJoinPoint</a></li>
  1517. </ul>
  1518. <p>The following changes are only required when porting code written
  1519. prior to the 0.7beta10 release of AspectJ.</p>
  1520. <h3><a name="joinPoints">Changing access to thisJoinPoint</a></h3>
  1521. <p> In AspectJ 0.7beta10, access to the reflective object
  1522. <code>thisJoinPoint</code> substantially changed. The two parts of
  1523. this change were the elimination of the <code>runNext()</code> static
  1524. method, and the use of an interface hierarchy represent the join point
  1525. object. </p>
  1526. <h4><a name="proceed"><code>thisJoinPoint.runNext()</code> to
  1527. <code>proceed()</code></a></h4>
  1528. <p> The elimination of the <code>runNext()</code> static method
  1529. requires almost no porting work. An automatic replacement of the
  1530. string
  1531. </p>
  1532. <blockquote><code>thisJoinPoint.runNext</code></blockquote>
  1533. <p> with the string
  1534. </p>
  1535. <blockquote><code>proceed</code></blockquote>
  1536. <p> will do the job. However, if any around advice used the
  1537. identifier "<code>proceed</code>" as a formal parameter or local
  1538. variable, it must be renamed, and if any aspect used it as a field,
  1539. then references to the field in around advice should be made explicit
  1540. (prefixing the reference with the aspect name or "<code>this</code>",
  1541. depending on whether the field is static or not). </p>
  1542. <h4><a name="thisJoinPoint">Using <code>thisJoinPoint</code></a></h4>
  1543. <p> While access to reflective information through
  1544. <code>thisJoinPoint</code> is more powerful and regular through its
  1545. interface hierarchy, the previous uses must be rewritten. Changing
  1546. your code will likely require manual editing, but in doing so your
  1547. code should get simpler and cleaner. </p>
  1548. <!-- -->
  1549. <p> Many existing uses of the fields on join points can be re-written
  1550. to use one of:
  1551. </p>
  1552. <ul>
  1553. <li><code>thisJoinPoint.toString()</code></li>
  1554. <li><code>thisJoinPoint.toShortString()</code></li>
  1555. <li><code>thisJoinPoint.toLongString()</code></li>
  1556. <li><code>thisJoinPoint.getSignature().toString()</code></li>
  1557. <li><code>thisJoinPoint.getSignature().toShortString()</code></li>
  1558. <li><code>thisJoinPoint.getSignature().toLongString()</code></li>
  1559. </ul>
  1560. <p>For example:
  1561. </p>
  1562. <blockquote><pre>
  1563. System.out.println(thisJoinPoint.className + "." +
  1564. thisJoinPoint.methodName)
  1565. </pre></blockquote>
  1566. <p> can be replaced with
  1567. </p>
  1568. <blockquote><code>System.out.println(thisJoinPoint)</code></blockquote>
  1569. <p> or
  1570. </p>
  1571. <blockquote><code>System.out.println(thisJoinPoint.getSignature().toShortString())</code></blockquote>
  1572. <p> with comparable behavior.
  1573. </p>
  1574. <!-- -->
  1575. <p> Accesses to the parameters field of join points should be changed
  1576. as follows. A field access like:
  1577. </p>
  1578. <blockquote><code>thisJoinPoint.parameters</code></blockquote>
  1579. <p> must be changed to:
  1580. </p>
  1581. <ul>
  1582. <li><code>thisJoinPoint.getArgs()</code></li>
  1583. </ul>
  1584. <!-- -->
  1585. <p> Accesses to the methodName and className fields of join points
  1586. that are not suitable for replacement with a toString method,
  1587. should be changed as follows. Field accesses like:
  1588. </p>
  1589. <ul>
  1590. <li><code>thisJoinPoint.className</code></li>
  1591. <li><code>thisJoinPoint.methodName</code></li>
  1592. </ul>
  1593. <p> must be changed to:
  1594. </p>
  1595. <ul>
  1596. <li><code>thisJoinPoint.getSignature().getDeclaringType().getName()</code></li>
  1597. <li><code>thisJoinPoint.getSignature().getName()</code></li>
  1598. </ul>
  1599. <!-- -->
  1600. <p> Accessses to the parameterNames and parameterTypes fields of
  1601. join points, that are not suitable for conversion to one of the
  1602. toString() methods should be changed as follows. Field access
  1603. like:
  1604. </p>
  1605. <ul>
  1606. <li><code>thisJoinPoint.parameterNames</code></li>
  1607. <li><code>thisJoinPoint.parameterTypes</code></li>
  1608. </ul>
  1609. <p> must be changed to:
  1610. </p>
  1611. <ul>
  1612. <li><code>((CodeSignature)thisJoinPoint.getSignature()).getParameterNames()</code></li>
  1613. <li><code>((CodeSignature)thisJoinPoint.getSignature()).getParameterTypes()</code></li>
  1614. </ul>
  1615. </body>
  1616. </html>