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

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