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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  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 id="using-annotations" xreflabel="using-annotations">
  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 id="retention-policies" xreflabel="retention-policies">
  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 id="accessing-annotations-at-runtime" xreflabel="accessing-annotations-at-runtime">
  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 id="annotation-inheritance" xreflabel="annotation-inheritance">
  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 id="annotation-patterns" xreflabel="annotation-patterns">
  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.An annotation pattern element has one of two basic
  253. forms:
  254. </para>
  255. <itemizedlist>
  256. <listitem>@&lt;qualified-name&gt;, for example, @Foo, or
  257. @org.xyz.Foo.</listitem>
  258. <listitem>@(&lt;type-pattern&gt;), for example, @(org.xyz..*), or
  259. @(Foo || Boo)</listitem>
  260. </itemizedlist>
  261. <para>These simple elements may be negated using <literal>!</literal>, and
  262. combined by simple concatentation. The pattern <literal>@Foo @Boo</literal>
  263. matches an annotated element that has both an annotation of type <literal>Foo</literal>
  264. and an annotation of type <literal>Boo</literal>.</para>
  265. <para>Some examples of annotation patterns follow:</para>
  266. <variablelist>
  267. <varlistentry>
  268. <term>@Immutable</term>
  269. <listitem>
  270. <para>
  271. Matches any annotated element which has an annotation of
  272. type <literal>Immutable</literal>.
  273. </para>
  274. </listitem>
  275. </varlistentry>
  276. <varlistentry>
  277. <term>!@Persistent</term>
  278. <listitem>
  279. <para>
  280. Matches any annotated element which does not have an annotation of
  281. type <literal>Persistent</literal>.
  282. </para>
  283. </listitem>
  284. </varlistentry>
  285. <varlistentry>
  286. <term>@Foo @Goo</term>
  287. <listitem>
  288. <para>
  289. Matches any annotated element which has both an annotation of type <literal>Foo</literal> and
  290. an annotation of type <literal>Goo</literal>.
  291. </para>
  292. </listitem>
  293. </varlistentry>
  294. <varlistentry>
  295. <term>@(Foo || Goo)</term>
  296. <listitem>
  297. <para>
  298. Matches any annotated element which has either an annotation of a type matching
  299. the type pattern <literal>(Foo || Goo)</literal>.
  300. In other words, an annotated element with either an
  301. annotation of type <literal>Foo</literal> or
  302. an annotation of type <literal>Goo</literal> (or both). (The parenthesis are required in this example).
  303. </para>
  304. </listitem>
  305. </varlistentry>
  306. <varlistentry>
  307. <term>@(org.xyz..*)</term>
  308. <listitem>
  309. <para>
  310. Matches any annotated element which has either an annotation of a type matching
  311. the type pattern <literal>(org.xyz..*)</literal>.
  312. In other words, an annotated element with an annotation that is declared in the
  313. org.xyz package or a sub-package. (The parenthesis are required in this example).
  314. </para>
  315. </listitem>
  316. </varlistentry>
  317. </variablelist>
  318. </sect2>
  319. <sect2 id="type-patterns" xreflabel="type-patterns">
  320. <title>Type Patterns</title>
  321. <para>AspectJ 1.5 extends type patterns to allow an optional <literal>AnnotationPattern</literal>
  322. prefix.</para>
  323. <programlisting><![CDATA[
  324. TypePattern := SimpleTypePattern |
  325. '!' TypePattern |
  326. '(' AnnotationPattern? TypePattern ')'
  327. TypePattern '&&' TypePattern |
  328. TypePattern '||' TypePattern
  329. SimpleTypePattern := DottedNamePattern '+'? '[]'*
  330. DottedNamePattern := FullyQualifiedName RestOfNamePattern? |
  331. '*' NotStarNamePattern?
  332. RestOfNamePattern := '..' DottedNamePattern |
  333. '*' NotStarNamePattern?
  334. NotStarNamePattern := FullyQualifiedName RestOfNamePattern? |
  335. '..' DottedNamePattern
  336. FullyQualifiedName := JavaIdentifierCharacter+ ('.' JavaIdentifierCharacter+)*
  337. ]]></programlisting>
  338. <para>Note that in most cases when annotations are used as part of a type pattern,
  339. the parenthesis are required (as in <literal>(@Foo Hello+)</literal>). In
  340. some cases (such as a type pattern used within a <literal>within</literal> or
  341. <literal>handler</literal>
  342. pointcut expression), the parenthesis are optional:</para>
  343. <programlisting><![CDATA[
  344. OptionalParensTypePattern := AnnotationPattern? TypePattern
  345. ]]></programlisting>
  346. <para>
  347. The following examples illustrate the use of annotations in type
  348. patterns:
  349. </para>
  350. <variablelist>
  351. <varlistentry>
  352. <term>(@Immutable *)</term>
  353. <listitem>
  354. <para>
  355. Matches any type with an <literal>@Immutable</literal> annotation.
  356. </para>
  357. </listitem>
  358. </varlistentry>
  359. <varlistentry>
  360. <term>(!@Immutable *)</term>
  361. <listitem>
  362. <para>
  363. Matches any type which does not have an <literal>@Immutable</literal> annotation.
  364. </para>
  365. </listitem>
  366. </varlistentry>
  367. <varlistentry>
  368. <term> (@Immutable (org.xyz.* || org.abc.*))</term>
  369. <listitem>
  370. <para>
  371. Matches any type in the <literal>org.xyz</literal> or <literal>org.abc</literal>
  372. packages with the <literal>@Immutable</literal> annotation.
  373. </para>
  374. </listitem>
  375. </varlistentry>
  376. <varlistentry>
  377. <term>((@Immutable Foo+) || Goo)</term>
  378. <listitem>
  379. <para>
  380. Matches a type <literal>Foo</literal> or any of its subtypes, which have the <literal>@Immutable</literal>
  381. annotation, or a type <literal>Goo</literal>.
  382. </para>
  383. </listitem>
  384. </varlistentry>
  385. <varlistentry>
  386. <term>((@(Immutable || NonPersistent) org.xyz..*)</term>
  387. <listitem>
  388. <para>
  389. Matches any type in a package beginning with the prefix <literal>org.xyz</literal>,
  390. which has either the <literal>@Immutable</literal> annotation or the
  391. <literal>@NonPersistent</literal> annotation.
  392. </para>
  393. </listitem>
  394. </varlistentry>
  395. <varlistentry>
  396. <term>(@Immutable @NonPersistent org.xyz..*)</term>
  397. <listitem>
  398. <para>
  399. Matches any type in a package beginning with the prefix <literal>org.xyz</literal>,
  400. which has both an <literal>@Immutable</literal> annotation and an
  401. <literal>@NonPersistent</literal> annotation.
  402. </para>
  403. </listitem>
  404. </varlistentry>
  405. <varlistentry>
  406. <term> (@(@Inherited *) org.xyz..*)</term>
  407. <listitem>
  408. <para>
  409. Matches any type in a package beginning with the prefix <literal>org.xyz</literal>,
  410. which has an inheritable annotation. The annotation pattern
  411. <literal>@(@Inherited *)</literal> matches any annotation of a type matching the
  412. type pattern <literal>@Inherited *</literal>, which in turn matches any type with the
  413. <literal>@Inherited</literal> annotation.
  414. </para>
  415. </listitem>
  416. </varlistentry>
  417. </variablelist>
  418. </sect2>
  419. <sect2 id="signaturePatterns" xreflabel="Signature Patterns">
  420. <title>Signature Patterns</title>
  421. <sect3 id="fieldPatterns" xreflabel="Field Patterns">
  422. <title>Field Patterns</title>
  423. <para>A <literal>FieldPattern</literal> can optionally specify an annotation-matching
  424. pattern as the first element:</para>
  425. <programlisting><![CDATA[
  426. FieldPattern :=
  427. AnnotationPattern? FieldModifiersPattern?
  428. TypePattern (TypePattern DotOrDotDot)? SimpleNamePattern
  429. FieldModifiersPattern := '!'? FieldModifier FieldModifiersPattern*
  430. FieldModifier := 'public' | 'private' | 'protected' | 'static' |
  431. 'transient' | 'final'
  432. DotOrDotDot := '.' | '..'
  433. SimpleNamePattern := JavaIdentifierChar+ ('*' SimpleNamePattern)?
  434. ]]></programlisting>
  435. <para>
  436. If present, the <literal>AnnotationPattern</literal> restricts matches to fields with
  437. annotations that match the pattern. For example:
  438. </para>
  439. <variablelist>
  440. <varlistentry>
  441. <term>@SensitiveData * *</term>
  442. <listitem>
  443. <para>
  444. Matches a field of any type and any name, that has an annotation of
  445. type <literal>@SensitiveData</literal>
  446. </para>
  447. </listitem>
  448. </varlistentry>
  449. <varlistentry>
  450. <term>@SensitiveData List org.xyz..*.*</term>
  451. <listitem>
  452. <para>
  453. Matches a member field of a type in a package with prefix <literal>org.xzy</literal>,
  454. where the field is of type <literal>List</literal>, and has an annotation of type
  455. <literal>@SensitiveData</literal>
  456. </para>
  457. </listitem>
  458. </varlistentry>
  459. <varlistentry>
  460. <term>(@SensitiveData *) org.xyz..*.*</term>
  461. <listitem>
  462. <para>
  463. Matches a member field of a type in a package with prefix <literal>org.xzy</literal>,
  464. where the field is of a type which has a <literal>@SensitiveData</literal> annotation.
  465. </para>
  466. </listitem>
  467. </varlistentry>
  468. <varlistentry>
  469. <term>@Foo (@Goo *) (@Hoo *).*</term>
  470. <listitem>
  471. <para>
  472. Matches a field with an annotation <literal>@Foo</literal>, of a type with an
  473. annotation <literal>@Goo</literal>, declared in a type with annotation
  474. <literal>@Hoo</literal>.
  475. </para>
  476. </listitem>
  477. </varlistentry>
  478. <varlistentry>
  479. <term>@Persisted @Classified * *</term>
  480. <listitem>
  481. <para>
  482. Matches a field with an annotation <literal>@Persisted</literal> and
  483. an annotation <literal>@Classified</literal>.
  484. </para>
  485. </listitem>
  486. </varlistentry>
  487. </variablelist>
  488. </sect3>
  489. <sect3 id="methodPatterns" xreflabel="Method Patterns">
  490. <title>Method and Constructor Patterns</title>
  491. <para>A <literal>MethodPattern</literal> can optionally specify an annotation-matching
  492. pattern as the first element.</para>
  493. <programlisting><![CDATA[
  494. MethodPattern :=
  495. AnnotationPattern? MethodModifiersPattern? TypePattern
  496. (TypePattern DotOrDotDot)? SimpleNamePattern
  497. '(' FormalsPattern ')'ThrowsPattern?
  498. MethodModifiersPattern := '!'? MethodModifier MethodModifiersPattern*
  499. MethodModifier := 'public' | 'private' | 'protected' | 'static' |
  500. 'synchronized' | 'final'
  501. FormalsPattern := '..' (',' FormalsPatternAfterDotDot)* |
  502. OptionalParensTypePattern (',' FormalsPattern)* |
  503. TypePattern '...'
  504. FormalsPatternAfterDotDot :=
  505. OptionalParensTypePattern (',' FormalsPatternAfterDotDot)* |
  506. TypePattern '...'
  507. ThrowsPattern := 'throws' TypePatternList
  508. TypePatternList := TypePattern (',' TypePattern)*
  509. ]]></programlisting>
  510. <para>A <literal>ConstructorPattern</literal> has the form</para>
  511. <programlisting><![CDATA[
  512. ConstructorPattern :=
  513. AnnotationPattern? ConstructorModifiersPattern?
  514. (TypePattern DotOrDotDot)? 'new' '(' FormalsPattern ')'
  515. ThrowsPattern?
  516. ConstructorModifiersPattern := '!'? ConstructorModifier ConstructorModifiersPattern*
  517. ConstructorModifier := 'public' | 'private' | 'protected'
  518. ]]></programlisting>
  519. <para>
  520. The optional <literal>AnnotationPattern</literal> at the beginning of a
  521. method or constructor pattern restricts matches to methods/constructors with
  522. annotations that match the pattern. For example:
  523. </para>
  524. <variablelist>
  525. <varlistentry>
  526. <term>@Oneway * *(..)</term>
  527. <listitem>
  528. <para>
  529. Matches a method with any return type and any name, that has an annotation of
  530. type <literal>@Oneway</literal>.
  531. </para>
  532. </listitem>
  533. </varlistentry>
  534. <varlistentry>
  535. <term>@Transaction * (@Persistent org.xyz..*).*(..)</term>
  536. <listitem>
  537. <para>
  538. Matches a method with the <literal>@Transaction</literal> annotation,
  539. declared in a type with the <literal>@Persistent</literal> annotation, and
  540. in a package beginning with the <literal>org.xyz</literal> prefix.
  541. </para>
  542. </listitem>
  543. </varlistentry>
  544. <varlistentry>
  545. <term>* *.*(@Immutable *,..)</term>
  546. <listitem>
  547. <para>
  548. Matches any method taking at least one parameter, where the parameter
  549. type has an annotation <literal>@Immutable</literal>.
  550. </para>
  551. </listitem>
  552. </varlistentry>
  553. </variablelist>
  554. </sect3>
  555. </sect2>
  556. <sect2 id="example-pointcuts" xreflabel="example-pointcuts">
  557. <title>Example Pointcuts</title>
  558. <variablelist>
  559. <varlistentry>
  560. <term>within(@Secure *)</term>
  561. <listitem>
  562. <para>
  563. Matches any join point where the code executing is declared in a
  564. type with an <literal>@Secure</literal>
  565. annotation. The format of the <literal>within</literal> pointcut designator
  566. in AspectJ 5 is <literal>'within' '(' OptionalParensTypePattern ')'</literal>.
  567. </para>
  568. </listitem>
  569. </varlistentry>
  570. <varlistentry>
  571. <term>staticinitialization(@Persistent *)</term>
  572. <listitem>
  573. <para>
  574. Matches the staticinitialization join point of any type with the
  575. <literal>@Persistent</literal> annotation. The format of the
  576. <literal>staticinitialization</literal> pointcut designator
  577. in AspectJ 5 is <literal>'staticinitialization' '(' OptionalParensTypePattern ')'</literal>.
  578. </para>
  579. </listitem>
  580. </varlistentry>
  581. <varlistentry>
  582. <term>call(@Oneway * *(..))</term>
  583. <listitem>
  584. <para>
  585. Matches a call to a method with a <literal>@Oneway</literal> annotation.
  586. </para>
  587. </listitem>
  588. </varlistentry>
  589. <varlistentry>
  590. <term>execution(public (@Immutable *) org.xyz..*.*(..))</term>
  591. <listitem>
  592. <para>
  593. The execution of any public method in a package with prefix
  594. <literal>org.xyz</literal>, where the method returns an
  595. immutable result.
  596. </para>
  597. </listitem>
  598. </varlistentry>
  599. <varlistentry>
  600. <term>set(@Cachable * *)</term>
  601. <listitem>
  602. <para>
  603. Matches the set of any cachable field.
  604. </para>
  605. </listitem>
  606. </varlistentry>
  607. <varlistentry>
  608. <term>handler(!@Catastrophic *)</term>
  609. <listitem>
  610. <para>
  611. Matches the handler join point for the handling of any exception that is
  612. not <literal>Catastrophic</literal>. The format of the <literal>handler</literal>
  613. pointcut designator in AspectJ 5 is <literal>'handler' '(' OptionalParensTypePattern ')'</literal>.
  614. </para>
  615. </listitem>
  616. </varlistentry>
  617. </variablelist>
  618. </sect2>
  619. <sect2 id="runtime-type-matching-and-context-exposure" xreflabel="runtime-type-matching-and-context-exposure">
  620. <title>Runtime type matching and context exposure</title>
  621. <para>AspectJ 5 supports a set of "@" pointcut designators which
  622. can be used both to match based on the presence of an annotation at
  623. runtime, and to expose the annotation value as context in a pointcut or
  624. advice definition. These designators are <literal>@args, @this, @target,
  625. @within, @withincode</literal>, and <literal>@annotation</literal>
  626. </para>
  627. <para>It is a compilation error to attempt to match on an annotation type
  628. that does not have runtime retention using <literal>@this, @target</literal>
  629. or <literal>@args</literal>. It is a compilation error to attempt to use
  630. any of these designators to expose an annotation value that does not
  631. have runtime retention.</para>
  632. <para>
  633. The <literal>this()</literal>, <literal>target()</literal>, and
  634. <literal>args()</literal> pointcut designators allow matching based
  635. on the runtime type of an object, as opposed to the statically
  636. declared type. In AspectJ 5, these designators are supplemented
  637. with three new designators : <literal>@this()</literal> (read, "this
  638. annotation"), <literal>@target()</literal>, and <literal>@args()</literal>.
  639. </para>
  640. <para>
  641. Like their counterparts, these pointcut designators can be used
  642. both for join point matching, and to expose context. The format of
  643. these new designators is:
  644. </para>
  645. <programlisting><![CDATA[
  646. AtThis := '@this' '(' AnnotationOrIdentifer ')'
  647. AtTarget := '@target' '(' AnnotationOrIdentifier ')'
  648. AnnotationOrIdentifier := FullyQualifiedName | Identifier
  649. AtArgs := '@args' '(' AnnotationsOrIdentifiersPattern ')'
  650. AnnotationsOrIdentifiersPattern :=
  651. '..' (',' AnnotationsOrIdentifiersPatternAfterDotDot)? |
  652. AnnotationOrIdentifier (',' AnnotationsOrIdentifiersPattern)* |
  653. '*' (',' AnnotationsOrIdentifiersPattern)*
  654. AnnotationsOrIdentifiersPatternAfterDotDot :=
  655. AnnotationOrIdentifier (',' AnnotationsOrIdentifiersPatternAfterDotDot)* |
  656. '*' (',' AnnotationsOrIdentifiersPatternAfterDotDot)*
  657. ]]></programlisting>
  658. <para>
  659. The forms of <literal>@this()</literal> and <literal>@target()</literal> that
  660. take a single annotation name are analogous to their counterparts that take
  661. a single type name. They match at join points where the object bound to
  662. <literal>this</literal> (or <literal>target</literal>, respectively) has an
  663. annotation of the specified type. For example:
  664. </para>
  665. <variablelist>
  666. <varlistentry>
  667. <term>@this(Foo)</term>
  668. <listitem>
  669. <para>
  670. Matches any join point where the object currently bound to 'this'
  671. has an annotation of type <literal>Foo</literal>.
  672. </para>
  673. </listitem>
  674. </varlistentry>
  675. <varlistentry>
  676. <term>call(* *(..)) &amp;&amp; @target(Classified)</term>
  677. <listitem>
  678. <para>
  679. Matches a call to any object where the target of the call has
  680. a <literal>@Classified</literal> annotation.
  681. </para>
  682. </listitem>
  683. </varlistentry>
  684. </variablelist>
  685. <para>
  686. Annotations can be exposed as context in the body of advice by
  687. using the forms of <literal>@this(), @target()</literal> and
  688. <literal>@args()</literal> that use bound variables in the place
  689. of annotation names. For example:
  690. </para>
  691. <programlisting><![CDATA[
  692. pointcut callToClassifiedObject(Classified classificationInfo) :
  693. call(* *(..)) && @target(classificationInfo);
  694. pointcut txRequiredMethod(Tx transactionAnnotation) :
  695. execution(* *(..)) && @this(transactionAnnotation)
  696. && if(transactionAnnotation.policy() == TxPolicy.REQUIRED);
  697. ]]></programlisting>
  698. <para>
  699. The <literal>@args</literal> pointcut designator behaves as its <literal>args</literal>
  700. counterpart, matching join points based on number and position of arguments, and
  701. supporting the <literal>*</literal> wildcard and at most one <literal>..</literal>
  702. wildcard. An annotation at a given position in an <literal>@args</literal> expression
  703. indicates that the runtime type of the argument in that position at a join point must
  704. have an annotation of the indicated type. For example:
  705. </para>
  706. <programlisting><![CDATA[
  707. /**
  708. * matches any join point with at least one argument, and where the
  709. * type of the first argument has the @Classified annotation
  710. */
  711. pointcut classifiedArgument() : @args(Classified,..);
  712. /**
  713. * matches any join point with three arguments, where the third
  714. * argument has an annotation of type @Untrusted.
  715. */
  716. pointcut untrustedData(Untrusted untrustedDataSource) :
  717. @args(*,*,untrustedDataSource);
  718. ]]></programlisting>
  719. <para>In addition to accessing annotation information at runtime through context binding,
  720. access to <literal>AnnotatedElement</literal> information is also available
  721. reflectively with the body of advice through the <literal>thisJoinPoint</literal>,
  722. <literal>thisJoinPointStaticPart</literal>, and
  723. <literal>thisEnclosingJoinPointStaticPart</literal> variables. To access
  724. annotations on the arguments, or object bound to this or target at a join
  725. point you can use the following code fragments:</para>
  726. <programlisting><![CDATA[
  727. Annotation[] thisAnnotations = thisJoinPoint.getThis().getClass().getAnnotations();
  728. Annotation[] targetAnnotations = thisJoinPoint.getTarget().getClass().getAnnotations();
  729. Annotation[] firstParamAnnotations = thisJoinPoint.getArgs()[0].getClass().getAnnotations();
  730. ]]></programlisting>
  731. <para>
  732. The <literal>@within</literal> and <literal>@withincode</literal> pointcut designators
  733. match any join point where the executing code is defined within a type (<literal>@within</literal>),
  734. or a method/constructor (<literal>@withincode</literal>) that has an annotation of the specified
  735. type. The form of these designators is:
  736. </para>
  737. <programlisting><![CDATA[
  738. AtWithin := '@within' '(' AnnotationOrIdentifier ')'
  739. AtWithinCode := '@withincode' '(' AnnotationOrIdentifier ')'
  740. ]]></programlisting>
  741. <para>Some examples of using these designators follow:</para>
  742. <variablelist>
  743. <varlistentry>
  744. <term>@within(Foo)</term>
  745. <listitem>
  746. <para>
  747. Matches any join point where the executing code is defined
  748. within a type which has an annotation of type <literal>Foo</literal>.
  749. </para>
  750. </listitem>
  751. </varlistentry>
  752. <varlistentry>
  753. <term>pointcut insideCriticalMethod(Critical c) :
  754. @withincode(c);</term>
  755. <listitem>
  756. <para>
  757. Matches any join point where the executing code is defined
  758. in a method or constructor which has an annotation of type <literal>@Critical</literal>,
  759. and exposes the value of the annotation in the parameter
  760. <literal>c</literal>.
  761. </para>
  762. </listitem>
  763. </varlistentry>
  764. </variablelist>
  765. <para>The <literal>@annotation</literal> pointcut designator matches any
  766. join point where the <emphasis>subject</emphasis> of the join point has
  767. an annotation of the given type. Like the other @pcds, it can also be
  768. used for context exposure.</para>
  769. <programlisting><![CDATA[
  770. AtAnnotation := '@annotation' '(' AnnotationOrIdentifier ')'
  771. ]]></programlisting>
  772. <para>The subject of a join point is defined in the table in chapter one of
  773. this guide.</para>
  774. <para>
  775. Access to annotation information on members at a matched join point is also available
  776. through the <literal>getSignature</literal> method of the <literal>JoinPoint</literal>
  777. and <literal>JoinPoint.StaticPart</literal> interfaces. The <literal>Signature</literal>
  778. interfaces are extended with additional operations that provide access to the
  779. <literal>java.lang.reflect</literal> <literal>Method, Field</literal> and
  780. <literal>Constructor</literal> objects on which annnotations can be queried. The following fragment
  781. illustrates an example use of this interface to access annotation information.
  782. </para>
  783. <programlisting><![CDATA[
  784. Signature sig = thisJoinPointStaticPart.getSignature();
  785. AnnotatedElement declaringTypeAnnotationInfo = sig.getDeclaringType();
  786. if (sig instanceof MethodSignature) {
  787. // this must be a call or execution join point
  788. Method method = ((MethodSignature)sig).getMethod();
  789. }
  790. ]]></programlisting>
  791. <para>
  792. <emphasis>Note again that it would be nicer to add the method getAnnotationInfo
  793. directly to MemberSignature, but this would once more couple the runtime library
  794. to Java 5.</emphasis>
  795. </para>
  796. <para>
  797. The <literal>@this,@target</literal> and <literal>@args</literal>
  798. pointcut designators can only be used to match against annotations
  799. that have runtime retention. The <literal>@within, @withincode</literal>
  800. and <literal>@annotation</literal> pointcut designators can only be used
  801. to match against annotations that have at least class-file retention, and
  802. if used in the binding form the annotation must have runtime retention.
  803. </para>
  804. </sect2>
  805. <sect2 id="package-and-parameter-annotations" xreflabel="package-and-parameter-annotations">
  806. <title>Package and Parameter Annotations</title>
  807. <para>
  808. <emphasis>Matching on package and parameter annotations is not supported
  809. in AspectJ 1.5.0. Support for this capability may be considered in a future
  810. release.</emphasis>
  811. </para>
  812. <!-- @withinpackage ??? -->
  813. <!--
  814. <para>
  815. Java 5 allows both packages and parameters to be annotated. To allow matching on package and parameter annotations,
  816. AspectJ 5 introduces the <literal>@package</literal> and <literal>@parameters</literal> pointcut designators.
  817. </para>
  818. <programlisting><![CDATA[
  819. PackageAnnotationPointcut := '@package' '(' AnnotationPattern ')'
  820. ]]></programlisting>
  821. <para>The <literal>@package</literal> pointcut matches any join point
  822. occuring within the scope of a package with
  823. annotations matching the giving <literal>AnnotationPattern</literal>. For
  824. example:
  825. </para>
  826. <programlisting><![CDATA[
  827. @package(@Model)
  828. ]]></programlisting>
  829. <para>
  830. Matches any join point occuring within the scope of a package with the
  831. <literal>@Model</literal> annotation.
  832. </para>
  833. <para>
  834. <emphasis>
  835. Note: we added @package as a result of a conscious decision not to allow the
  836. specification of package annotation patterns within a general TypePattern. A
  837. consequence of this decision is that we lose the ability to say the following
  838. things: "a call to a method defined in a type in a package with annotations
  839. matching..." ; "the set of a field defined in a type in a package with annotations
  840. matching..." ; "the get of a field defined in a type in a package with annotations
  841. matching...". As well as the package of the target at these join points, there is
  842. also the package of the runtime type of the target (call/target difference). So
  843. there are at least three possible sets of package annotations you could theoretically
  844. want to match on at a call, get, or set join point. We have chosen to provide the
  845. means to express the simplest of these, and could consider extending the language
  846. to allow for the others in the future when we better understanding how users will
  847. really use both package annotations and these features.
  848. </emphasis>
  849. </para>
  850. <para>
  851. The <literal>@parameter</literal> pointcut designator acts in a similar manner to
  852. <literal>args</literal> in that it matches based on number and position of arguments
  853. at a join point (and supports the same wildcard options of <literal>*</literal> and
  854. <literal>..</literal>.
  855. </para>
  856. <programlisting><![CDATA[
  857. ParamsAnnotationPointcut := '@parameters' '(' ParamsAnnotationPattern ')'
  858. ParamsAnnotationPattern := AnnotationPattern (',' ParamsAnnotationPattern)? |
  859. '*' (',' ParamsAnnotationPattern)? |
  860. '..' (',' SingleParamsAnnotationPattern)*
  861. SingleParamsAnnotationPattern := AnnotationPattern (',' SingleParamsAnnotationPattern)? |
  862. '*' (',' SingleParamsAnnotationPattern)?
  863. ]]></programlisting>
  864. <para>The <literal>*</literal> wildcard matches a single parameter regardless of its annotations.
  865. The <literal>..</literal> wildcard matches zero or more parameters with any annotations. An
  866. annotation pattern in a given parameter position matches a parameter in that position with annotations
  867. matching the given annotation pattern. For example, the method signature</para>
  868. <programlisting><![CDATA[
  869. public void foo(@Immutable int i, String s, @Cached Object o);
  870. ]]></programlisting>
  871. <para>
  872. Is matched by:
  873. </para>
  874. <programlisting><![CDATA[
  875. @parameters(@Immutable, *, @Cached);
  876. and,
  877. @parameters(..,@Cached);
  878. and,
  879. @parameters(@Immutable, *, *);
  880. ]]></programlisting>
  881. <para>
  882. It is not matched by:
  883. </para>
  884. <programlisting><![CDATA[
  885. @parameters(@Immutable, *);
  886. or,
  887. @parameters(*,@Immutable);
  888. or,
  889. @parameters(*, int, @Cached);
  890. ]]></programlisting>
  891. <para>
  892. This last example will result in a compilation error since <literal>int</literal> is not a
  893. valid annotation pattern.
  894. </para>
  895. -->
  896. </sect2>
  897. <sect2 id="annotation-inheritance-and-pointcut-matching" xreflabel="annotation-inheritance-and-pointcut-matching">
  898. <title>Annotation Inheritance and pointcut matching</title>
  899. <para>
  900. According to the Java 5 specification, non-type annotations are not
  901. inherited, and annotations on types are only inherited if they have the
  902. <literal>@Inherited</literal> meta-annotation.
  903. Given the following program:
  904. </para>
  905. <programlisting><![CDATA[
  906. class C1 {
  907. @SomeAnnotation
  908. public void aMethod() {...}
  909. }
  910. class C2 extends C1 {
  911. public void aMethod() {...}
  912. }
  913. class Main {
  914. public static void main(String[] args) {
  915. C1 c1 = new C1();
  916. C2 c2 = new C2();
  917. c1.aMethod();
  918. c2.aMethod();
  919. }
  920. }
  921. aspect X {
  922. pointcut annotatedC2MethodCall() :
  923. call(@SomeAnnotation * C2.aMethod());
  924. pointcut annotatedMethodCall() :
  925. call(@SomeAnnotation * aMethod());
  926. }
  927. ]]></programlisting>
  928. <para>
  929. The pointcut <literal>annotatedC2MethodCall</literal> will not match anything
  930. since the definition of <literal>aMethod</literal> in <literal>C2</literal>
  931. does not have the annotation.
  932. </para>
  933. <para>
  934. The pointcut <literal>annotatedMethodCall</literal> matches
  935. <literal>c1.aMethod()</literal> but not <literal>c2.aMethod()</literal>. The call
  936. to <literal>c2.aMethod</literal> is not matched because join point matching for
  937. modifiers (the visibility modifiers, annotations, and throws clause) is based on
  938. the subject of the join point (the method actually being called).
  939. </para>
  940. </sect2>
  941. <sect2 id="matchingOnAnnotationValues" xreflabel="matchingOnAnnotationValues">
  942. <title>Matching based on annotation values</title>
  943. <para>
  944. The <literal>if</literal> pointcut designator can be used to write pointcuts
  945. that match based on the values annotation members. For example:
  946. </para>
  947. <programlisting><![CDATA[
  948. pointcut txRequiredMethod(Tx transactionAnnotation) :
  949. execution(* *(..)) && @this(transactionAnnotation)
  950. && if(transactionAnnotation.policy() == TxPolicy.REQUIRED);
  951. ]]></programlisting>
  952. </sect2>
  953. </sect1>
  954. <!-- ============================== -->
  955. <sect1 id="annotations-decp">
  956. <title>Using Annotations with declare statements</title>
  957. <sect2 id="declare-error-and-declare-warning" xreflabel="declare-error-and-declare-warning">
  958. <title>Declare error and declare warning</title>
  959. <para>
  960. Since pointcut expressions in AspectJ 5 support join point matching based
  961. on annotations, this facility can be exploited when writing
  962. <literal>declare warning</literal> and <literal>declare error</literal>
  963. statements. For example:
  964. </para>
  965. <programlisting><![CDATA[
  966. declare warning : withincode(@PerformanceCritical * *(..)) &&
  967. call(@ExpensiveOperation * *(..))
  968. : "Expensive operation called from within performance critical section";
  969. ]]></programlisting>
  970. <programlisting><![CDATA[
  971. declare error : call(* org.xyz.model.*.*(..)) &&
  972. !@within(Trusted)
  973. : "Untrusted code should not call the model classes directly";
  974. ]]></programlisting>
  975. </sect2>
  976. <sect2 id="declare-parents" xreflabel="declare-parents">
  977. <title>declare parents</title>
  978. <para>
  979. The general form of a <literal>declare parents</literal> statement is:
  980. </para>
  981. <programlisting><![CDATA[
  982. declare parents : TypePattern extends Type;
  983. declare parents : TypePattern implements TypeList;
  984. ]]></programlisting>
  985. <para>
  986. Since AspectJ 5 supports annotations as part of a type pattern
  987. specification, it is now possible to match types based on the presence
  988. of annotations <emphasis>with either class-file or runtime retention</emphasis>.
  989. For example:
  990. </para>
  991. <variablelist>
  992. <varlistentry>
  993. <term>declare parents : (@Secured *) implements SecuredObject;</term>
  994. <listitem>
  995. <para>
  996. All types with the <literal>@Secured</literal> annotation
  997. implement the <literal>SecuredObject</literal> inteface.
  998. </para>
  999. </listitem>
  1000. </varlistentry>
  1001. <varlistentry>
  1002. <term>declare parents : (@Secured BankAccount+) implements SecuredObject;</term>
  1003. <listitem>
  1004. <para>
  1005. The subset of types drawn from the <literal>BankAccount</literal> type and any subtype of
  1006. <literal>BankAccount</literal>, where the
  1007. <literal>@Secured</literal> annotation is present, implement the
  1008. <literal>SecuredObject</literal> interface.
  1009. </para>
  1010. </listitem>
  1011. </varlistentry>
  1012. </variablelist>
  1013. <para>An annotation type may not be used as the target of a declare parents
  1014. statement. If an annotation type is named explicitly as the target of a
  1015. declare parents statement, a compilation error will result. If an annotation
  1016. type is matched by a non-explicit type pattern used in a declare parents
  1017. statement it will be ignored (and an XLint warning issued).</para>
  1018. </sect2>
  1019. <sect2 id="declare-precedence" xreflabel="declare-precedence">
  1020. <title>declare precedence</title>
  1021. <para>
  1022. The general form of a declare precedence statement is:
  1023. </para>
  1024. <programlisting><![CDATA[
  1025. declare precedence : TypePatList;
  1026. ]]></programlisting>
  1027. <para>
  1028. AspectJ 5 allows the type patterns in the list to include annotation information
  1029. as part of the pattern specification. For example:
  1030. </para>
  1031. <variablelist>
  1032. <varlistentry>
  1033. <term>declare precedence : (@Security *),*;</term>
  1034. <listitem>
  1035. <para>
  1036. All aspects with the <literal>@Security</literal> annotation
  1037. take precedence over any other aspects in the system. (Or, more
  1038. informally, all security-related aspects take precedence).
  1039. </para>
  1040. </listitem>
  1041. </varlistentry>
  1042. </variablelist>
  1043. </sect2>
  1044. </sect1>
  1045. <!-- ============================== -->
  1046. <sect1 id="annotations-declare">
  1047. <title>Declare Annotation</title>
  1048. <para>AspectJ 5 supports a new kind of declare statement, <literal>declare annotation</literal>.
  1049. This takes different forms according to the recipient of the annotation:
  1050. <literal>declare @type</literal> for types, <literal>declare @method</literal> for methods,
  1051. <literal>declare @constructor</literal> for constructors, and <literal>declare @field</literal>
  1052. for fields. <literal>declare @package</literal> may be supported in a future release.
  1053. </para>
  1054. <para>The general form is:</para>
  1055. <programlisting><![CDATA[
  1056. declare @<kind> : ElementPattern : Annotation ;
  1057. ]]></programlisting>
  1058. <para>Where annotation is a regular annotation expression as defined in the Java 5 language. If the annotation has
  1059. the <literal>@Target</literal> meta-annotation, then the elements matched by <literal>ElementPattern</literal>
  1060. must be of the kind specified by the <literal>@Target</literal> annotation.</para>
  1061. <para><literal>ElementPattern</literal> is defined as follows:</para>
  1062. <programlisting><![CDATA[
  1063. ElementPattern := TypePattern |
  1064. MethodPattern |
  1065. ConstructorPattern |
  1066. FieldPattern
  1067. ]]></programlisting>
  1068. <para>The following examples illustrate the use of <literal>declare annotation</literal>.</para>
  1069. <variablelist>
  1070. <varlistentry>
  1071. <term>declare @type : org.xyz.model..* : @BusinessDomain ;</term>
  1072. <listitem>
  1073. <para>
  1074. All types defined in a package with the prefix <literal>org.xyz.model</literal>
  1075. have the <literal>@BusinessDomain</literal> annotation.
  1076. </para>
  1077. </listitem>
  1078. </varlistentry>
  1079. <varlistentry>
  1080. <term>declare @method : public * BankAccount+.*(..) : @Secured(role="supervisor")</term>
  1081. <listitem>
  1082. <para>
  1083. All public methods in <literal>BankAccount</literal> and its subtypes have the
  1084. annotation <literal>@Secured(role="supervisor")</literal>.
  1085. </para>
  1086. </listitem>
  1087. </varlistentry>
  1088. <varlistentry>
  1089. <term>declare @constructor : BankAccount+.new(..) : @Secured(role="supervisor")</term>
  1090. <listitem>
  1091. <para>
  1092. All constructors in <literal>BankAccount</literal> and its subtypes have the
  1093. annotation <literal>@Secured(role="supervisor")</literal>.
  1094. </para>
  1095. </listitem>
  1096. </varlistentry>
  1097. <varlistentry>
  1098. <term>declare @field : * DAO+.* : @Persisted;</term>
  1099. <listitem>
  1100. <para>
  1101. All fields defined in <literal>DAO</literal> or its subtypes have the
  1102. <literal>@Persisted</literal> annotation.
  1103. </para>
  1104. </listitem>
  1105. </varlistentry>
  1106. </variablelist>
  1107. </sect1>
  1108. <sect1 id="annotations-itds">
  1109. <title>Inter-type Declarations</title>
  1110. <para>An annotation type may not be the target of an inter-type declaration.</para>
  1111. </sect1>
  1112. </chapter>