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.

ataspectj.xml 36KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. <chapter id="ataspectj" xreflabel="AtAspectJ">
  2. <title>An Annotation Based Development Style</title>
  3. <sect1 id="ataspectj-intro">
  4. <title>Introduction</title>
  5. <para>In addition to the familiar AspectJ code-based style of aspect
  6. declaration, AspectJ 5 also supports an annotation-based style of
  7. aspect declaration. We informally call the set of annotations that
  8. support this development style the "@AspectJ" annotations.</para>
  9. <para>
  10. AspectJ 5 allows aspects and their members to be specified using
  11. either the code style or the annotation style. Whichever style you
  12. use, the AspectJ weaver ensures that your program has exactly the
  13. same semantics. It is, to quote a famous advertising campaign,
  14. "a choice, not a compromise". The two styles can be mixed within
  15. a single application, and even within a single source file, though
  16. we doubt this latter mix will be recommended in practice.
  17. </para>
  18. <para>
  19. The use of the @AspectJ annotations means that there are large
  20. classes of AspectJ applications that can be compiled by a regular
  21. Java 5 compiler, and subsequently woven by the AspectJ weaver (for
  22. example, as an additional build stage, or as late as class load-time).
  23. In this chapter we introduce the @AspectJ annotations and show how
  24. they can be used to declare aspects and aspect members.
  25. </para>
  26. </sect1>
  27. <sect1 id="ataspectj-aspects">
  28. <title>Aspect Declarations</title>
  29. <para>
  30. Aspect declarations are supported by the
  31. <literal>org.aspectj.lang.annotation.Aspect</literal>
  32. annotation.
  33. The declaration:
  34. </para>
  35. <programlisting><![CDATA[
  36. @Aspect
  37. public class Foo {}
  38. ]]></programlisting>
  39. <para>Is equivalent to:</para>
  40. <programlisting><![CDATA[
  41. public aspect Foo {}
  42. ]]></programlisting>
  43. <para>To specify an aspect an aspect instantiation model (the default is
  44. singleton), provide the perclause as the
  45. <literal>@Aspect</literal>
  46. value.
  47. For example:
  48. </para>
  49. <programlisting><![CDATA[
  50. @Aspect("perthis(execution(* abc..*(..)))")
  51. public class Foo {}
  52. ]]></programlisting>
  53. <para>is equivalent to...</para>
  54. <programlisting><![CDATA[
  55. public aspect Foo perthis(execution(* abc..*(..))) {}
  56. ]]></programlisting>
  57. <sect2 id="limitations" xreflabel="limitations">
  58. <title>Limitations</title>
  59. <para>Privileged aspects are not supported by the annotation style.</para>
  60. <!--
  61. <programlisting><![CDATA[
  62. @Aspect(isPrivileged=true)
  63. public class Foo {}
  64. ]]></programlisting>
  65. <para>is equivalent to...</para>
  66. <programlisting><![CDATA[
  67. public privileged aspect Foo {}
  68. ]]></programlisting>
  69. -->
  70. </sect2>
  71. </sect1>
  72. <sect1 id="ataspectj-pcadvice">
  73. <title>Pointcuts and Advice</title>
  74. <para>
  75. Pointcut and advice declarations can be made using the
  76. <literal>Pointcut, Before, After, AfterReturning, AfterThrowing,</literal>
  77. and
  78. <literal>Around</literal>
  79. annotations.
  80. </para>
  81. <sect2 id="pointcuts" xreflabel="pointcuts">
  82. <title>Pointcuts</title>
  83. <para>
  84. Pointcuts are specified using the
  85. <literal>org.aspectj.lang.annotation.Pointcut</literal>
  86. annotation
  87. on a method declaration. The method should have a
  88. <literal>void</literal>
  89. return type. The parameters of the method correspond to the parameters
  90. of the pointcut. The modifiers of the method correspond to the modifiers
  91. of the pointcut.
  92. </para>
  93. <para>
  94. As a general rule, the
  95. <literal>@Pointcut</literal>
  96. annotated method must have an empty method body
  97. and must not have any
  98. <literal>throws</literal>
  99. clause. If formal are bound (using
  100. <literal>args(), target(), this(), @args(), @target(), @this(), @annotation())</literal>
  101. in the
  102. pointcut, then they must appear in the method signature.
  103. </para>
  104. <para>
  105. The
  106. <literal>if()</literal>
  107. pointcut is treated specially and is discussed in a later section.
  108. </para>
  109. <para>Here is a simple example of a pointcut declaration in both code and @AspectJ styles:</para>
  110. <programlisting><![CDATA[
  111. @Pointcut("call(* *.*(..))")
  112. void anyCall() {}
  113. ]]></programlisting>
  114. <para>is equivalent to...</para>
  115. <programlisting><![CDATA[
  116. pointcut anyCall() : call(* *.*(..));
  117. ]]></programlisting>
  118. <para>When binding arguments, simply declare the arguments as normal in the annotated method:</para>
  119. <programlisting><![CDATA[
  120. @Pointcut("call(* *.*(int)) && args(i) && target(callee)")
  121. void someCall(int i, Foo callee) {}
  122. ]]></programlisting>
  123. <para>is equivalent to...</para>
  124. <programlisting><![CDATA[
  125. pointcut anyCall(int i, Foo callee) : call(* *.*(int)) && args(i) && target(callee);
  126. ]]></programlisting>
  127. <para>An example with modifiers (Remember that Java 5 annotations are not
  128. inherited, so the <literal>@Pointcut</literal> annotation must be
  129. present on the extending aspect's pointcut declaration too):</para>
  130. <programlisting><![CDATA[
  131. @Pointcut("")
  132. protected abstract void anyCall();
  133. ]]></programlisting>
  134. <para>is equivalent to...</para>
  135. <programlisting><![CDATA[
  136. protected abstract pointcut anyCall();
  137. ]]></programlisting>
  138. <sect3>
  139. <title>Type references inside @AspectJ annotations</title>
  140. <para>
  141. Using the code style, types referenced in pointcut expressions are
  142. resolved with respect to the imported types in the compilation unit.
  143. When using the annotation style, types referenced in pointcut
  144. expressions are resolved in the absence of any imports and so have
  145. to be fully qualified if they are not by default visible to the
  146. declaring type (outside of the declaring package and
  147. <literal>java.lang</literal>
  148. ). This
  149. does not apply to type patterns with wildcards, which are always resolved
  150. in a global scope.
  151. </para>
  152. <para>
  153. Consider the following compilation unit:
  154. </para>
  155. <programlisting><![CDATA[
  156. package org.aspectprogrammer.examples;
  157. import java.util.List;
  158. public aspect Foo {
  159. pointcut listOperation() : call(* List.*(..));
  160. pointcut anyUtilityCall() : call(* java.util..*(..));
  161. }
  162. ]]></programlisting>
  163. <para>
  164. Using the annotation style this would be written as:
  165. </para>
  166. <programlisting><![CDATA[
  167. package org.aspectprogrammer.examples;
  168. import java.util.List; // redundant but harmless
  169. @Aspect
  170. public class Foo {
  171. @Pointcut("call(* java.util.List.*(..))") // must qualify
  172. void listOperation() {}
  173. @Pointcut("call(* java.util..*(..))")
  174. void anyUtilityCall() {}
  175. }
  176. ]]></programlisting>
  177. </sect3>
  178. <sect3>
  179. <title>if() pointcut expressions</title>
  180. <para>In code style, it is possible to use the
  181. <literal>if(...)</literal>
  182. poincut to define
  183. a conditional pointcut expression which will be evaluated at runtime for each candidate join point.
  184. The
  185. <literal>if(...)</literal>
  186. body can be any valid Java boolean expression, and can use any exposed formal, as well as the join
  187. point forms
  188. <literal>thisJoinPoint, thisJoinPointStaticPart and thisJoinPointEnclosingStaticPart</literal>
  189. .
  190. </para>
  191. <para>
  192. When using the annotation style, it is not possible to write a full Java expression
  193. within
  194. the annotation value so the syntax differs slightly, whilst providing the very same
  195. semantics and runtime behaviour. An
  196. <literal>if()</literal>
  197. pointcut expression can be
  198. declared in an
  199. <literal>@Pointcut</literal>
  200. , but must have either an empty body (<literal>if()</literal>, or be one
  201. of the expression forms
  202. <literal>if(true)</literal>
  203. or
  204. <literal>if(false)</literal>
  205. . The annotated
  206. method must be public, static, and return a boolean. The body of the method contains the
  207. condition to be evaluated. For example:
  208. </para>
  209. <programlisting><![CDATA[
  210. @Pointcut("call(* *.*(int)) && args(i) && if()")
  211. public static boolean someCallWithIfTest(int i) {
  212. return i > 0;
  213. }
  214. ]]></programlisting>
  215. <para>is equivalent to...</para>
  216. <programlisting><![CDATA[
  217. pointcut someCallWithIfTest(int i) : call(* *.*(int)) && args(i) && if(i > 0);
  218. ]]></programlisting>
  219. <para>and the following is also a valid form:</para>
  220. <programlisting><![CDATA[
  221. static int COUNT = 0;
  222. @Pointcut("call(* *.*(int)) && args(i) && if()")
  223. public static boolean someCallWithIfTest(int i, JoinPoint jp, JoinPoint.EnclosingStaticPart esjp) {
  224. // any legal Java expression...
  225. return i > 0
  226. && jp.getSignature().getName.startsWith("doo")
  227. && esjp.getSignature().getName().startsWith("test")
  228. && COUNT++ < 10;
  229. }
  230. @Before("someCallWithIfTest(anInt, jp, enc)")
  231. public void beforeAdviceWithRuntimeTest(int anInt, JoinPoint jp, JoinPoint.EnclosingStaticPart enc) {
  232. //...
  233. }
  234. // Note that the following is NOT valid
  235. /*
  236. @Before("call(* *.*(int)) && args(i) && if()")
  237. public void advice(int i) {
  238. // so you were writing an advice or an if body ?
  239. }
  240. */
  241. ]]></programlisting>
  242. <para>
  243. It is thus possible with the annotation style to use the
  244. <literal>if()</literal>
  245. pointcut
  246. only within an
  247. <literal>@Pointcut</literal>
  248. expression. The
  249. <literal>if()</literal>
  250. must not contain any
  251. body. The annotated
  252. <literal>@Pointcut</literal>
  253. method must then be of the form
  254. <literal>public static boolean</literal>
  255. and can use formal bindings as usual.
  256. Extra
  257. <emphasis>implicit</emphasis>
  258. arguments of type JoinPoint, JoinPoint.StaticPart and JoinPoint.EnclosingStaticPart can also be used
  259. (this is not permitted for regular annotated pointcuts not using the
  260. <literal>if()</literal>
  261. form).
  262. </para>
  263. <para>
  264. The special forms
  265. <literal>if(true)</literal>
  266. and
  267. <literal>if(false)</literal>
  268. can be used in a more
  269. general way and don't imply that the pointcut method must have a body.
  270. You can thus write
  271. <literal>@Before("somePoincut() &amp;&amp; if(false)")</literal>
  272. .
  273. </para>
  274. </sect3>
  275. </sect2>
  276. <sect2 id="advice" xreflabel="advice">
  277. <title>Advice</title>
  278. <para>In this section we first discuss the use of annotations for
  279. simple advice declarations. Then we show how
  280. <literal>thisJoinPoint</literal>
  281. and its siblings are handled in the body of advice and discuss the
  282. treatment of
  283. <literal>proceed</literal>
  284. in around advice.
  285. </para>
  286. <para>Using the annotation style, an advice declaration is written as
  287. a regular Java method with one of the
  288. <literal>Before, After, AfterReturning,
  289. AfterThrowing,</literal>
  290. or
  291. <literal>Around</literal>
  292. annotations. Except in
  293. the case of around advice, the method should return void. The method should
  294. be declared public.
  295. </para>
  296. <para>A method that has an advice annotation is treated exactly as an
  297. advice declaration by AspectJ's weaver. This includes the join points that
  298. arise when the advice is executed (an adviceexecution join point, not a
  299. method execution join point).</para>
  300. <para>The following example shows a simple before advice declaration in
  301. both styles:</para>
  302. <programlisting><![CDATA[
  303. @Before("call(* org.aspectprogrammer..*(..)) && this(Foo)")
  304. public void callFromFoo() {
  305. System.out.println("Call from Foo");
  306. }
  307. ]]></programlisting>
  308. <para>is equivalent to...</para>
  309. <programlisting><![CDATA[
  310. before() : call(* org.aspectprogrammer..*(..)) && this(Foo) {
  311. System.out.println("Call from Foo");
  312. }
  313. ]]></programlisting>
  314. <!--
  315. AMC: enhanced adviceexecution pointcuts and @AdviceName will most likely not make AJ5 1.5.0
  316. <para>Notice one slight difference between the two advice declarations: in
  317. the annotation style, the advice has a name, "callFromFoo". Even though
  318. advice cannot be invoked explicitly, this name is useful in join point
  319. matching when advising advice execution. For this reason, and to preserve
  320. exact semantic equivalence between the two styles, we also support the
  321. <literal>org.aspectj.lang.annotation.AdviceName</literal> annotation.
  322. The exact equivalent declarations are:
  323. </para>
  324. <programlisting><![CDATA[
  325. @AdviceName("callFromFoo")
  326. before() : call(* org.aspectprogrammer..*(..)) && this(Foo) {
  327. System.out.println("Call from Foo");
  328. }
  329. is equivalent to...
  330. @Before("call(* org.aspectprogrammer..*(..)) && this(Foo)")
  331. public void callFromFoo() {
  332. System.out.println("Call from Foo");
  333. }
  334. ]]></programlisting>
  335. -->
  336. <para>If the advice body needs to know which particular
  337. <literal>Foo</literal>
  338. instance
  339. is making the call, just add a parameter to the advice declaration.
  340. </para>
  341. <programlisting><![CDATA[
  342. before(Foo foo) : call(* org.aspectprogrammer..*(..)) && this(foo) {
  343. System.out.println("Call from Foo: " + foo);
  344. }
  345. ]]></programlisting>
  346. <para>can be written as:</para>
  347. <programlisting><![CDATA[
  348. @Before("call(* org.aspectprogrammer..*(..)) && this(foo)")
  349. public void callFromFoo(Foo foo) {
  350. System.out.println("Call from Foo: " + foo);
  351. }
  352. ]]></programlisting>
  353. <para>If the advice body needs access to
  354. <literal>thisJoinPoint</literal>
  355. ,
  356. <literal>thisJoinPointStaticPart</literal>
  357. ,
  358. <literal>thisEnclosingJoinPointStaticPart</literal>
  359. then these need to
  360. be declared as additional method parameters when using the annotation
  361. style.
  362. </para>
  363. <programlisting><![CDATA[
  364. @Before("call(* org.aspectprogrammer..*(..)) && this(foo)")
  365. public void callFromFoo(JoinPoint thisJoinPoint, Foo foo) {
  366. System.out.println("Call from Foo: " + foo + " at "
  367. + thisJoinPoint);
  368. }
  369. ]]></programlisting>
  370. <para>is equivalent to...</para>
  371. <programlisting><![CDATA[
  372. before(Foo foo) : call(* org.aspectprogrammer..*(..)) && this(foo) {
  373. System.out.println("Call from Foo: " + foo + " at "
  374. + thisJoinPoint);
  375. }
  376. ]]></programlisting>
  377. <para>Advice that needs all three variables would be declared:</para>
  378. <programlisting><![CDATA[
  379. @Before("call(* org.aspectprogrammer..*(..)) && this(Foo)")
  380. public void callFromFoo(JoinPoint thisJoinPoint,
  381. JoinPoint.StaticPart thisJoinPointStaticPart,
  382. JoinPoint.EnclosingStaticPart thisEnclosingJoinPointStaticPart) {
  383. // ...
  384. }
  385. ]]></programlisting>
  386. <para>
  387. <literal>JoinPoint.EnclosingStaticPart</literal>
  388. is a new (empty) sub-interface
  389. of
  390. <literal>JoinPoint.StaticPart</literal>
  391. which allows the AspectJ weaver to
  392. distinguish based on type which of
  393. <literal>thisJoinPointStaticPart</literal>
  394. and
  395. <literal>thisEnclosingJoinPointStaticPart</literal>
  396. should be passed in a given
  397. parameter position.
  398. </para>
  399. <para>
  400. <literal>After</literal>
  401. advice declarations take exactly the same form
  402. as
  403. <literal>Before</literal>
  404. , as do the forms of
  405. <literal>AfterReturning</literal>
  406. and
  407. <literal>AfterThrowing</literal>
  408. that do not expose the return type or
  409. thrown exception respectively.
  410. </para>
  411. <para>
  412. To expose a return value with after returning advice simply declare the returning
  413. parameter as a parameter in the method body and bind it with the "returning"
  414. attribute:
  415. </para>
  416. <programlisting><![CDATA[
  417. @AfterReturning("criticalOperation()")
  418. public void phew() {
  419. System.out.println("phew");
  420. }
  421. @AfterReturning(pointcut="call(Foo+.new(..))",returning="f")
  422. public void itsAFoo(Foo f) {
  423. System.out.println("It's a Foo: " + f);
  424. }
  425. ]]></programlisting>
  426. <para>is equivalent to...</para>
  427. <programlisting><![CDATA[
  428. after() returning : criticalOperation() {
  429. System.out.println("phew");
  430. }
  431. after() returning(Foo f) : call(Foo+.new(..)) {
  432. System.out.println("It's a Foo: " + f);
  433. }
  434. ]]></programlisting>
  435. <para>(Note the use of the "pointcut=" prefix in front of the pointcut
  436. expression in the returning case).</para>
  437. <para>After throwing advice works in a similar fashion, using the
  438. <literal>throwing</literal>
  439. attribute when needing to expose a
  440. thrown exception.
  441. </para>
  442. <para>For around advice, we have to tackle the problem of
  443. <literal>proceed</literal>
  444. .
  445. One of the design goals for the annotation style is that a large class of
  446. AspectJ applications should be compilable with a standard Java 5 compiler.
  447. A straight call to
  448. <literal>proceed</literal>
  449. inside a method body:
  450. </para>
  451. <programlisting><![CDATA[
  452. @Around("call(* org.aspectprogrammer..*(..))")
  453. public Object doNothing() {
  454. return proceed(); // CE on this line
  455. }
  456. ]]></programlisting>
  457. <para>will result in a "No such method" compilation error. For this
  458. reason AspectJ 5 defines a new sub-interface of
  459. <literal>JoinPoint</literal>
  460. ,
  461. <literal>ProceedingJoinPoint</literal>
  462. .
  463. </para>
  464. <programlisting><![CDATA[
  465. public interface ProceedingJoinPoint extends JoinPoint {
  466. public Object proceed(Object[] args);
  467. }
  468. ]]></programlisting>
  469. <para>The around advice given above can now be written as:</para>
  470. <programlisting><![CDATA[
  471. @Around("call(* org.aspectprogrammer..*(..))")
  472. public Object doNothing(ProceedingJoinPoint thisJoinPoint) {
  473. return thisJoinPoint.proceed();
  474. }
  475. ]]></programlisting>
  476. <para>Here's an example that uses parameters for the proceed call:</para>
  477. <programlisting><![CDATA[
  478. @Aspect
  479. public class ProceedAspect {
  480. @Pointcut("call(* setAge(..)) && args(i)")
  481. void setAge(int i) {}
  482. @Around("setAge(i)")
  483. public Object twiceAsOld(ProceedingJoinPoint thisJoinPoint, int i) {
  484. return thisJoinPoint.proceed(new Object[]{i*2}); //using Java 5 autoboxing
  485. }
  486. }
  487. ]]></programlisting>
  488. <para>is equivalent to:</para>
  489. <programlisting><![CDATA[
  490. public aspect ProceedAspect {
  491. pointcut setAge(int i): call(* setAge(..)) && args(i);
  492. Object around(int i): setAge(i) {
  493. return proceed(i*2);
  494. }
  495. }
  496. ]]></programlisting>
  497. <para>Note that the ProceedingJoinPoint does not need to be passed to the proceed(..) arguments.
  498. </para>
  499. <para>In code style, the proceed method has the same signature as the advice, any reordering of
  500. actual arguments to the joinpoint that is done in the advice signature must be respected. Annotation
  501. style is different. The proceed(..) call takes, in this order:
  502. <itemizedlist>
  503. <listitem>If 'this()' was used in the pointcut <emphasis>for binding</emphasis>, it must be passed first in proceed(..).
  504. </listitem>
  505. <listitem>If 'target()' was used in the pointcut <emphasis>for binding</emphasis>, it must be passed next in proceed(..) - it will be the
  506. first argument to proceed(..) if this() was not used for binding.
  507. </listitem>
  508. <listitem>Finally come <emphasis>all</emphasis> the arguments expected at the join point, in the order they
  509. are supplied at the join point. Effectively the advice signature is ignored - it doesn't
  510. matter if a subset of arguments were bound or the ordering was changed in the advice
  511. signature, the proceed(..) calls takes all of them in the right order for the join point.
  512. </listitem>
  513. </itemizedlist>
  514. </para>
  515. <para>Since proceed(..) in this case takes an Object array, AspectJ cannot do as much compile time
  516. checking as it can for code style. If the rules above aren't obeyed then it will unfortunately
  517. manifest as a runtime error.
  518. </para>
  519. </sect2>
  520. </sect1>
  521. <sect1 id="ataspectj-itds">
  522. <title>Inter-type Declarations</title>
  523. <para>
  524. Inter-type declarations are challenging to support using an annotation style.
  525. It's very important to preserve the same semantics between the code style
  526. and the annotation style. We also want to support compilation of a large set
  527. of @AspectJ applications using a standard Java 5 compiler. For these reasons,
  528. the 1.5.0 release of AspectJ 5 only supports inter-type declarations
  529. backed by interfaces when using the annotation style -
  530. which means it is not possible to
  531. introduce constructors or fields, as it would not be not possible to call
  532. those unless already woven and available on a binary form.
  533. </para>
  534. <para>
  535. Consider the following aspect:
  536. </para>
  537. <programlisting><![CDATA[
  538. public aspect MoodIndicator {
  539. public interface Moody {};
  540. private Mood Moody.mood = Mood.HAPPY;
  541. public Mood Moody.getMood() {
  542. return mood;
  543. }
  544. declare parents : org.xyz..* implements Moody;
  545. before(Moody m) : execution(* *.*(..)) && this(m) {
  546. System.out.println("I'm feeling " + m.getMood());
  547. }
  548. }
  549. ]]></programlisting>
  550. <para>
  551. This declares an interface
  552. <literal>Moody</literal>
  553. , and then makes two
  554. inter-type declarations on the interface - a field that is private to the
  555. aspect, and a method that returns the mood. Within the body of the inter-type
  556. declared method
  557. <literal>getMoody</literal>
  558. , the type of
  559. <literal>this</literal>
  560. is
  561. <literal>Moody</literal>
  562. (the target type of the inter-type declaration).
  563. </para>
  564. <para>Using the annotation style this aspect can be written:
  565. </para>
  566. <programlisting><![CDATA[
  567. @Aspect
  568. public class MoodIndicator {
  569. // this interface can be outside of the aspect
  570. public interface Moody {
  571. Mood getMood();
  572. };
  573. // this implementation can be outside of the aspect
  574. public static class MoodyImpl implements Moody {
  575. private Mood mood = Mood.HAPPY;
  576. public Mood getMood() {
  577. return mood;
  578. }
  579. }
  580. // the field type must be the introduced interface. It can't be a class.
  581. @DeclareParents(value="org.xzy..*",defaultImpl=MoodyImpl.class)
  582. private Moody implementedInterface;
  583. @Before("execution(* *.*(..)) && this(m)")
  584. void feelingMoody(Moody m) {
  585. System.out.println("I'm feeling " + m.getMood());
  586. }
  587. }
  588. ]]></programlisting>
  589. <para>
  590. This is very similar to the mixin mechanism supported by AspectWerkz. The
  591. effect of the
  592. <literal>@DeclareParents</literal>
  593. annotation is equivalent to
  594. a declare parents statement that all types matching the type pattern implement
  595. the given interface (in this case Moody).
  596. Each method declared in the interface is treated as an inter-type declaration.
  597. Note how this scheme operates within the constraints
  598. of Java type checking and ensures that
  599. <literal>this</literal>
  600. has access
  601. to the exact same set of members as in the code style example.
  602. </para>
  603. <para>
  604. Note that it is illegal to use the @DeclareParents annotation on an aspect' field of a non-interface type.
  605. The interface type is the inter-type declaration contract that dictates
  606. which methods are declared on the target type.
  607. </para>
  608. <programlisting><![CDATA[
  609. // this type will be affected by the inter-type declaration as the type pattern matches
  610. package org.xyz;
  611. public class MoodTest {
  612. public void test() {
  613. // see here the cast to the introduced interface (required)
  614. Mood mood = ((Moody)this).getMood();
  615. ...
  616. }
  617. }
  618. ]]></programlisting>
  619. <para>The <literal>@DeclareParents</literal> annotation can also be used without specifying
  620. a <literal>defaultImpl</literal> value (for example,
  621. <literal>@DeclareParents("org.xyz..*")</literal>). This is equivalent to a
  622. <literal>declare parents ... implements</literal> clause, and does <emphasis>not</emphasis>
  623. make any inter-type declarations for default implementation of the interface methods.
  624. </para>
  625. <para>
  626. Consider the following aspect:
  627. </para>
  628. <programlisting><![CDATA[
  629. public aspect SerializableMarker {
  630. declare parents : org.xyz..* implements Serializable;
  631. }
  632. ]]></programlisting>
  633. <para>Using the annotation style this aspect can be written:
  634. </para>
  635. <programlisting><![CDATA[
  636. @Aspect
  637. public class SerializableMarker {
  638. @DeclareParents("org.xyz..*")
  639. Serializable implementedInterface;
  640. }
  641. ]]></programlisting>
  642. <para>
  643. If the interface defines one or more operations, and these are not implemented by
  644. the target type, an error will be issued during weaving.
  645. </para>
  646. </sect1>
  647. <sect1 id="ataspectj-declare">
  648. <title>Declare statements</title>
  649. <para>The previous section on inter-type declarations covered the case
  650. of declare parents ... implements. The 1.5.0 release of AspectJ 5 does
  651. not support annotation style declarations for declare parents ... extends
  652. and declare soft (programs with these declarations would not in general
  653. be compilable by a regular Java 5 compiler, reducing the priority of
  654. their implementation). These may be supported in a future release.</para>
  655. <para>
  656. Declare annotation is also not supported in the 1.5.0 release of AspectJ 5.
  657. </para>
  658. <para>Declare precedence <emphasis>is</emphasis>
  659. supported. For declare precedence, use the
  660. <literal>@DeclarePrecedence</literal>
  661. annotation as in the following example:
  662. </para>
  663. <programlisting><![CDATA[
  664. public aspect SystemArchitecture {
  665. declare precedence : Security*, TransactionSupport, Persistence;
  666. // ...
  667. }
  668. can be written as:
  669. @Aspect
  670. @DeclarePrecedence("Security*,org.xyz.TransactionSupport,org.xyz.Persistence")
  671. public class SystemArchitecture {
  672. // ...
  673. }
  674. ]]></programlisting>
  675. <!--
  676. note: below is not supported for now.
  677. <para>
  678. Declare annotation is supported via annotations on a dummy type member. If the
  679. <literal>Target</literal>
  680. specification of the annotation allows it, use a field,
  681. otherwise declare a member of the type required by the
  682. <literal>Target</literal>
  683. .
  684. For example:
  685. </para>
  686. <programlisting><![CDATA[
  687. public aspect DeclareAnnotationExamples {
  688. declare annotation : org.xyz.model..* : @BusinessDomain;
  689. declare annotation : public * BankAccount+.*(..) : @Secured(role="supervisor");
  690. declare anotation : * DAO+.* : @Persisted;
  691. }
  692. can be written as...
  693. @Aspect
  694. public class DeclareAnnotationExamples {
  695. @DeclareAnnotation("org.xyz.model..*)
  696. @BusinessDomain Object modelClass;
  697. // this example assumes that the @Secured annotation has a Target
  698. // annotation with value ElementType.METHOD
  699. @DeclareAnnotation("public * org.xyz.banking.BankAccount+.*(..)")
  700. @Secured(role="supervisor) void bankAccountMethod();
  701. @DeclareAnnotation("* DAO+.*")
  702. @Persisted Object daoFields;
  703. }
  704. ]]></programlisting>
  705. <para>
  706. <emphasis>Note: Declare annotation is not available in AspectJ 1.5 M3 and syntax may change
  707. when the design and implementation is complete.</emphasis>
  708. </para>
  709. -->
  710. <para>We also support annotation style declarations for declare warning and
  711. declare error - any corresponding warnings and errors will be emitted at
  712. weave time, not when the aspects containing the declarations are compiled.
  713. (This is the same behaviour as when using declare warning or error with the
  714. code style). Declare warning and error declarations are made by annotating
  715. a string constant whose value is the message to be issued.</para>
  716. <para>Note that the String must be a literal and not the result of the invocation
  717. of a static method for example.</para>
  718. <programlisting><![CDATA[
  719. declare warning : call(* javax.sql..*(..)) && !within(org.xyz.daos..*)
  720. : "Only DAOs should be calling JDBC.";
  721. declare error : execution(* IFoo+.*(..)) && !within(org.foo..*)
  722. : "Only foo types can implement IFoo";
  723. can be written as...
  724. @DeclareWarning("call(* javax.sql..*(..)) && !within(org.xyz.daos..*)")
  725. static final String aMessage = "Only DAOs should be calling JDBC.";
  726. @DeclareError("execution(* IFoo+.*(..)) && !within(org.foo..*)")
  727. static final String badIFooImplementors = "Only foo types can implement IFoo";
  728. // the following is not valid since the message is not a String literal
  729. @DeclareError("execution(* IFoo+.*(..)) && !within(org.foo..*)")
  730. static final String badIFooImplementorsCorrupted = getMessage();
  731. static String getMessage() {
  732. return "Only foo types can implement IFoo " + System.currentTimeMillis();
  733. }
  734. ]]></programlisting>
  735. </sect1>
  736. <sect1 id="ataspectj-aspectof">
  737. <title>aspectOf() and hasAspect() methods</title>
  738. <para>A central part of AspectJ's programming model is that aspects
  739. written using the code style and compiled using ajc support
  740. <literal>aspectOf</literal>
  741. and
  742. <literal>hasAspect</literal>
  743. static
  744. methods. When developing an aspect using the annotation style and compiling
  745. using a regular Java 5 compiler, these methods will not be visible to the
  746. compiler and will result in a compilation error if another part of the
  747. program tries to call them.
  748. </para>
  749. <para>To provide equivalent support for AspectJ applications compiled with
  750. a standard Java 5 compiler, AspectJ 5 defines the
  751. <literal>Aspects</literal>
  752. utility class:
  753. </para>
  754. <programlisting><![CDATA[
  755. public class Aspects {
  756. /* variation used for singleton, percflow, percflowbelow */
  757. static<T> public static T aspectOf(T aspectType) {...}
  758. /* variation used for perthis, pertarget */
  759. static<T> public static T aspectOf(T aspectType, Object forObject) {...}
  760. /* variation used for pertypewithin */
  761. static<T> public static T aspectOf(T aspectType, Class forType) {...}
  762. /* variation used for singleton, percflow, percflowbelow */
  763. public static boolean hasAspect(Object anAspect) {...}
  764. /* variation used for perthis, pertarget */
  765. public static boolean hasAspect(Object anAspect, Object forObject) {...}
  766. /* variation used for pertypewithin */
  767. public static boolean hasAspect(Object anAspect, Class forType) {...}
  768. }
  769. ]]></programlisting>
  770. <!-- TODO AV - stuff below is not done -->
  771. <!--
  772. <para>When the AspectJ weaver sees calls to these methods, it will convert
  773. them into the most efficient form possible (to get performance equivalent
  774. to a direct <literal>MyAspect.aspectOf()</literal> call).</para>
  775. -->
  776. </sect1>
  777. </chapter>