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.

annotations.xml 50KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  1. <chapter id="annotations" xreflabel="Annotations">
  2. <title>Annotations</title>
  3. <sect1 id="annotations-inJava5">
  4. <title>Annotations in Java 5</title>
  5. <para>
  6. This section provides the essential information about annotations in
  7. Java 5 needed to understand how annotations are treated in AspectJ 5.
  8. For a full introduction to annotations in Java, please see the
  9. documentation for the Java 5 SDK.
  10. </para>
  11. <sect2>
  12. <title>Using Annotations</title>
  13. <para>
  14. Java 5 introduces <emphasis>annotation types</emphasis> which can
  15. be used to express metadata relating to program members in the
  16. form of <emphasis>annotations</emphasis>. Annotations in Java 5
  17. can be applied to package and type declarations (classes,
  18. interfaces, enums, and annotations), constructors, methods,
  19. fields, parameters, and variables. Annotations are specified in the
  20. program source by using the <literal>@</literal> symbol. For example,
  21. the following piece of code uses the <literal>@Deprecated</literal>
  22. annotation to indicate that the <literal>obsoleteMethod()</literal>
  23. has been deprecated:
  24. </para>
  25. <programlisting><![CDATA[
  26. @Deprecated
  27. public void obsoleteMethod() { ... }
  28. ]]></programlisting>
  29. <para>
  30. Annotations may be <emphasis>marker annotations</emphasis>,
  31. <emphasis>single-valued annotations</emphasis>, or
  32. <emphasis>multi-valued annotations</emphasis>.
  33. Annotation types with no members or that provide default values
  34. for all members may be used simply as marker annotations, as in
  35. the deprecation example above. Single-value annotation types have
  36. a single member, and the annotation may be written in one of
  37. two equivalent forms:
  38. </para>
  39. <programlisting><![CDATA[
  40. @SuppressWarnings({"unchecked"})
  41. public void someMethod() {...}
  42. ]]></programlisting>
  43. <para>
  44. or
  45. </para>
  46. <programlisting><![CDATA[
  47. @SuppressWarnings(value={"unchecked"})
  48. public void someMethod() {...}
  49. ]]></programlisting>
  50. <para>
  51. Multi-value annotations must use the <literal>member-name=value
  52. </literal> syntax to specify annotation values. For example:
  53. </para>
  54. <programlisting><![CDATA[
  55. @Authenticated(role="supervisor",clearanceLevel=5)
  56. public void someMethod() {...}
  57. ]]></programlisting>
  58. </sect2>
  59. <sect2>
  60. <title>Retention Policies</title>
  61. <para>
  62. Annotations can have one of three retention policies:
  63. </para>
  64. <variablelist>
  65. <varlistentry>
  66. <term>Source-file retention</term>
  67. <listitem>
  68. <para>
  69. Annotations with source-file retention are read by the
  70. compiler during the compilation process, but are not
  71. rendered in the generated <literal>.class</literal> files.
  72. </para>
  73. </listitem>
  74. </varlistentry>
  75. <varlistentry>
  76. <term>Class-file retention</term>
  77. <listitem>
  78. <para>
  79. This is the default retention policy. Annotations
  80. with class-file retention are read by the compiler
  81. and also retained in the generated <literal>
  82. .class</literal> files.
  83. </para>
  84. </listitem>
  85. </varlistentry>
  86. <varlistentry>
  87. <term>Runtime retention</term>
  88. <listitem>
  89. <para>
  90. Annotations with runtime retention are read by the
  91. compiler, retained in the generated <literal>
  92. .class</literal> files, and also made available
  93. at runtime.
  94. </para>
  95. </listitem>
  96. </varlistentry>
  97. </variablelist>
  98. <para>Local variable annotations are not retained in class files (or at runtime)
  99. regardless of the retention policy set on the annotation type. See JLS 9.6.1.2.</para>
  100. </sect2>
  101. <sect2>
  102. <title>Accessing Annotations at Runtime</title>
  103. <para>
  104. Java 5 supports a new interface,
  105. <literal>java.lang.reflect.AnnotatedElement</literal>, that is
  106. implemented by the reflection classes in Java (<literal>Class</literal>,
  107. <literal>Constructor</literal>,
  108. <literal>Field</literal>, <literal>Method</literal>, and
  109. <literal>Package</literal>). This interface gives you access
  110. to annotations <emphasis>that have runtime retention</emphasis> via
  111. the <literal>getAnnotation</literal>, <literal>getAnnotations</literal>,
  112. and <literal>isAnnotationPresent</literal>. Because annotation types are
  113. just regular Java classes, the annotations returned by these methods
  114. can be queried just like any regular Java object.
  115. </para>
  116. </sect2>
  117. <sect2>
  118. <title>Annotation Inheritance</title>
  119. <para>
  120. It is important to understand the rules relating to inheritance of
  121. annotations, as these have a bearing on join point matching
  122. based on the presence or absence of annotations.
  123. </para>
  124. <para>
  125. By default annotations are <emphasis>not</emphasis> inherited. Given
  126. the following program
  127. </para>
  128. <programlisting><![CDATA[
  129. @MyAnnotation
  130. class Super {
  131. @Oneway public void foo() {}
  132. }
  133. class Sub extends Super {
  134. public void foo() {}
  135. }
  136. ]]></programlisting>
  137. <para>
  138. Then <literal>Sub</literal> <emphasis>does not</emphasis> have
  139. the <literal>MyAnnotation</literal> annotation, and
  140. <literal>Sub.foo()</literal> is not an <literal>@Oneway</literal>
  141. method, despite the fact that it overrides
  142. <literal>Super.foo()</literal> which is.
  143. </para>
  144. <para>
  145. If an annotation type has the meta-annotation <literal>@Inherited</literal>
  146. then an annotation of that type on a <emphasis>class</emphasis> will cause
  147. the annotation to be inherited by sub-classes. So, in the example
  148. above, if the <literal>MyAnnotation</literal> type had the
  149. <literal>@Inherited</literal> attribute, then <literal>Sub</literal>
  150. would have the <literal>MyAnnotation</literal> annotation.
  151. </para>
  152. <para>
  153. <literal>@Inherited</literal> annotations are not inherited when used to
  154. annotate anything other than a type. A type
  155. that implements one or more interfaces never inherits any annotations from
  156. the interfaces it implements.
  157. </para>
  158. </sect2>
  159. </sect1>
  160. <!-- ============================== -->
  161. <sect1 id="annotations-aspectmembers">
  162. <title>Annotating Aspects</title>
  163. <para>
  164. AspectJ 5 supports annotations on aspects, and on method, field,
  165. constructor, advice, and inter-type declarations within aspects.
  166. Method and advice parameters may also be annotated.
  167. Annotations are not permitted on pointcut declarations or on
  168. <literal>declare</literal> statements.
  169. </para>
  170. <para>
  171. The following example illustrates the use of annotations in aspects:
  172. </para>
  173. <programlisting><![CDATA[
  174. @AspectAnnotation
  175. public abstract aspect ObserverProtocol {
  176. @InterfaceAnnotation
  177. interface Observer {}
  178. @InterfaceAnnotation
  179. interface Subject {}
  180. @ITDFieldAnnotation
  181. private List<Observer> Subject.observers;
  182. @ITDMethodAnnotation
  183. public void Subject.addObserver(Observer o) {
  184. observers.add(o);
  185. }
  186. @ITDMethodAnnotation
  187. public void Subject.removeObserver(Observer o) {
  188. observers.remove(o);
  189. }
  190. @MethodAnnotation
  191. private void notifyObservers(Subject subject) {
  192. for(Observer o : subject.observers)
  193. notifyObserver(o,subject);
  194. }
  195. /**
  196. * Delegate to concrete sub-aspect the actual form of
  197. * notification for a given type of Observer.
  198. */
  199. @MethodAnnotation
  200. protected abstract void notifyObserver(Observer o, Subject s);
  201. /* no annotations on pointcuts */
  202. protected abstract pointcut observedEvent(Subject subject);
  203. @AdviceAnnotation
  204. after(Subject subject) returning : observedEvent(subject) {
  205. notifyObservers(subject);
  206. }
  207. }
  208. ]]></programlisting>
  209. <para>
  210. An annotation on an aspect will be inherited by sub-aspects, iff it has
  211. the <literal>@Inherited</literal> meta-annotation.
  212. </para>
  213. <para>
  214. AspectJ 5 supports a new XLint warning, "the pointcut associated with this
  215. advice does not match any join points". The warning is enabled by default and
  216. will be emitted by the compiler if the pointcut expression associated with an
  217. advice statement can be statically determined to not match any join points. The
  218. warning can be suppressed for an individual advice statement by using the
  219. <literal>@SuppressAjWarnings({"adviceDidNotMatch"})</literal> annotation. This works in
  220. the same way as the Java 5 SuppressWarnings annotation (See JLS 9.6.1.5), but has class file
  221. retention.
  222. </para>
  223. <programlisting><![CDATA[
  224. import org.aspectj.lang.annotation.SuppressAjWarnings;
  225. public aspect AnAspect {
  226. pointcut anInterfaceOperation() : execution(* AnInterface.*(..));
  227. @SuppressAjWarnings // may not match if there are no implementers of the interface...
  228. before() : anInterfaceOperation() {
  229. // do something...
  230. }
  231. @SuppressAjWarnings("adviceDidNotMatch") // alternate form
  232. after() returning : anInterfaceOperation() {
  233. // do something...
  234. }
  235. }
  236. ]]></programlisting>
  237. </sect1>
  238. <!-- ============================== -->
  239. <sect1 id="annotations-pointcuts-and-advice">
  240. <title>Join Point Matching based on Annotations</title>
  241. <para>
  242. This section discusses changes to type pattern and signature pattern matching in
  243. AspectJ 5 that support matching join points based on the presence or absence of
  244. annotations. We then discuss means of exposing annotation values within the body
  245. of advice.
  246. </para>
  247. <sect2>
  248. <title>Annotation Patterns</title>
  249. <para>
  250. For any kind of annotated element (type, method, constructor, package, etc.),
  251. an annotation pattern can be used to match against the set of annotations
  252. on the annotated element. Annotation patterns are defined by the following
  253. grammar.
  254. </para>
  255. <programlisting><![CDATA[
  256. AnnotationPattern := '!'? '@' AnnotationTypePattern AnnotationPattern*
  257. AnnotationTypePattern := FullyQualifiedName |
  258. '(' TypePattern ')'
  259. FullyQualifiedName := JavaIdentifierCharacter+ ('.' JavaIdentifierCharacter+)*
  260. ]]></programlisting>
  261. <para>In simple terms, an annotation pattern element has one of two basic
  262. forms:</para>
  263. <itemizedlist>
  264. <listitem>@&lt;qualified-name&gt;, for example, @Foo, or
  265. @org.xyz.Foo.</listitem>
  266. <listitem>@(&lt;type-pattern&gt;), for example, @(org.xyz..*), or
  267. @(Foo || Boo)</listitem>
  268. </itemizedlist>
  269. <para>These simple elements may be negated using <literal>!</literal>, and
  270. combined by simple concatentation. The pattern <literal>@Foo @Boo</literal>
  271. matches an annotated element that has both an annotation of type <literal>Foo</literal>
  272. and an annotation of type <literal>Boo</literal>.</para>
  273. <para>Some examples of annotation patterns follow:</para>
  274. <variablelist>
  275. <varlistentry>
  276. <term>@Immutable</term>
  277. <listitem>
  278. <para>
  279. Matches any annotated element which has an annotation of
  280. type <literal>Immutable</literal>.
  281. </para>
  282. </listitem>
  283. </varlistentry>
  284. <varlistentry>
  285. <term>!@Persistent</term>
  286. <listitem>
  287. <para>
  288. Matches any annotated element which does not have an annotation of
  289. type <literal>Persistent</literal>.
  290. </para>
  291. </listitem>
  292. </varlistentry>
  293. <varlistentry>
  294. <term>@Foo @Goo</term>
  295. <listitem>
  296. <para>
  297. Matches any annotated element which has both an annotation of type <literal>Foo</literal> and
  298. an annotation of type <literal>Goo</literal>.
  299. </para>
  300. </listitem>
  301. </varlistentry>
  302. <varlistentry>
  303. <term>@(Foo || Goo)</term>
  304. <listitem>
  305. <para>
  306. Matches any annotated element which has either an annotation of a type matching
  307. the type pattern <literal>(Foo || Goo)</literal>.
  308. In other words, an annotated element with either an
  309. annotation of type <literal>Foo</literal> or
  310. an annotation of type <literal>Goo</literal> (or both). (The parenthesis are required in this example).
  311. </para>
  312. </listitem>
  313. </varlistentry>
  314. <varlistentry>
  315. <term>@(org.xyz..*)</term>
  316. <listitem>
  317. <para>
  318. Matches any annotated element which has either an annotation of a type matching
  319. the type pattern <literal>(org.xyz..*)</literal>.
  320. In other words, an annotated element with an annotation that is declared in the
  321. org.xyz package or a sub-package. (The parenthesis are required in this example).
  322. </para>
  323. </listitem>
  324. </varlistentry>
  325. </variablelist>
  326. </sect2>
  327. <sect2>
  328. <title>Type Patterns</title>
  329. <para>AspectJ 1.5 extends type patterns to allow an optional <literal>AnnotationPattern</literal>
  330. prefix. (Extensions to this definition for generics are shown in the next chapter).</para>
  331. <programlisting><![CDATA[
  332. TypePattern := SimpleTypePattern |
  333. '!' TypePattern |
  334. '(' AnnotationPattern? TypePattern ')'
  335. TypePattern '&&' TypePattern |
  336. TypePattern '||' TypePattern |
  337. SimpleTypePattern := DottedNamePattern '+'? '[]'*
  338. DottedNamePattern := FullyQualifiedName RestOfNamePattern? |
  339. '*' NotStarNamePattern?
  340. RestOfNamePattern := '..' DottedNamePattern |
  341. '*' NotStarNamePattern?
  342. NotStarNamePattern := FullyQualifiedName RestOfNamePattern? |
  343. '..' DottedNamePattern
  344. FullyQualifiedName := JavaIdentifierCharacter+ ('.' JavaIdentifierCharacter+)*
  345. ]]></programlisting>
  346. <para>Note that in most cases when annotations are used as part of a type pattern,
  347. the parenthesis are required (as in <literal>(@Foo Hello+)</literal>). In
  348. some cases (such as a type pattern used within a <literal>this</literal>
  349. pointcut expression, the parenthesis are optional:</para>
  350. <programlisting><![CDATA[
  351. OptionalParensTypePattern := AnnotationPattern? TypePattern
  352. ]]></programlisting>
  353. <para>
  354. The following examples illustrate the use of annotations in type
  355. patterns:
  356. </para>
  357. <variablelist>
  358. <varlistentry>
  359. <term>(@Immutable *)</term>
  360. <listitem>
  361. <para>
  362. Matches any type with an <literal>@Immutable</literal> annotation.
  363. </para>
  364. </listitem>
  365. </varlistentry>
  366. <varlistentry>
  367. <term>(!@Immutable *)</term>
  368. <listitem>
  369. <para>
  370. Matches any type which does not have an <literal>@Immutable</literal> annotation.
  371. </para>
  372. </listitem>
  373. </varlistentry>
  374. <varlistentry>
  375. <term> (@Immutable (org.xyz.* || org.abc.*))</term>
  376. <listitem>
  377. <para>
  378. Matches any type in the <literal>org.xyz</literal> or <literal>org.abc</literal>
  379. packages with the <literal>@Immutable</literal> annotation.
  380. </para>
  381. </listitem>
  382. </varlistentry>
  383. <varlistentry>
  384. <term>((@Immutable Foo+) || Goo)</term>
  385. <listitem>
  386. <para>
  387. Matches a type <literal>Foo</literal> or any of its subtypes, which have the <literal>@Immutable</literal>
  388. annotation, or a type <literal>Goo</literal>.
  389. </para>
  390. </listitem>
  391. </varlistentry>
  392. <varlistentry>
  393. <term>((@(Immutable || NonPersistent) org.xyz..*)</term>
  394. <listitem>
  395. <para>
  396. Matches any type in a package beginning with the prefix <literal>org.xyz</literal>,
  397. which has either the <literal>@Immutable</literal> annotation or the
  398. <literal>@NonPersistent</literal> annotation.
  399. </para>
  400. </listitem>
  401. </varlistentry>
  402. <varlistentry>
  403. <term>(@Immutable @NonPersistent org.xyz..*)</term>
  404. <listitem>
  405. <para>
  406. Matches any type in a package beginning with the prefix <literal>org.xyz</literal>,
  407. which has both an <literal>@Immutable</literal> annotation and an
  408. <literal>@NonPersistent</literal> annotation.
  409. </para>
  410. </listitem>
  411. </varlistentry>
  412. <varlistentry>
  413. <term> (@(@Inherited *) org.xyz..*)</term>
  414. <listitem>
  415. <para>
  416. Matches any type in a package beginning with the prefix <literal>org.xyz</literal>,
  417. which has an inheritable annotation. The annotation pattern
  418. <literal>@(@Inherited *)</literal> matches any annotation of a type matching the
  419. type pattern <literal>@Inherited *</literal>, which in turn matches any type with the
  420. <literal>@Inherited</literal> annotation.
  421. </para>
  422. </listitem>
  423. </varlistentry>
  424. </variablelist>
  425. </sect2>
  426. <sect2 id="signaturePatterns" xreflabel="Signature Patterns">
  427. <title>Signature Patterns</title>
  428. <para>A <literal>FieldPattern</literal> is described by the following
  429. grammar:</para>
  430. <programlisting><![CDATA[
  431. FieldPattern :=
  432. AnnotationPattern? FieldModifiersPattern?
  433. TypePattern (TypePattern DotOrDotDot)? SimpleNamePattern
  434. FieldModifiersPattern := '!'? FieldModifier FieldModifiersPattern*
  435. FieldModifier := 'public' | 'private' | 'protected' | 'static' |
  436. 'transient' | 'final'
  437. DotOrDotDot := '.' | '..'
  438. SimpleNamePattern := JavaIdentifierChar+ ('*' SimpleNamePattern)?
  439. ]]></programlisting>
  440. <para>
  441. The optional <literal>AnnotationPattern</literal> restricts matches to fields with
  442. annotations that match the pattern. For example:
  443. </para>
  444. <variablelist>
  445. <varlistentry>
  446. <term>@SensitiveData * *</term>
  447. <listitem>
  448. <para>
  449. Matches a field of any type and any name, that has an annotation of
  450. type <literal>@SensitiveData</literal>
  451. </para>
  452. </listitem>
  453. </varlistentry>
  454. <varlistentry>
  455. <term>@SensitiveData List org.xyz..*.*</term>
  456. <listitem>
  457. <para>
  458. Matches a member field of a type in a package with prefix <literal>org.xzy</literal>,
  459. where the field is of type <literal>List</literal>, and has an annotation of type
  460. <literal>@SensitiveData</literal>
  461. </para>
  462. </listitem>
  463. </varlistentry>
  464. <varlistentry>
  465. <term>(@SensitiveData *) org.xyz..*.*</term>
  466. <listitem>
  467. <para>
  468. Matches a member field of a type in a package with prefix <literal>org.xzy</literal>,
  469. where the field is of a type which has a <literal>@SensitiveData</literal> annotation.
  470. </para>
  471. </listitem>
  472. </varlistentry>
  473. <varlistentry>
  474. <term>@Foo (@Goo *) (@Hoo *).*</term>
  475. <listitem>
  476. <para>
  477. Matches a field with an annotation <literal>@Foo</literal>, of a type with an
  478. annotation <literal>@Goo</literal>, declared in a type with annotation
  479. <literal>@Hoo</literal>.
  480. </para>
  481. </listitem>
  482. </varlistentry>
  483. <varlistentry>
  484. <term>@Persisted @Classified * *</term>
  485. <listitem>
  486. <para>
  487. Matches a field with an annotation <literal>@Persisted</literal> and
  488. an annotation <literal>@Classified</literal>.
  489. </para>
  490. </listitem>
  491. </varlistentry>
  492. </variablelist>
  493. <para>A <literal>MethodPattern</literal> is of the form</para>
  494. <programlisting><![CDATA[
  495. MethodPattern :=
  496. AnnotationPattern? MethodModifiersPattern? TypePattern
  497. (TypePattern DotOrDotDot)? SimpleNamePattern
  498. '(' FormalsPattern ')'ThrowsPattern?
  499. MethodModifiersPattern := '!'? MethodModifier MethodModifiersPattern*
  500. MethodModifier := 'public' | 'private' | 'protected' | 'static' |
  501. 'synchronized' | 'final'
  502. FormalsPattern := '..' (',' FormalsPatternAfterDotDot)* |
  503. OptionalParensTypePattern (',' FormalsPattern)* |
  504. TypePattern '...'
  505. FormalsPatternAfterDotDot :=
  506. OptionalParensTypePattern (',' FormalsPatternAfterDotDot)* |
  507. TypePattern '...'
  508. ThrowsPattern := 'throws' TypePatternList
  509. TypePatternList := TypePattern (',' TypePattern)*
  510. ]]></programlisting>
  511. <para><emphasis>Note: compared to the previous version, this definition of MethodPattern does
  512. not allow parameter annotation matching (only matching on annotations of parameter types).</emphasis></para>
  513. <para>A <literal>ConstructorPattern</literal> has the form</para>
  514. <programlisting><![CDATA[
  515. ConstructorPattern :=
  516. AnnotationPattern? ConstructorModifiersPattern?
  517. (TypePattern DotOrDotDot)? 'new' '(' FormalsPattern ')'
  518. ThrowsPattern?
  519. ConstructorModifiersPattern := '!'? ConstructorModifier ConstructorModifiersPattern*
  520. ConstructorModifier := 'public' | 'private' | 'protected'
  521. ]]></programlisting>
  522. <para>
  523. The optional <literal>AnnotationPattern</literal> at the beginning of a
  524. method or constructor pattern restricts matches to methods/constructors with
  525. annotations that match the pattern. For example:
  526. </para>
  527. <variablelist>
  528. <varlistentry>
  529. <term>@Oneway * *(..)</term>
  530. <listitem>
  531. <para>
  532. Matches a method with any return type and any name, that has an annotation of
  533. type <literal>@Oneway</literal>.
  534. </para>
  535. </listitem>
  536. </varlistentry>
  537. <varlistentry>
  538. <term>@Transaction * (@Persistent org.xyz..*).*(..)</term>
  539. <listitem>
  540. <para>
  541. Matches a method with the <literal>@Transaction</literal> annotation,
  542. declared in a type with the <literal>@Persistent</literal> annotation, and
  543. in a package beginning with the <literal>org.xyz</literal> prefix.
  544. </para>
  545. </listitem>
  546. </varlistentry>
  547. <varlistentry>
  548. <term>* *.*(@Immutable *,..)</term>
  549. <listitem>
  550. <para>
  551. Matches any method taking at least one parameter, where the parameter
  552. type has an annotation <literal>@Immutable</literal>.
  553. </para>
  554. </listitem>
  555. </varlistentry>
  556. </variablelist>
  557. </sect2>
  558. <sect2>
  559. <title>Example Pointcuts</title>
  560. <variablelist>
  561. <varlistentry>
  562. <term>within(@Secure *)</term>
  563. <listitem>
  564. <para>
  565. Matches any join point where the code executing is declared in a
  566. type with an <literal>@Secure</literal>
  567. annotation. The format of the <literal>within</literal> pointcut designator
  568. in AspectJ 5 is <literal>'within' '(' OptionalParensTypePattern ')'</literal>.
  569. </para>
  570. </listitem>
  571. </varlistentry>
  572. <varlistentry>
  573. <term>staticinitialization(@Persistent *)</term>
  574. <listitem>
  575. <para>
  576. Matches the staticinitialization join point of any type with the
  577. <literal>@Persistent</literal> annotation. The format of the
  578. <literal>staticinitialization</literal> pointcut designator
  579. in AspectJ 5 is <literal>'staticinitialization' '(' OptionalParensTypePattern ')'</literal>.
  580. </para>
  581. </listitem>
  582. </varlistentry>
  583. <varlistentry>
  584. <term>call(@Oneway * *(..))</term>
  585. <listitem>
  586. <para>
  587. Matches a call to a method with a <literal>@Oneway</literal> annotation.
  588. </para>
  589. </listitem>
  590. </varlistentry>
  591. <varlistentry>
  592. <term>execution(public (@Immutable *) org.xyz..*.*(..))</term>
  593. <listitem>
  594. <para>
  595. The execution of any public method in a package with prefix
  596. <literal>org.xyz</literal>, where the method returns an
  597. immutable result.
  598. </para>
  599. </listitem>
  600. </varlistentry>
  601. <varlistentry>
  602. <term>set(@Cachable * *)</term>
  603. <listitem>
  604. <para>
  605. Matches the set of any cachable field.
  606. </para>
  607. </listitem>
  608. </varlistentry>
  609. <varlistentry>
  610. <term>handler(!@Catastrophic *)</term>
  611. <listitem>
  612. <para>
  613. Matches the handler join point for the handling of any exception that is
  614. not <literal>Catastrophic</literal>. The format of the <literal>handler</literal>
  615. pointcut designator in AspectJ 5 is <literal>'handler' '(' OptionalParensTypePattern ')'</literal>.
  616. </para>
  617. </listitem>
  618. </varlistentry>
  619. </variablelist>
  620. </sect2>
  621. <sect2>
  622. <title>Runtime type matching and context exposure</title>
  623. <para>AspectJ 5 supports a set of "@" pointcut designators which
  624. can be used both to match based on the presence of an annotation at
  625. runtime, and to expose the annotation value as context in a pointcut or
  626. advice definition. These designators are <literal>@args, @this, @target,
  627. @within, @withincode</literal>, and <literal>@annotation</literal>
  628. </para>
  629. <para>It is a compilation error to attempt to match on an annotation type
  630. that does not have runtime retention using <literal>@this, @target</literal>
  631. or <literal>@args</literal>. It is a compilation error to attempt to use
  632. any of these designators to expose an annotation value that does not
  633. have runtime retention.</para>
  634. <para>
  635. The <literal>this()</literal>, <literal>target()</literal>, and
  636. <literal>args()</literal> pointcut designators allow matching based
  637. on the runtime type of an object, as opposed to the statically
  638. declared type. In AspectJ 5, these designators are supplemented
  639. with three new designators : <literal>@this()</literal> (read, "this
  640. annotation"), <literal>@target()</literal>, and <literal>@args()</literal>.
  641. </para>
  642. <para>
  643. Like their counterparts, these pointcut designators can be used
  644. both for join point matching, and to expose context. The format of
  645. these new designators is:
  646. </para>
  647. <programlisting><![CDATA[
  648. AtThis := '@this' '(' AnnotationOrIdentifer ')'
  649. AtTarget := '@target' '(' AnnotationOrIdentifier ')'
  650. AnnotationOrIdentifier := FullyQualifiedName | Identifier
  651. AtArgs := '@args' '(' AnnotationsOrIdentifiersPattern ')'
  652. AnnotationsOrIdentifiersPattern :=
  653. '..' (',' AnnotationsOrIdentifiersPatternAfterDotDot)? |
  654. AnnotationOrIdentifier (',' AnnotationsOrIdentifiersPattern)* |
  655. '*' (',' AnnotationsOrIdentifiersPattern)*
  656. AnnotationsOrIdentifiersPatternAfterDotDot :=
  657. AnnotationOrIdentifier (',' AnnotationsOrIdentifiersPatternAfterDotDot)* |
  658. '*' (',' AnnotationsOrIdentifiersPatternAfterDotDot)*
  659. ]]></programlisting>
  660. <para>
  661. The forms of <literal>@this()</literal> and <literal>@target()</literal> that
  662. take a single annotation name are analogous to their counterparts that take
  663. a single type name. They match at join points where the object bound to
  664. <literal>this</literal> (or <literal>target</literal>, respectively) has an
  665. annotation of the specified type. For example:
  666. </para>
  667. <variablelist>
  668. <varlistentry>
  669. <term>@this(Foo)</term>
  670. <listitem>
  671. <para>
  672. Matches any join point where the object currently bound to 'this'
  673. has an annotation of type <literal>Foo</literal>.
  674. </para>
  675. </listitem>
  676. </varlistentry>
  677. <varlistentry>
  678. <term>call(* *(..)) &amp;&amp; @target(Classified)</term>
  679. <listitem>
  680. <para>
  681. Matches a call to any object where the target of the call has
  682. a <literal>@Classified</literal> annotation.
  683. </para>
  684. </listitem>
  685. </varlistentry>
  686. </variablelist>
  687. <para>
  688. Annotations can be exposed as context in the body of advice by
  689. using the forms of <literal>@this(), @target()</literal> and
  690. <literal>@args()</literal> that use bound variables in the place
  691. of annotation names. For example:
  692. </para>
  693. <programlisting><![CDATA[
  694. pointcut callToClassifiedObject(Classified classificationInfo) :
  695. call(* *(..)) && @target(classificationInfo);
  696. pointcut txRequiredMethod(Tx transactionAnnotation) :
  697. execution(* *(..)) && @this(transactionAnnotation)
  698. && if(transactionAnnotation.policy() == TxPolicy.REQUIRED);
  699. ]]></programlisting>
  700. <para>
  701. The <literal>@args</literal> pointcut designator behaves as its <literal>args</literal>
  702. counterpart, matching join points based on number and position of arguments, and
  703. supporting the <literal>*</literal> wildcard and at most one <literal>..</literal>
  704. wildcard. An annotation at a given position in an <literal>@args</literal> expression
  705. indicates that the runtime type of the argument in that position at a join point must
  706. have an annotation of the indicated type. For example:
  707. </para>
  708. <programlisting><![CDATA[
  709. /**
  710. * matches any join point with at least one argument, and where the
  711. * type of the first argument has the @Classified annotation
  712. */
  713. pointcut classifiedArgument() : @args(Classified,..);
  714. /**
  715. * matches any join point with three arguments, where the third
  716. * argument has an annotation of type @Untrusted.
  717. */
  718. pointcut untrustedData(Untrusted untrustedDataSource) :
  719. @args(*,*,untrustedDataSource);
  720. ]]></programlisting>
  721. <para>
  722. <emphasis>Note: an alternative design would be to allow both annotation
  723. patterns and type patterns to be specified in the existing args pcd.
  724. This works well for matching, but is more awkward when it comes to
  725. exposing context.</emphasis>
  726. </para>
  727. <para>Access to <literal>AnnotatedElement</literal> information is available
  728. reflectively with the body of advice through the <literal>thisJoinPoint</literal>,
  729. <literal>thisJoinPointStaticPart</literal>, and
  730. <literal>thisEnclosingJoinPointStaticPart</literal> variables. To access
  731. annotations on the arguments, or object bound to this or target at a join
  732. point you can use the following code fragments:</para>
  733. <programlisting><![CDATA[
  734. Annotation[] thisAnnotations = thisJoinPoint.getThis().getClass().getAnnotations();
  735. Annotation[] targetAnnotations = thisJoinPoint.getTarget().getClass().getAnnotations();
  736. Annotation[] firstParamAnnotations = thisJoinPoint.getArgs()[0].getClass().getAnnotations();
  737. ]]></programlisting>
  738. <para>
  739. <emphasis>Note: it would be nicer to provide direct helper methods in
  740. the JoinPoint interface or a sub-interface that provide the annotations
  741. directly, something like "AnnotatedElement getThisAnnotationInfo()".
  742. The problem here is that the "AnnotatedElement" type is only in the
  743. Java 5 runtime libraries, and we don't want to tie the AspectJ runtime
  744. library to Java 5. A sub-interface and downcast solution could be used
  745. if these helpers were felt to be sufficiently important.</emphasis>
  746. </para>
  747. <para>
  748. The <literal>@within</literal> and <literal>@withincode</literal> pointcut designators
  749. match any join point where the executing code is defined within a type (<literal>@within</literal>),
  750. or a method/constructor (<literal>@withincode</literal>) that has an annotation of the specified
  751. type. The form of these designators is:
  752. </para>
  753. <programlisting><![CDATA[
  754. AtWithin := '@within' '(' AnnotationOrIdentifier ')'
  755. AtWithinCode := '@withincode' '(' AnnotationOrIdentifier ')'
  756. ]]></programlisting>
  757. <para>Some examples of using these designators follow:</para>
  758. <variablelist>
  759. <varlistentry>
  760. <term>@within(Foo)</term>
  761. <listitem>
  762. <para>
  763. Matches any join point where the executing code is defined
  764. within a type which has an annotation of type <literal>Foo</literal>.
  765. </para>
  766. </listitem>
  767. </varlistentry>
  768. <varlistentry>
  769. <term>pointcut insideCriticalMethod(Critical c) :
  770. @withincode(c);</term>
  771. <listitem>
  772. <para>
  773. Matches any join point where the executing code is defined
  774. in a method or constructor which has an annotation of type <literal>@Critical</literal>,
  775. and exposes the value of the annotation in the parameter
  776. <literal>c</literal>.
  777. </para>
  778. </listitem>
  779. </varlistentry>
  780. </variablelist>
  781. <para>The <literal>@annotation</literal> pointcut designator matches any
  782. join point where the <emphasis>subject</emphasis> of the join point has
  783. an annotation of the given type. Like the other @pcds, it can also be
  784. used for context exposure.</para>
  785. <programlisting><![CDATA[
  786. AtAnnotation := '@annotation' '(' AnnotationOrIdentifier ')'
  787. ]]></programlisting>
  788. <para>The subject of a join point is defined in the table in chapter one of
  789. this guide.</para>
  790. <para>
  791. Access to annotation information on members at a matched join point is also available
  792. through the <literal>getSignature</literal> method of the <literal>JoinPoint</literal>
  793. and <literal>JoinPoint.StaticPart</literal> interfaces. The <literal>Signature</literal>
  794. interfaces are extended with additional operations that provide access to the
  795. <literal>java.lang.reflect</literal> <literal>Method, Field</literal> and
  796. <literal>Constructor</literal> objects on which annnotations can be queried. The following fragment
  797. illustrates an example use of this interface to access annotation information.
  798. </para>
  799. <programlisting><![CDATA[
  800. Signature sig = thisJoinPointStaticPart.getSignature();
  801. AnnotatedElement declaringTypeAnnotationInfo = sig.getDeclaringType();
  802. if (sig instanceof MethodSignature) {
  803. // this must be a call or execution join point
  804. Method method = ((MethodSignature)sig).getMethod();
  805. }
  806. ]]></programlisting>
  807. <para>
  808. <emphasis>Note again that it would be nicer to add the method getAnnotationInfo
  809. directly to MemberSignature, but this would once more couple the runtime library
  810. to Java 5.</emphasis>
  811. </para>
  812. <para>
  813. The <literal>@this,@target</literal> and <literal>@args</literal>
  814. pointcut designators can only be used to match against annotations
  815. that have runtime retention. The <literal>@within, @withincode</literal>
  816. and <literal>@annotation</literal> pointcut designators can only be used
  817. to match against annotations that have at least class-file retention, and
  818. if used in the binding form the annotation must have runtime retention.
  819. </para>
  820. </sect2>
  821. <sect2>
  822. <title>Package and Parameter Annotations</title>
  823. <para>
  824. <emphasis>Note: A previous design allowed package annotation patterns to be specified
  825. directly in type patterns, and parameter annotation patterns to be
  826. specified directly in method and constructor signature patterns. Because
  827. this made some pointcut expressions hard to read and understand, we moved
  828. in favour of the design presented below, which also has its drawbacks.
  829. Matching on package and parameter annotations will be
  830. deferred until after the 1.5.0 release so that we can gain more understanding
  831. of the kinds of uses AspectJ users are making of annotations in pointcut
  832. expressions before commiting to any one approach.</emphasis>
  833. </para>
  834. <!-- @withinpackage ??? -->
  835. <!--
  836. <para>
  837. Java 5 allows both packages and parameters to be annotated. To allow matching on package and parameter annotations,
  838. AspectJ 5 introduces the <literal>@package</literal> and <literal>@parameters</literal> pointcut designators.
  839. </para>
  840. <programlisting><![CDATA[
  841. PackageAnnotationPointcut := '@package' '(' AnnotationPattern ')'
  842. ]]></programlisting>
  843. <para>The <literal>@package</literal> pointcut matches any join point
  844. occuring within the scope of a package with
  845. annotations matching the giving <literal>AnnotationPattern</literal>. For
  846. example:
  847. </para>
  848. <programlisting><![CDATA[
  849. @package(@Model)
  850. ]]></programlisting>
  851. <para>
  852. Matches any join point occuring within the scope of a package with the
  853. <literal>@Model</literal> annotation.
  854. </para>
  855. <para>
  856. <emphasis>
  857. Note: we added @package as a result of a conscious decision not to allow the
  858. specification of package annotation patterns within a general TypePattern. A
  859. consequence of this decision is that we lose the ability to say the following
  860. things: "a call to a method defined in a type in a package with annotations
  861. matching..." ; "the set of a field defined in a type in a package with annotations
  862. matching..." ; "the get of a field defined in a type in a package with annotations
  863. matching...". As well as the package of the target at these join points, there is
  864. also the package of the runtime type of the target (call/target difference). So
  865. there are at least three possible sets of package annotations you could theoretically
  866. want to match on at a call, get, or set join point. We have chosen to provide the
  867. means to express the simplest of these, and could consider extending the language
  868. to allow for the others in the future when we better understanding how users will
  869. really use both package annotations and these features.
  870. </emphasis>
  871. </para>
  872. <para>
  873. The <literal>@parameter</literal> pointcut designator acts in a similar manner to
  874. <literal>args</literal> in that it matches based on number and position of arguments
  875. at a join point (and supports the same wildcard options of <literal>*</literal> and
  876. <literal>..</literal>.
  877. </para>
  878. <programlisting><![CDATA[
  879. ParamsAnnotationPointcut := '@parameters' '(' ParamsAnnotationPattern ')'
  880. ParamsAnnotationPattern := AnnotationPattern (',' ParamsAnnotationPattern)? |
  881. '*' (',' ParamsAnnotationPattern)? |
  882. '..' (',' SingleParamsAnnotationPattern)*
  883. SingleParamsAnnotationPattern := AnnotationPattern (',' SingleParamsAnnotationPattern)? |
  884. '*' (',' SingleParamsAnnotationPattern)?
  885. ]]></programlisting>
  886. <para>The <literal>*</literal> wildcard matches a single parameter regardless of its annotations.
  887. The <literal>..</literal> wildcard matches zero or more parameters with any annotations. An
  888. annotation pattern in a given parameter position matches a parameter in that position with annotations
  889. matching the given annotation pattern. For example, the method signature</para>
  890. <programlisting><![CDATA[
  891. public void foo(@Immutable int i, String s, @Cached Object o);
  892. ]]></programlisting>
  893. <para>
  894. Is matched by:
  895. </para>
  896. <programlisting><![CDATA[
  897. @parameters(@Immutable, *, @Cached);
  898. and,
  899. @parameters(..,@Cached);
  900. and,
  901. @parameters(@Immutable, *, *);
  902. ]]></programlisting>
  903. <para>
  904. It is not matched by:
  905. </para>
  906. <programlisting><![CDATA[
  907. @parameters(@Immutable, *);
  908. or,
  909. @parameters(*,@Immutable);
  910. or,
  911. @parameters(*, int, @Cached);
  912. ]]></programlisting>
  913. <para>
  914. This last example will result in a compilation error since <literal>int</literal> is not a
  915. valid annotation pattern.
  916. </para>
  917. -->
  918. </sect2>
  919. <sect2>
  920. <title>Annotation Inheritance and pointcut matching</title>
  921. <para>
  922. According to the Java 5 specification, non-type annotations are not
  923. inherited, and annotations on types are only inherited if they have the
  924. <literal>@Inherited</literal> meta-annotation.
  925. Given the following program:
  926. </para>
  927. <programlisting><![CDATA[
  928. class C1 {
  929. @SomeAnnotation
  930. public void aMethod() {...}
  931. }
  932. class C2 extends C1 {
  933. public void aMethod() {...}
  934. }
  935. class Main {
  936. public static void main(String[] args) {
  937. C1 c1 = new C1();
  938. C2 c2 = new C2();
  939. c1.aMethod();
  940. c2.aMethod();
  941. }
  942. }
  943. aspect X {
  944. pointcut annotatedMethodCall() :
  945. call(@SomeAnnotation * C1.aMethod());
  946. pointcut c1MethodCall() :
  947. call(* C1.aMethod());
  948. }
  949. ]]></programlisting>
  950. <para>
  951. The pointcut <literal>annotatedMethodCall</literal> will match the call
  952. to <literal>c1.aMethod()</literal>, but not the call to
  953. <literal>c2.aMethod()</literal>.
  954. </para>
  955. <para>
  956. The pointcut <literal>c1MethodCall</literal> matches both
  957. <literal>c1.aMethod()</literal> and <literal>c2.aMethod()</literal>.
  958. </para>
  959. </sect2>
  960. <sect2>
  961. <title>Limitations</title>
  962. <para>
  963. It would be useful to be able to match join points based on
  964. annotation values, rather than merely the presence of a
  965. class-file retention annotation of a given type. This facility may be supported in a future version of AspectJ, by expanding the
  966. definition of <literal>AnnotationPattern</literal>. Matching annotation values for
  967. annotations with runtime retention can be done by exposing the annotation value
  968. as a pointcut parameter and then using an <literal>if</literal> pointcut expression
  969. to test the value.
  970. </para>
  971. </sect2>
  972. </sect1>
  973. <!-- ============================== -->
  974. <sect1 id="annotations-decp">
  975. <title>Using Annotations with declare statements</title>
  976. <sect2>
  977. <title>Declare error and declare warning</title>
  978. <para>
  979. Since pointcut expressions in AspectJ 5 support join point matching based
  980. on annotations, this facility can be exploited when writing
  981. <literal>declare warning</literal> and <literal>declare error</literal>
  982. statements. For example:
  983. </para>
  984. <programlisting><![CDATA[
  985. declare warning : withincode(@PerformanceCritical * *(..)) &&
  986. call(@ExpensiveOperation * *(..))
  987. : "Expensive operation called from within performance critical section";
  988. ]]></programlisting>
  989. <programlisting><![CDATA[
  990. declare error : call(* org.xyz.model.*.*(..)) &&
  991. !@within(Trusted)
  992. : "Untrusted code should not call the model classes directly";
  993. ]]></programlisting>
  994. </sect2>
  995. <sect2>
  996. <title>declare parents</title>
  997. <para>
  998. The general form of a <literal>declare parents</literal> statement is:
  999. </para>
  1000. <programlisting><![CDATA[
  1001. declare parents : TypePattern extends Type;
  1002. declare parents : TypePattern implements TypeList;
  1003. ]]></programlisting>
  1004. <para>
  1005. Since AspectJ 5 supports annotations as part of a type pattern
  1006. specification, it is now possible to match types based on the presence
  1007. of annotations <emphasis>with either class-file or runtime retention</emphasis>.
  1008. For example:
  1009. </para>
  1010. <variablelist>
  1011. <varlistentry>
  1012. <term>declare parents : (@Secured *) implements SecuredObject;</term>
  1013. <listitem>
  1014. <para>
  1015. All types with the <literal>@Secured</literal> annotation
  1016. implement the <literal>SecuredObject</literal> inteface.
  1017. </para>
  1018. </listitem>
  1019. </varlistentry>
  1020. <varlistentry>
  1021. <term>declare parents : (@Secured BankAccount+) implements SecuredObject;</term>
  1022. <listitem>
  1023. <para>
  1024. The subset of types drawn from the <literal>BankAccount</literal> type and any subtype of
  1025. <literal>BankAccount</literal>, where the
  1026. <literal>@Secured</literal> annotation is present, implement the
  1027. <literal>SecuredObject</literal> interface.
  1028. </para>
  1029. </listitem>
  1030. </varlistentry>
  1031. </variablelist>
  1032. <para>An annotation type may not be used as the target of a declare parents
  1033. statement. If an annotation type is named explicitly as the target of a
  1034. declare parents statement, a compilation error will result. If an annotation
  1035. type is matched by a non-explicit type pattern used in a declare parents
  1036. statement it will be ignored (and an XLint warning issued).</para>
  1037. </sect2>
  1038. <sect2>
  1039. <title>declare precedence</title>
  1040. <para>
  1041. The general form of a declare precedence statement is:
  1042. </para>
  1043. <programlisting><![CDATA[
  1044. declare precedence : TypePatList;
  1045. ]]></programlisting>
  1046. <para>
  1047. AspectJ 5 allows the type patterns in the list to include annotation information
  1048. as part of the pattern specification. For example:
  1049. </para>
  1050. <variablelist>
  1051. <varlistentry>
  1052. <term>declare precedence : (@Security *),*;</term>
  1053. <listitem>
  1054. <para>
  1055. All aspects with the <literal>@Security</literal> annotation
  1056. take precedence over any other aspects in the system. (Or, more
  1057. informally, all security-related aspects take precedence).
  1058. </para>
  1059. </listitem>
  1060. </varlistentry>
  1061. </variablelist>
  1062. </sect2>
  1063. </sect1>
  1064. <!-- ============================== -->
  1065. <sect1 id="annotations-declare">
  1066. <title>Declare Annotation</title>
  1067. <para>AspectJ 5 supports a new kind of declare statement, <literal>declare annotation</literal>.
  1068. This takes different forms according to the recipient of the annotation:
  1069. <literal>declare @type</literal> for types, <literal>declare @method</literal> for methods,
  1070. <literal>declare @constructor</literal> for constructors, and <literal>declare @field</literal>
  1071. for fields. <literal>declare @package</literal> may be supported in a future release.
  1072. </para>
  1073. <para>The general form is:</para>
  1074. <programlisting><![CDATA[
  1075. declare @<kind> : ElementPattern : Annotation ;
  1076. ]]></programlisting>
  1077. <para>Where annotation is a regular annotation expression as defined in the Java 5 language. If the annotation has
  1078. the <literal>@Target</literal> meta-annotation, then the elements matched by <literal>ElementPattern</literal>
  1079. must be of the kind specified by the <literal>@Target</literal> annotation.</para>
  1080. <para><literal>ElementPattern</literal> is defined as follows:</para>
  1081. <programlisting><![CDATA[
  1082. ElementPattern := TypePattern |
  1083. MethodPattern |
  1084. ConstructorPattern |
  1085. FieldPattern
  1086. ]]></programlisting>
  1087. <para>The following examples illustrate the use of <literal>declare annotation</literal>.</para>
  1088. <variablelist>
  1089. <varlistentry>
  1090. <term>declare @type : org.xyz.model..* : @BusinessDomain ;</term>
  1091. <listitem>
  1092. <para>
  1093. All types defined in a package with the prefix <literal>org.xyz.model</literal>
  1094. have the <literal>@BusinessDomain</literal> annotation.
  1095. </para>
  1096. </listitem>
  1097. </varlistentry>
  1098. <varlistentry>
  1099. <term>declare @method : public * BankAccount+.*(..) : @Secured(role="supervisor")</term>
  1100. <listitem>
  1101. <para>
  1102. All public methods in <literal>BankAccount</literal> and its subtypes have the
  1103. annotation <literal>@Secured(role="supervisor")</literal>.
  1104. </para>
  1105. </listitem>
  1106. </varlistentry>
  1107. <varlistentry>
  1108. <term>declare @constructor : BankAccount+.new(..) : @Secured(role="supervisor")</term>
  1109. <listitem>
  1110. <para>
  1111. All constructors in <literal>BankAccount</literal> and its subtypes have the
  1112. annotation <literal>@Secured(role="supervisor")</literal>.
  1113. </para>
  1114. </listitem>
  1115. </varlistentry>
  1116. <varlistentry>
  1117. <term>declare @field : * DAO+.* : @Persisted;</term>
  1118. <listitem>
  1119. <para>
  1120. All fields defined in <literal>DAO</literal> or its subtypes have the
  1121. <literal>@Persisted</literal> annotation.
  1122. </para>
  1123. </listitem>
  1124. </varlistentry>
  1125. </variablelist>
  1126. </sect1>
  1127. <sect1 id="annotations-itds">
  1128. <title>Inter-type Declarations</title>
  1129. <para>An annotation type may not be the target of an inter-type declaration.</para>
  1130. </sect1>
  1131. </chapter>