Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  1. <!--<!DOCTYPE chapter SYSTEM "file:/C:/Documents%20and%20Settings/colyer/My%20Documents/projects/aspectjdev/lib/docbook/docbook-dtd/docbookx.dtd">
  2. -->
  3. <chapter id="generics" xreflabel="Generics">
  4. <title>Generics</title>
  5. <sect1 id="generics-inJava5">
  6. <title>Generics in Java 5</title>
  7. <para>
  8. This section provides the essential information about generics in
  9. Java 5 needed to understand how generics are treated in AspectJ 5.
  10. For a full introduction to generics in Java, please see the
  11. documentation for the Java 5 SDK.
  12. </para>
  13. <sect2 id="declaring-generic-types" xreflabel="declaring-generic-types">
  14. <title>Declaring Generic Types</title>
  15. <para>
  16. A generic type is declared with one or more type parameters following the type name.
  17. By convention formal type parameters are named using a single letter, though this is not required.
  18. A simple generic list type
  19. (that can contain elements of any type <literal>E</literal>) could be declared:
  20. </para>
  21. <programlisting><![CDATA[
  22. interface List<E> {
  23. Iterator<E> iterator();
  24. void add(E anItem);
  25. E remove(E anItem);
  26. }
  27. ]]></programlisting>
  28. <para>
  29. It is important to understand that unlike template mechanisms there will only be one type, and one class file, corresponding to
  30. the <literal>List</literal> interface, regardless of how many different instantiations of the <literal>List</literal> interface a program
  31. has (each potentially providing a different value for the type parameter <literal>E</literal>). A consequence of this
  32. is that you cannot refer to the type parameters of a type declaration in a static method or initializer, or in the declaration or
  33. initializer of a static variable.
  34. </para>
  35. <para>
  36. A <emphasis>parameterized type</emphasis>
  37. is an invocation of a generic type with concrete values supplied for
  38. all of its type parameters (for example, <literal>List&lt;String></literal> or <literal>List&lt;Food></literal>).
  39. </para>
  40. <para>A generic type may be declared with multiple type parameters. In addition to simple type parameter names, type
  41. parameter declarations can also constrain the set of types allowed by using the <literal>extends</literal>
  42. keyword. Some examples follow:</para>
  43. <variablelist>
  44. <varlistentry>
  45. <term>class Foo&lt;T> {...}</term>
  46. <listitem>
  47. <para>A class <literal>Foo</literal> with one type parameter, <literal>T</literal>.
  48. </para>
  49. </listitem>
  50. </varlistentry>
  51. <varlistentry>
  52. <term>class Foo&lt;T,S> {...}</term>
  53. <listitem>
  54. <para>A class <literal>Foo</literal> with two type parameters, <literal>T</literal> and <literal>S</literal>.
  55. </para>
  56. </listitem>
  57. </varlistentry>
  58. <varlistentry>
  59. <term>class Foo&lt;T extends Number> {...}</term>
  60. <listitem>
  61. <para>A class <literal>Foo</literal> with one type parameter <literal>T</literal>, where <literal>T</literal> must be
  62. instantiated as the type <literal>Number</literal> or a subtype of <literal>Number</literal>.
  63. </para>
  64. </listitem>
  65. </varlistentry>
  66. <varlistentry>
  67. <term>class Foo&lt;T, S extends T> {...}</term>
  68. <listitem>
  69. <para>A class <literal>Foo</literal> with two type parameters, <literal>T</literal> and <literal>S</literal>. <literal>Foo</literal>
  70. must be instantiated with a type <literal>S</literal> that is a subtype of the type specified for parameter <literal>T</literal>.
  71. </para>
  72. </listitem>
  73. </varlistentry>
  74. <varlistentry>
  75. <term>class Foo&lt;T extends Number &amp; Comparable> {...}</term>
  76. <listitem>
  77. <para>A class <literal>Foo</literal> with one type parameter, <literal>T</literal>. <literal>Foo</literal>
  78. must be instantiated with a type that is a subtype of <literal>Number</literal> and that implements <literal>Comparable</literal>.
  79. </para>
  80. </listitem>
  81. </varlistentry>
  82. </variablelist>
  83. </sect2>
  84. <sect2 id="using-generic-and-parameterized-types" xreflabel="using-generic-and-parameterized-types">
  85. <title>Using Generic and Parameterized Types</title>
  86. <para>You declare a variable (or a method/constructor argument) of a parameterized type by specifying a concrete type specfication for each type parameter in
  87. the generic type. The following example declares a list of strings and a list of numbers:</para>
  88. <programlisting><![CDATA[
  89. List<String> strings;
  90. List<Number> numbers;
  91. ]]></programlisting>
  92. <para>It is also possible to declare a variable of a generic type without specifying any values for the type
  93. parameters (a <emphasis>raw</emphasis> type). For example, <literal>List strings</literal>.
  94. In this case, unchecked warnings may be issued by the compiler
  95. when the referenced object is passed as a parameter to a method expecting a parameterized type such as a
  96. <literal>List&lt;String></literal>. New code written in the Java 5 language would not be expected to use
  97. raw types.</para>
  98. <para>Parameterized types are instantiated by specifying type parameter values in the constructor call expression as in
  99. the following examples:</para>
  100. <programlisting><![CDATA[
  101. List<String> strings = new MyListImpl<String>();
  102. List<Number> numbers = new MyListImpl<Number>();
  103. ]]></programlisting>
  104. <para>
  105. When declaring parameterized types, the <literal>?</literal> wildcard may be used, which stands for "some type".
  106. The <literal>extends</literal> and <literal>super</literal> keywords may be used in conjunction with the wildcard
  107. to provide upper and lower bounds on the types that may satisfy the type constraints. For example:
  108. </para>
  109. <variablelist>
  110. <varlistentry>
  111. <term>List&lt;?&gt;</term>
  112. <listitem>
  113. <para>A list containing elements of some type, the type of the elements in the list is unknown.
  114. </para>
  115. </listitem>
  116. </varlistentry>
  117. <varlistentry>
  118. <term>List&lt;? extends Number&gt;</term>
  119. <listitem>
  120. <para>A list containing elements of some type that extends Number, the exact type of the elements in the list is unknown.
  121. </para>
  122. </listitem>
  123. </varlistentry>
  124. <varlistentry>
  125. <term>List&lt;? super Double&gt;</term>
  126. <listitem>
  127. <para>A list containing elements of some type that is a super-type of Double, the exact type of the elements in the list is unknown.
  128. </para>
  129. </listitem>
  130. </varlistentry>
  131. </variablelist>
  132. <para>
  133. A generic type may be extended as any other type. Given a generic type <literal>Foo&lt;T&gt;</literal> then
  134. a subtype <literal>Goo</literal> may be declared in one of the following ways:
  135. </para>
  136. <variablelist>
  137. <varlistentry>
  138. <term>class Goo extends Foo</term>
  139. <listitem>
  140. <para>Here <literal>Foo</literal> is used as a raw type, and the appropriate warning messages will be
  141. issued by the compiler on attempting to invoke methods in <literal>Foo</literal>.
  142. </para>
  143. </listitem>
  144. </varlistentry>
  145. <varlistentry>
  146. <term>class Goo&lt;E&gt; extends Foo</term>
  147. <listitem>
  148. <para><literal>Goo</literal> is a generic type, but the super-type <literal>Foo</literal> is used as a raw
  149. type and the appropriate warning messages will be
  150. issued by the compiler on attempting to invoke methods defined by <literal>Foo</literal>.
  151. </para>
  152. </listitem>
  153. </varlistentry>
  154. <varlistentry>
  155. <term>class Goo&lt;E&gt; extends Foo&lt;E&gt;</term>
  156. <listitem>
  157. <para>This is the most usual form. <literal>Goo</literal> is a generic type with one parameter that extends
  158. the generic type <literal>Foo</literal> with that same parameter. So <literal>Goo&lt;String&lt;</literal> is
  159. a subclass of <literal>Foo&lt;String&gt;</literal>.
  160. </para>
  161. </listitem>
  162. </varlistentry>
  163. <varlistentry>
  164. <term>class Goo&lt;E,F&gt; extends Foo&lt;E&gt;</term>
  165. <listitem>
  166. <para><literal>Goo</literal> is a generic type with two parameters that extends
  167. the generic type <literal>Foo</literal> with the first type parameter of <literal>Goo</literal> being used
  168. to parameterize <literal>Foo</literal>. So <literal>Goo&lt;String,Integer&lt;</literal> is
  169. a subclass of <literal>Foo&lt;String&gt;</literal>.
  170. </para>
  171. </listitem>
  172. </varlistentry>
  173. <varlistentry>
  174. <term>class Goo extends Foo&lt;String&gt;</term>
  175. <listitem>
  176. <para><literal>Goo</literal> is a type that extends
  177. the parameterized type <literal>Foo&lt;String&gt;</literal>.
  178. </para>
  179. </listitem>
  180. </varlistentry>
  181. </variablelist>
  182. <para>A generic type may implement one or more generic interfaces, following the type binding
  183. rules given above. A type may also implement one or more parameterized interfaces (for example,
  184. <literal>class X implements List&lt;String&gt;</literal>, however a type may not at the same time
  185. be a subtype of two interface types which are different parameterizations of the same interface.</para>
  186. </sect2>
  187. <sect2 id="subtypes-supertypes-and-assignability" xreflabel="subtypes-supertypes-and-assignability">
  188. <title>Subtypes, Supertypes, and Assignability</title>
  189. <para>
  190. The supertype of a generic type <literal>C</literal> is the type given in the extends clause of
  191. <literal>C</literal>, or <literal>Object</literal> if no extends clause is present. Given the type declaration
  192. </para>
  193. <programlisting><![CDATA[
  194. public interface List<E> extends Collection<E> {... }
  195. ]]></programlisting>
  196. <para>
  197. then the supertype of <literal>List&lt;E&gt;</literal> is <literal>Collection&lt;E&gt;</literal>.
  198. </para>
  199. <para>
  200. The supertype of a parameterized type <literal>P</literal> is the type given in the extends clause of
  201. <literal>P</literal>, or <literal>Object</literal> if no extends clause is present. Any type parameters in
  202. the supertype are substituted in accordance with the parameterization of <literal>P</literal>. An example
  203. will make this much clearer: Given the type <literal>List&lt;Double&gt;</literal> and the definition of
  204. the <literal>List</literal> given above, the direct supertype is
  205. <literal>Collection&lt;Double&gt;</literal>. <literal>List&lt;Double&gt;</literal> is <emphasis>not</emphasis>
  206. considered to be a subtype of <literal>List&lt;Number&gt;</literal>.
  207. </para>
  208. <para>
  209. An instance of a parameterized type <literal>P&lt;T1,T2,...Tn&gt;</literal>may be assigned to a variable of
  210. the same type or a supertype
  211. without casting. In addition it may be assigned to a variable <literal>R&lt;S1,S2,...Sm&gt;</literal> where
  212. <literal>R</literal> is a supertype of <literal>P</literal> (the supertype relationship is reflexive),
  213. <literal>m &lt;= n</literal>, and for all type parameters <literal>S1..m</literal>, <literal>Tm</literal> equals
  214. <literal>Sm</literal> <emphasis>or</emphasis> <literal>Sm</literal> is a wildcard type specification and
  215. <literal>Tm</literal> falls within the bounds of the wildcard. For example, <literal>List&lt;String&gt;</literal>
  216. can be assigned to a variable of type <literal>Collection&lt;?&gt;</literal>, and <literal>List&lt;Double&gt;</literal>
  217. can be assigned to a variable of type <literal>List&lt;? extends Number&gt;</literal>.
  218. </para>
  219. </sect2>
  220. <sect2 id="generic-methods-and-constructors" xreflabel="generic-methods-and-constructors">
  221. <title>Generic Methods and Constructors</title>
  222. <para>
  223. A static method may be declared with one or more type parameters as in the following declaration:
  224. </para>
  225. <programlisting><![CDATA[
  226. static <T> T first(List<T> ts) { ... }
  227. ]]></programlisting>
  228. <para>
  229. Such a definition can appear in any type, the type parameter <literal>T</literal> does not need to
  230. be declared as a type parameter of the enclosing type.
  231. </para>
  232. <para>
  233. Non-static methods may also be declared with one or more type parameters in a similar fashion:
  234. </para>
  235. <programlisting><![CDATA[
  236. <T extends Number> T max(T t1, T t2) { ... }
  237. ]]></programlisting>
  238. <para>The same technique can be used to declare a generic constructor.</para>
  239. </sect2>
  240. <sect2 id="erasure" xreflabel="erasure">
  241. <title>Erasure</title>
  242. <para>Generics in Java are implemented using a technique called <emphasis>erasure</emphasis>. All
  243. type parameter information is erased from the run-time type system. Asking an object of a parameterized
  244. type for its class will return the class object for the raw type (eg. <literal>List</literal> for an object
  245. declared to be of type <literal>List&lt;String&gt;</literal>. A consequence of this is that you cannot at
  246. runtime ask if an object is an <literal>instanceof</literal> a parameterized type.</para>
  247. </sect2>
  248. </sect1>
  249. <!-- ===================================================================== -->
  250. <sect1 id="generics-inAspectJ5">
  251. <title>Generics in AspectJ 5</title>
  252. <para>
  253. AspectJ 5 provides full support for all of the Java 5 language features, including generics. Any legal Java 5 program is a
  254. legal AspectJ 5 progam. In addition, AspectJ 5 provides support for generic and parameterized types in pointcuts, inter-type
  255. declarations, and declare statements. Parameterized types may freely be used within aspect members, and support is
  256. also provided for generic <emphasis>abstract</emphasis> aspects.
  257. </para>
  258. <sect2 id="matching-generic-and-parameterized-types-in-pointcut-expressions" xreflabel="matching-generic-and-parameterized-types-in-pointcut-expressions">
  259. <title>Matching generic and parameterized types in pointcut expressions</title>
  260. <para>
  261. The simplest way to work with generic and parameterized types in pointcut expressions and type patterns
  262. is simply to use the raw type name. For example, the type pattern <literal>List</literal> will match
  263. the generic type <literal>List&lt;E&gt;</literal> and any parameterization of that type
  264. (<literal>List&lt;String&gt;, List&lt;?&gt;, List&lt;? extends Number&gt;</literal> and so on. This
  265. ensures that pointcuts written in existing code that is not generics-aware will continue to work as
  266. expected in AspectJ 5. It is also the recommended way to match against generic and parameterized types
  267. in AspectJ 5 unless you explicitly wish to narrow matches to certain parameterizations of a generic type.
  268. </para>
  269. <para>Generic methods and constructors, and members defined in generic types, may use type variables
  270. as part of their signature. For example:</para>
  271. <programlisting><![CDATA[
  272. public class Utils {
  273. /** static generic method */
  274. static <T> T first(List<T> ts) { ... }
  275. /** instance generic method */
  276. <T extends Number> T max(T t1, T t2) { ... }
  277. }
  278. public class G<T> {
  279. // field with parameterized type
  280. T myData;
  281. // method with parameterized return type
  282. public List<T> getAllDataItems() {...}
  283. }
  284. ]]></programlisting>
  285. <para>
  286. AspectJ 5 does not allow the use of type variables in pointcut expressions and type patterns. Instead, members that
  287. use type parameters as part of their signature are matched by their <emphasis>erasure</emphasis>. Java 5 defines the
  288. rules for determing the erasure of a type as follows.
  289. </para>
  290. <para>Let <literal>|T|</literal> represent the erasure of some type <literal>T</literal>. Then:</para>
  291. <simplelist>
  292. <member>The erasure of a parameterized type <literal>T&lt;T1,...,Tn&gt;</literal> is <literal>|T|</literal>.
  293. For example, the erasure of <literal>List&lt;String&gt;</literal> is <literal>List</literal>.</member>
  294. <member>The erasure of a nested type <literal>T.C</literal> is <literal>|T|.C</literal>. For example,
  295. the erasure of the nested type <literal>Foo&lt;T&gt;.Bar</literal> is <literal>Foo.Bar</literal>.</member>
  296. <member>The erasure of an array type <literal>T[]</literal> is <literal>|T|[]</literal>. For example,
  297. the erasure of <literal>List&lt;String&gt;[]</literal> is <literal>List[]</literal>.</member>
  298. <member>The erasure of a type variable is its leftmost bound. For example, the erasure of a
  299. type variable <literal>P</literal> is <literal>Object</literal>, and the erasure of a type
  300. variable <literal>N extends Number</literal> is <literal>Number</literal>.</member>
  301. <member>The erasure of every other type is the type itself</member>
  302. </simplelist>
  303. <!-- see tests/java5/generics/ajdk/ErasureMatching.aj -->
  304. <para>Applying these rules to the earlier examples, we find that the methods defined in <literal>Utils</literal>
  305. can be matched by a signature pattern matching <literal>static Object Utils.first(List)</literal> and
  306. <literal>Number Utils.max(Number, Number)</literal> respectively. The members of the generic type
  307. <literal>G</literal> can be matched by a signature pattern matching <literal>Object G.myData</literal> and
  308. <literal>public List G.getAllDataItems()</literal> respectively.</para>
  309. <sect3>
  310. <title>Restricting matching using parameterized types</title>
  311. <para>Pointcut matching can be further restricted to match only given parameterizations of parameter types (methods and constructors), return
  312. types (methods) and field types (fields). This is achieved by specifying a parameterized type pattern at the appropriate point
  313. in the signature pattern. For example, given the class <literal>Foo</literal>:</para>
  314. <programlisting><![CDATA[
  315. public class Foo {
  316. List<String> myStrings;
  317. List<Float> myFloats;
  318. public List<String> getStrings() { return myStrings; }
  319. public List<Float> getFloats() { return myFloats; }
  320. public void addStrings(List<String> evenMoreStrings) {
  321. myStrings.addAll(evenMoreStrings);
  322. }
  323. }
  324. ]]></programlisting>
  325. <!-- see tests/java5/generics/ajdk/SimpleParameterizedTypeExamples.aj -->
  326. <para>Then a <literal>get</literal> join point for the field <literal>myStrings</literal> can be matched by the
  327. pointcut <literal>get(List Foo.myStrings)</literal> and by the pointcut <literal>get(List&lt;String&gt; Foo.myStrings)</literal>,
  328. but <emphasis>not</emphasis> by the pointcut <literal>get(List&lt;Number&gt; *)</literal>.</para>
  329. <para>A <literal>get</literal> join point for the field <literal>myFloats</literal> can be matched by the
  330. pointcut <literal>get(List Foo.myFloats)</literal>, the pointcut <literal>get(List&lt;Float&gt; *)</literal>,
  331. and the pointcut <literal>get(List&lt;Number+&gt; *)</literal>. This last example shows how AspectJ type
  332. patterns can be used to match type parameters types just like any other type. The pointcut
  333. <literal>get(List&lt;Double&gt; *)</literal> does <emphasis>not</emphasis> match.</para>
  334. <para>The execution of the methods <literal>getStrings</literal> and <literal>getFloats</literal> can be
  335. matched by the pointcut expression <literal>execution(List get*(..))</literal>, and the pointcut
  336. expression <literal>execution(List&lt;*&gt; get*(..))</literal>, but only <literal>getStrings</literal>
  337. is matched by <literal>execution(List&lt;String&gt; get*(..))</literal> and only <literal>getFloats</literal>
  338. is matched by <literal>execution(List&lt;Number+&gt; get*(..))</literal></para>
  339. <para>A call to the method <literal>addStrings</literal> can be matched by the pointcut expression
  340. <literal>call(* addStrings(List))</literal> and by the expression <literal>call(* addStrings(List&lt;String&gt;))</literal>,
  341. but <emphasis>not</emphasis> by the expression <literal>call(* addStrings(List&lt;Number&gt;))</literal>.
  342. </para>
  343. <para>Remember that any type variable reference in a generic member is
  344. <emphasis>always</emphasis> matched by its erasure. Thus given the following
  345. example:</para>
  346. <programlisting><![CDATA[
  347. class G<T> {
  348. List<T> foo(List<String> ls) { return null; }
  349. }
  350. ]]></programlisting>
  351. <!-- see tests/java5/generics/ajdk/MixedParameterizedAndTypeVariables.aj -->
  352. <para>The execution of <literal>foo</literal> can be matched by
  353. <literal>execution(List foo(List))</literal>,
  354. <literal>execution(List foo(List&lt;String&gt;>))</literal>, and
  355. <literal>execution(* foo(List&lt;String&lt;))</literal>but
  356. <emphasis>not</emphasis> by <literal>execution(List&lt;Object&gt; foo(List&lt;String&gt;>)</literal>
  357. since the erasure of <literal>List&lt;T&gt;</literal> is <literal>List</literal>
  358. and not <literal>List&lt;Object&gt;</literal>.
  359. </para>
  360. </sect3>
  361. <sect3>
  362. <title>Generic wildcards and signature matching</title>
  363. <para>
  364. When it comes to signature matching, a type parameterized using a generic wildcard is a distinct type.
  365. For example, <literal>List&lt;?&gt;</literal> is a very different type to <literal>List&lt;String&gt;</literal>,
  366. even though a variable of type <literal>List&lt;String&gt;</literal> can be assigned to a variable of
  367. type <literal>List&lt;?&gt;</literal>. Given the methods:
  368. </para>
  369. <programlisting><![CDATA[
  370. class C {
  371. public void foo(List<? extends Number> listOfSomeNumberType) {}
  372. public void bar(List<?> listOfSomeType) {}
  373. public void goo(List<Double> listOfDoubles) {}
  374. }
  375. ]]></programlisting>
  376. <!-- see java5/generics/ajdk/SignatureWildcards.aj -->
  377. <variablelist>
  378. <varlistentry>
  379. <term>execution(* C.*(List))</term>
  380. <listitem>
  381. <para>Matches an execution join point for any of the three methods.
  382. </para>
  383. </listitem>
  384. </varlistentry>
  385. <varlistentry>
  386. <term>execution(* C.*(List&lt;? extends Number&gt;))</term>
  387. <listitem>
  388. <para>matches only the
  389. execution of <literal>foo</literal>, and <emphasis>not</emphasis> the execution
  390. of <literal>goo</literal> since <literal>List&lt;? extends Number&gt;</literal> and
  391. <literal>List&lt;Double&gt;</literal> are distinct types.
  392. </para>
  393. </listitem>
  394. </varlistentry>
  395. <varlistentry>
  396. <term>execution(* C.*(List&lt;?&gt;))</term>
  397. <listitem>
  398. <para>matches only the execution of <literal>bar</literal>.
  399. </para>
  400. </listitem>
  401. </varlistentry>
  402. <varlistentry>
  403. <term>execution(* C.*(List&lt;? extends Object+&gt;))</term>
  404. <listitem>
  405. <para>matches both the execution of <literal>foo</literal> and the execution of <literal>bar</literal>
  406. since the upper bound of <literal>List&lt;?&gt;</literal> is implicitly <literal>Object</literal>.
  407. </para>
  408. </listitem>
  409. </varlistentry>
  410. </variablelist>
  411. </sect3>
  412. <sect3>
  413. <title>Treatment of bridge methods</title>
  414. <para>Under certain circumstances a Java 5 compiler is required to create <emphasis>bridge
  415. methods</emphasis> that support the compilation of programs using raw types. Consider the types</para>
  416. <programlisting><![CDATA[
  417. class Generic<T> {
  418. public T foo(T someObject) {
  419. return someObject;
  420. }
  421. }
  422. class SubGeneric<N extends Number> extends Generic<N> {
  423. public N foo(N someNumber) {
  424. return someNumber;
  425. }
  426. }
  427. ]]></programlisting>
  428. <para>The class <literal>SubGeneric</literal> extends <literal>Generic</literal>
  429. and overrides the method <literal>foo</literal>. Since the upper bound of the type variable
  430. <literal>N</literal> in <literal>SubGeneric</literal> is different to the upper bound of
  431. the type variable <literal>T</literal> in <literal>Generic</literal>, the method <literal>foo</literal>
  432. in <literal>SubGeneric</literal> has a different erasure to the method <literal>foo</literal>
  433. in <literal>Generic</literal>. This is an example of a case where a Java 5 compiler will create
  434. a <emphasis>bridge method</emphasis> in <literal>SubGeneric</literal>. Although you never see it,
  435. the bridge method will look something like this:</para>
  436. <programlisting><![CDATA[
  437. public Object foo(Object arg) {
  438. Number n = (Number) arg; // "bridge" to the signature defined in this type
  439. return foo(n);
  440. }
  441. ]]></programlisting>
  442. <!-- see java5/generics/ajdk/BridgeMethodExamples.aj -->
  443. <para>Bridge methods are synthetic artefacts generated as a result of a particular compilation strategy and
  444. have no execution join points in AspectJ 5. So the pointcut <literal>execution(Object SubGeneric.foo(Object))</literal>
  445. does not match anything. (The pointcut <literal>execution(Object Generic.foo(Object))</literal> matches the
  446. execution of <literal>foo</literal> in both <literal>Generic</literal> and <literal>SubGeneric</literal> since
  447. both are implementations of <literal>Generic.foo</literal>).
  448. </para>
  449. <para>It <emphasis>is</emphasis> possible to <emphasis>call</emphasis> a bridge method as the following short
  450. code snippet demonstrates. Such a call <emphasis>does</emphasis> result in a call join point for the call to
  451. the method.
  452. </para>
  453. <programlisting><![CDATA[
  454. SubGeneric rawType = new SubGeneric();
  455. rawType.foo("hi"); // call to bridge method (will result in a runtime failure in this case)
  456. Object n = new Integer(5);
  457. rawType.foo(n); // call to bridge method that would succeed at runtime
  458. ]]></programlisting>
  459. </sect3>
  460. <sect3>
  461. <title>Runtime type matching with this(), target() and args()</title>
  462. <para>The <literal>this()</literal>, <literal>target()</literal>, and
  463. <literal>args()</literal> pointcut expressions all match based on the runtime
  464. type of their arguments. Because Java 5 implements generics using erasure, it is not
  465. possible to ask at runtime whether an object is an instance of a given parameterization of a type
  466. (only whether or not it is an instance of the erasure of that parameterized type). Therefore
  467. AspectJ 5 does not support the use of parameterized types with the <literal>this()</literal> and
  468. <literal>target()</literal> pointcuts. Parameterized types may however be used in conjunction with
  469. <literal>args()</literal>. Consider the following class
  470. </para>
  471. <programlisting><![CDATA[
  472. public class C {
  473. public void foo(List<String> listOfStrings) {}
  474. public void bar(List<Double> listOfDoubles) {}
  475. public void goo(List<? extends Number> listOfSomeNumberType) {}
  476. }
  477. ]]></programlisting>
  478. <!-- see java5/generics/ajdk/ArgsExamples.aj -->
  479. <variablelist>
  480. <varlistentry>
  481. <term>args(List)</term>
  482. <listitem>
  483. <para>will match an execution or call join point for any of
  484. these methods
  485. </para>
  486. </listitem>
  487. </varlistentry>
  488. <varlistentry>
  489. <term>args(List&lt;String&gt;)</term>
  490. <listitem>
  491. <para>will match an execution
  492. or call join point for <literal>foo</literal>.
  493. </para>
  494. </listitem>
  495. </varlistentry>
  496. <varlistentry>
  497. <term>args(List&lt;Double&gt;)</term>
  498. <listitem>
  499. <para>matches an execution or call join point for <literal>bar</literal>, and <emphasis>may</emphasis> match
  500. at an execution or call join point for <literal>goo</literal> since it is legitimate to pass an
  501. object of type <literal>List&lt;Double&gt;</literal> to a method expecting a <literal>List&lt;? extends Number&gt;</literal>.
  502. </para>
  503. <para>
  504. In this situation a runtime test would normally be applied to ascertain whether or not the argument
  505. was indeed an instance of the required type. However, in the case of parameterized types such a test is not
  506. possible and therefore AspectJ 5 considers this a match, but issues an <emphasis>unchecked</emphasis> warning.
  507. For example, compiling the aspect <literal>A</literal> below with the class <literal>C</literal> produces the
  508. compilation warning: "unchecked match of List&lt;Double&gt; with List&lt;? extends Number&gt; when argument is
  509. an instance of List at join point method-execution(void C.goo(List&lt;? extends Number&gt;)) [Xlint:uncheckedArgument]";
  510. </para>
  511. </listitem>
  512. </varlistentry>
  513. </variablelist>
  514. <programlisting><![CDATA[
  515. public aspect A {
  516. before(List<Double> listOfDoubles) : execution(* C.*(..)) && args(listOfDoubles) {
  517. for (Double d : listOfDoubles) {
  518. // do something
  519. }
  520. }
  521. }
  522. ]]></programlisting>
  523. <para>Like all Lint messages, the <literal>uncheckedArgument</literal> warning can be
  524. configured in severity from the default warning level to error or even ignore if preferred.
  525. In addition, AspectJ 5 offers the annotation <literal>@SuppressAjWarnings</literal> which is
  526. the AspectJ equivalent of Java's <literal>@SuppressWarnings</literal> annotation. If the
  527. advice is annotated with <literal>@SuppressWarnings</literal> then <emphasis>all</emphasis>
  528. lint warnings issued during matching of pointcut associated with the advice will be
  529. suppressed. To suppress just an <literal>uncheckedArgument</literal> warning, use the
  530. annotation <literal>@SuppressWarnings("uncheckedArgument")</literal> as in the following
  531. examples:
  532. </para>
  533. <programlisting><![CDATA[
  534. import org.aspectj.lang.annotation.SuppressAjWarnings
  535. public aspect A {
  536. @SuppressAjWarnings // will not see *any* lint warnings for this advice
  537. before(List<Double> listOfDoubles) : execution(* C.*(..)) && args(listOfDoubles) {
  538. for (Double d : listOfDoubles) {
  539. // do something
  540. }
  541. }
  542. @SuppressAjWarnings("uncheckedArgument") // will not see *any* lint warnings for this advice
  543. before(List<Double> listOfDoubles) : execution(* C.*(..)) && args(listOfDoubles) {
  544. for (Double d : listOfDoubles) {
  545. // do something
  546. }
  547. }
  548. }
  549. ]]></programlisting>
  550. <para>
  551. The safest way to deal with <literal>uncheckedArgument</literal> warnings however is to restrict the pointcut
  552. to match only at those join points where the argument is guaranteed to match. This is achieved by combining
  553. <literal>args</literal> with a <literal>call</literal> or <literal>execution</literal> signature matching
  554. pointcut. In the following example the advice will match the execution of <literal>bar</literal> but not
  555. of <literal>goo</literal> since the signature of <literal>goo</literal> is not matched by the execution pointcut
  556. expression.
  557. </para>
  558. <programlisting><![CDATA[
  559. public aspect A {
  560. before(List<Double> listOfDoubles) : execution(* C.*(List<Double>)) && args(listOfDoubles) {
  561. for (Double d : listOfDoubles) {
  562. // do something
  563. }
  564. }
  565. }
  566. ]]></programlisting>
  567. <para>Generic wildcards can be used in args type patterns, and matching follows regular Java 5 assignability rules. For
  568. example, <literal>args(List&lt;?&gt;)</literal> will match a list argument of any type, and
  569. <literal>args(List&lt;? extends Number&gt;)</literal> will match an argument of type
  570. <literal>List&lt;Number&gt;, List&lt;Double&gt;, List&lt;Float&gt;</literal> and so on. Where a match cannot be
  571. fully statically determined, the compiler will once more issue an <literal>uncheckedArgument</literal> warning.
  572. </para>
  573. <para>Consider the following program:</para>
  574. <programlisting><![CDATA[
  575. public class C {
  576. public static void main(String[] args) {
  577. C c = new C();
  578. List<String> ls = new ArrayList<String>();
  579. List<Double> ld = new ArrayList<Double>();
  580. c.foo("hi");
  581. c.foo(ls);
  582. c.foo(ld);
  583. }
  584. public void foo(Object anObject) {}
  585. }
  586. aspect A {
  587. before(List<? extends Number> aListOfSomeNumberType)
  588. : call(* foo(..)) && args(aListOfSomeNumberType) {
  589. // process list...
  590. }
  591. }
  592. ]]></programlisting>
  593. <!-- see java5/generics/ajdk/WildcardArgsExamples.aj -->
  594. <para>From the signature of <literal>foo</literal> all we know is that the runtime argument will be an instance of
  595. <literal>Object</literal>.Compiling this program gives the unchecked argument warning:
  596. "unchecked match of List&lt;? extends Number&gt; with List when argument is
  597. an instance of List at join point method-execution(void C.foo(Object)) [Xlint:uncheckedArgument]".
  598. The advice will not execute at the call join point for <literal>c.foo("hi")</literal> since <literal>String</literal>
  599. is not an instance of <literal>List</literal>. The advice <emphasis>will</emphasis> execute at the call join points
  600. for <literal>c.foo(ls)</literal> and <literal>c.foo(ld)</literal> since in both cases the argument is an instance of
  601. <literal>List</literal>.
  602. </para>
  603. <para>Combine a wildcard argument type with a signature pattern to avoid unchecked argument matches. In the example
  604. below we use the signature pattern <literal>List&lt;Number+&gt;</literal> to match a call to any method taking
  605. a <literal>List&lt;Number&gt;, List&lt;Double&gt;, List&lt;Float&gt;</literal> and so on. In addition the
  606. signature pattern <literal>List&lt;? extends Number+&gt;</literal> can be used to match a call to a method
  607. declared to take a <literal>List&lt;? extends Number&gt;</literal>, <literal>List&lt;? extends Double&gt;</literal>
  608. and so on. Taken together, these restrict matching to only
  609. those join points at which the argument is guaranteed to be an instance of <literal>List&lt;? extends Number&gt;</literal>.</para>
  610. <programlisting><![CDATA[
  611. aspect A {
  612. before(List<? extends Number> aListOfSomeNumberType)
  613. : (call(* foo(List<Number+>)) || call(* foo(List<? extends Number+>)))
  614. && args(aListOfSomeNumberType) {
  615. // process list...
  616. }
  617. }
  618. ]]></programlisting>
  619. </sect3>
  620. <sect3>
  621. <title>Binding return values in after returning advice</title>
  622. <para>
  623. After returning advice can be used to bind the return value from a matched join point. AspectJ 5 supports the use of
  624. a parameterized type in the returning clause, with matching following the same rules as described for args. For
  625. example, the following aspect matches the execution of any method returning a <literal>List</literal>, and makes
  626. the returned list available to the body of the advice.
  627. </para>
  628. <programlisting><![CDATA[
  629. public aspect A {
  630. pointcut executionOfAnyMethodReturningAList() : execution(List *(..));
  631. after() returning(List<?> listOfSomeType) : executionOfAnyMethodReturningAList() {
  632. for (Object element : listOfSomeType) {
  633. // process element...
  634. }
  635. }
  636. }
  637. ]]></programlisting>
  638. <!-- see java5/generics/ajdk/AfterReturningExamples.aj -->
  639. <para>The pointcut uses the raw type pattern <literal>List</literal>, and hence it
  640. matches methods returning any kind of list (<literal>List&lt;String&gt;, List&lt;Double&gt;</literal>,
  641. and so on). We've chosen to bind the returned list as the parameterized type
  642. <literal>List&lt;?&gt;</literal> in the advice since Java's type checking will now ensure
  643. that we only perform safe operations on the list.</para>
  644. <para>Given the class</para>
  645. <programlisting><![CDATA[
  646. public class C {
  647. public List<String> foo(List<String> listOfStrings) {...}
  648. public List<Double> bar(List<Double> listOfDoubles) {...}
  649. public List<? extends Number> goo(List<? extends Number> listOfSomeNumberType) {...}
  650. }
  651. ]]></programlisting>
  652. <para>The advice in the aspect below will run after the execution of <literal>bar</literal>
  653. and bind the return value. It will also run after the execution of <literal>goo</literal> and
  654. bind the return value, but gives an <literal>uncheckedArgument</literal> warning during
  655. compilation. It does <emphasis>not</emphasis> run after the execution of <literal>foo</literal>.
  656. </para>
  657. <programlisting><![CDATA[
  658. public aspect Returning {
  659. after() returning(List<Double> listOfDoubles) : execution(* C.*(..)) {
  660. for(Double d : listOfDoubles) {
  661. // process double...
  662. }
  663. }
  664. }
  665. ]]></programlisting>
  666. <para>As with <literal>args</literal> you can guarantee that after returning advice only
  667. executes on lists <emphasis>statically determinable</emphasis> to be of the right
  668. type by specifying a return type pattern in the associated pointcut. The
  669. <literal>@SuppressAjWarnings</literal> annotation can also be used if desired.</para>
  670. </sect3>
  671. <sect3>
  672. <title>Declaring pointcuts inside generic types</title>
  673. <para>Pointcuts can be declared in both classes and aspects. A pointcut declared in a generic
  674. type may use the type variables of the type in which it is declared. All references to
  675. a pointcut declared in a generic type from outside of that type must be via a parameterized type reference,
  676. and not a raw type reference.</para>
  677. <para>Consider the generic type <literal>Generic</literal> with a pointcut <literal>foo</literal>:
  678. </para>
  679. <programlisting><![CDATA[
  680. public class Generic<T> {
  681. /**
  682. * matches the execution of any implementation of a method defined for T
  683. */
  684. public pointcut foo() : execution(* T.*(..));
  685. }
  686. ]]></programlisting>
  687. <!-- see java5/generics/ajdk/PointcutInGenericClassExample.aj -->
  688. <para>Such a pointcut must be refered to using a parameterized reference as shown
  689. below.</para>
  690. <programlisting><![CDATA[
  691. public aspect A {
  692. // runs before the execution of any implementation of a method defined for MyClass
  693. before() : Generic<MyClass>.foo() {
  694. // ...
  695. }
  696. // runs before the execution of any implementation of a method defined for YourClass
  697. before() : Generic<YourClass>.foo() {
  698. // ...
  699. }
  700. // results in a compilation error - raw type reference
  701. before() : Generic.foo() { }
  702. }
  703. ]]></programlisting>
  704. </sect3>
  705. </sect2>
  706. <sect2 id="inter-type-declarations" xreflabel="inter-type-declarations">
  707. <title>Inter-type Declarations</title>
  708. <para>
  709. AspectJ 5 supports the inter-type declaration of generic methods, and of members on
  710. generic types. For generic methods, the syntax is exactly as for a regular method
  711. declaration, with the addition of the target type specification:
  712. </para>
  713. <variablelist>
  714. <varlistentry>
  715. <term>&lt;T extends Number&gt; T Utils.max(T first, T second) {...}</term>
  716. <listitem>
  717. <para>Declares a generic instance method <literal>max</literal> on the class <literal>Util</literal>.
  718. The <literal>max</literal> method takes two arguments, <literal>first</literal> and <literal>second</literal> which must
  719. both be of the same type (and that type must be Number or a subtype of Number) and returns an instance
  720. of that type.
  721. </para>
  722. </listitem>
  723. </varlistentry>
  724. <varlistentry>
  725. <term>static &lt;E&gt; E Utils.first(List&lt;E&gt; elements) {...}</term>
  726. <listitem>
  727. <para>Declares a static generic method <literal>first</literal> on the class <literal>Util</literal>.
  728. The <literal>first</literal> method takes a list of elements of some type, and returns an instance
  729. of that type.
  730. </para>
  731. </listitem>
  732. </varlistentry>
  733. <varlistentry>
  734. <term>&lt;T&gt; Sorter.new(List&lt;T&gt; elements,Comparator&lt;? super T&gt; comparator) {...}</term>
  735. <listitem>
  736. <para>Declares a constructor on the class <literal>Sorter</literal>.
  737. The constructor takes a list of elements of some type, and a comparator that can compare instances
  738. of the element type.
  739. </para>
  740. </listitem>
  741. </varlistentry>
  742. </variablelist>
  743. <para>
  744. A generic type may be the target of an inter-type declaration, used either in its raw form or with
  745. type parameters specified. If type parameters are specified, then the number of type parameters given
  746. must match the number of type parameters in
  747. the generic type declaration. Type parameter <emphasis>names</emphasis> do not have to match.
  748. For example, given the generic type <literal>Foo&lt;T,S extends Number&gt;</literal> then:
  749. </para>
  750. <variablelist>
  751. <varlistentry>
  752. <term>String Foo.getName() {...}</term>
  753. <listitem>
  754. <para>Declares a <literal>getName</literal> method on behalf of the type <literal>Foo</literal>. It is
  755. not possible to refer to the type parameters of Foo in such a declaration.
  756. </para>
  757. </listitem>
  758. </varlistentry>
  759. <varlistentry>
  760. <term>public R Foo&lt;Q, R&gt;.getMagnitude() {...}</term>
  761. <listitem>
  762. <para>Declares a method <literal>getMagnitude</literal> on the generic class <literal>Foo</literal>.
  763. The method returns an instance of the type substituted for the second type parameter in an invocation
  764. of <literal>Foo</literal> If <literal>Foo</literal> is declared as
  765. <literal>Foo&lt;T,N extends Number&gt; {...}</literal> then this inter-type declaration is
  766. equivalent to the declaration of a method <literal>public N getMagnitude()</literal>
  767. within the body of <literal>Foo</literal>.
  768. </para>
  769. </listitem>
  770. </varlistentry>
  771. <varlistentry>
  772. <term>R Foo&lt;Q, R extends Number&gt;.getMagnitude() {...}</term>
  773. <listitem>
  774. <para>Results in a compilation error since a bounds specification is not allowed in this
  775. form of an inter-type declaration (the bounds are determined from the declaration of the
  776. target type).
  777. </para>
  778. </listitem>
  779. </varlistentry>
  780. </variablelist>
  781. <para>A parameterized type may not be the target of an inter-type declaration. This is because
  782. there is only one type (the generic type) regardless of how many different invocations (parameterizations) of
  783. that generic type are made in a program. Therefore it does not make sense to try and declare a member
  784. on behalf of (say) <literal>Bar&lt;String&gt;</literal>, you can only declare members on the generic
  785. type <literal>Bar&lt;T&gt;</literal>.
  786. </para>
  787. </sect2>
  788. <sect2 id="declare-parents-java5" xreflabel="declare-parents-java5">
  789. <title>Declare Parents</title>
  790. <para>Both generic and parameterized types can be used as the parent type in a <literal>declare parents</literal>
  791. statement (as long as the resulting type hierarchy would be well-formed in accordance with Java's sub-typing
  792. rules). Generic types may also be used as the target type of a <literal>declare parents</literal> statement.</para>
  793. <variablelist>
  794. <varlistentry>
  795. <term>declare parents: Foo implements List&lt;String&gt;</term>
  796. <listitem>
  797. <para>The <literal>Foo</literal> type implements the <literal>List&lt;String&gt;</literal> interface. If
  798. <literal>Foo</literal> already implements some other parameterization of the <literal>List</literal>
  799. interface (for example, <literal>List&lt;Integer&gt;</literal> then a compilation error will result since a
  800. type cannot implement multiple parameterizations of the same generic interface type.
  801. </para>
  802. </listitem>
  803. </varlistentry>
  804. </variablelist>
  805. </sect2>
  806. <sect2 id="declare-soft" xreflabel="declare-soft">
  807. <title>Declare Soft</title>
  808. <para>It is an error to use a generic or parameterized type as the softened exception type in a declare soft statement. Java 5 does
  809. not permit a generic class to be a direct or indirect subtype of <literal>Throwable</literal> (JLS 8.1.2).</para>
  810. </sect2>
  811. <sect2 id="generic-aspects" xreflabel="generic-aspects">
  812. <title>Generic Aspects</title>
  813. <para>
  814. AspectJ 5 allows an <emphasis>abstract</emphasis> aspect to be declared as a generic type. Any concrete
  815. aspect extending a generic abstract aspect must extend a parameterized version of the abstract aspect.
  816. Wildcards are not permitted in this parameterization.
  817. </para>
  818. <para>Given the aspect declaration:</para>
  819. <programlisting><![CDATA[
  820. public abstract aspect ParentChildRelationship<P,C> {
  821. ...
  822. }
  823. ]]></programlisting>
  824. <para>then</para>
  825. <variablelist>
  826. <varlistentry>
  827. <term>public aspect FilesInFolders extends ParentChildRelationship&lt;Folder,File&gt; {...</term>
  828. <listitem>
  829. <para>declares a concrete sub-aspect, <literal>FilesInFolders</literal> which extends the
  830. parameterized abstract aspect <literal>ParentChildRelationship&lt;Folder,File&gt;</literal>.
  831. </para>
  832. </listitem>
  833. </varlistentry>
  834. <varlistentry>
  835. <term>public aspect FilesInFolders extends ParentChildRelationship {...</term>
  836. <listitem>
  837. <para>results in a compilation error since the <literal>ParentChildRelationship</literal> aspect must
  838. be fully parameterized.
  839. </para>
  840. </listitem>
  841. </varlistentry>
  842. <varlistentry>
  843. <term>public aspect ThingsInFolders&lt;T&gt; extends ParentChildRelationship&lt;Folder,T&gt;</term>
  844. <listitem>
  845. <para>results in a compilation error since concrete aspects may not have type parameters.
  846. </para>
  847. </listitem>
  848. </varlistentry>
  849. <varlistentry>
  850. <term>public abstract aspect ThingsInFolders&lt;T&gt; extends ParentChildRelationship&lt;Folder,T&gt;</term>
  851. <listitem>
  852. <para>declares a sub-aspect of <literal>ParentChildRelationship</literal> in which <literal>Folder</literal>
  853. plays the role of parent (is bound to the type variable <literal>P</literal>).
  854. </para>
  855. </listitem>
  856. </varlistentry>
  857. </variablelist>
  858. <para>The type parameter variables from a generic aspect declaration may be used in place of a type within any
  859. member of the aspect, <emphasis>except for within inter-type declarations</emphasis>.
  860. For example, we can declare a <literal>ParentChildRelationship</literal> aspect to
  861. manage the bi-directional relationship between parent and child nodes as follows:
  862. </para>
  863. <programlisting><![CDATA[
  864. /**
  865. * a generic aspect, we've used descriptive role names for the type variables
  866. * (Parent and Child) but you could use anything of course
  867. */
  868. public abstract aspect ParentChildRelationship<Parent,Child> {
  869. /** generic interface implemented by parents */
  870. interface ParentHasChildren<C extends ChildHasParent>{
  871. List<C> getChildren();
  872. void addChild(C child);
  873. void removeChild(C child);
  874. }
  875. /** generic interface implemented by children */
  876. interface ChildHasParent<P extends ParentHasChildren>{
  877. P getParent();
  878. void setParent(P parent);
  879. }
  880. /** ensure the parent type implements ParentHasChildren<child type> */
  881. declare parents: Parent implements ParentHasChildren<Child>;
  882. /** ensure the child type implements ChildHasParent<parent type> */
  883. declare parents: Child implements ChildHasParent<Parent>;
  884. // Inter-type declarations made on the *generic* interface types to provide
  885. // default implementations.
  886. /** list of children maintained by parent */
  887. private List<C> ParentHasChildren<C>.children = new ArrayList<C>();
  888. /** reference to parent maintained by child */
  889. private P ChildHasParent<P>.parent;
  890. /** Default implementation of getChildren for the generic type ParentHasChildren */
  891. public List<C> ParentHasChildren<C>.getChildren() {
  892. return Collections.unmodifiableList(children);
  893. }
  894. /** Default implementation of getParent for the generic type ChildHasParent */
  895. public P ChildHasParent<P>.getParent() {
  896. return parent;
  897. }
  898. /**
  899. * Default implementation of addChild, ensures that parent of child is
  900. * also updated.
  901. */
  902. public void ParentHasChildren<C>.addChild(C child) {
  903. if (child.parent != null) {
  904. child.parent.removeChild(child);
  905. }
  906. children.add(child);
  907. child.parent = this;
  908. }
  909. /**
  910. * Default implementation of removeChild, ensures that parent of
  911. * child is also updated.
  912. */
  913. public void ParentHasChildren<C>.removeChild(C child) {
  914. if (children.remove(child)) {
  915. child.parent = null;
  916. }
  917. }
  918. /**
  919. * Default implementation of setParent for the generic type ChildHasParent.
  920. * Ensures that this child is added to the children of the parent too.
  921. */
  922. public void ChildHasParent<P>.setParent(P parent) {
  923. parent.addChild(this);
  924. }
  925. /**
  926. * Matches at an addChild join point for the parent type P and child type C
  927. */
  928. public pointcut addingChild(Parent p, Child c) :
  929. execution(* ParentHasChildren.addChild(ChildHasParent)) && this(p) && args(c);
  930. /**
  931. * Matches at a removeChild join point for the parent type P and child type C
  932. */
  933. public pointcut removingChild(Parent p, Child c) :
  934. execution(* ParentHasChildren.removeChild(ChildHasParent)) && this(p) && args(c);
  935. }
  936. ]]></programlisting>
  937. <para>
  938. The example aspect captures the protocol for managing a bi-directional parent-child relationship between
  939. any two types playing the role of parent and child. In a compiler implementation managing an abstract syntax
  940. tree (AST) in which AST nodes may contain other AST nodes we could declare the concrete aspect:
  941. </para>
  942. <programlisting><![CDATA[
  943. public aspect ASTNodeContainment extends ParentChildRelationship<ASTNode,ASTNode> {
  944. before(ASTNode parent, ASTNode child) : addingChild(parent, child) {
  945. ...
  946. }
  947. }
  948. ]]></programlisting>
  949. <para>
  950. As a result of this declaration, <literal>ASTNode</literal> gains members:
  951. </para>
  952. <simplelist>
  953. <member><literal>List&lt;ASTNode&gt; children</literal></member>
  954. <member><literal>ASTNode parent</literal></member>
  955. <member><literal>List&lt;ASTNode&gt;getChildren()</literal></member>
  956. <member><literal>ASTNode getParent()</literal></member>
  957. <member><literal>void addChild(ASTNode child)</literal></member>
  958. <member><literal>void removeChild(ASTNode child)</literal></member>
  959. <member><literal>void setParent(ASTNode parent)</literal></member>
  960. </simplelist>
  961. <para>
  962. In a system managing orders, we could declare the concrete aspect:
  963. </para>
  964. <programlisting><![CDATA[
  965. public aspect OrderItemsInOrders extends ParentChildRelationship<Order, OrderItem> {
  966. }
  967. ]]></programlisting>
  968. <para>
  969. As a result of this declaration, <literal>Order</literal> gains members:
  970. </para>
  971. <simplelist>
  972. <member><literal>List&lt;OrderItem&gt; children</literal></member>
  973. <member><literal>List&lt;OrderItem&gt; getChildren()</literal></member>
  974. <member><literal>void addChild(OrderItem child)</literal></member>
  975. <member><literal>void removeChild(OrderItem child)</literal></member>
  976. </simplelist>
  977. <para>and <literal>OrderItem</literal> gains members:</para>
  978. <simplelist>
  979. <member><literal>Order parent</literal></member>
  980. <member><literal>Order getParent()</literal></member>
  981. <member><literal>void setParent(Order parent)</literal></member>
  982. </simplelist>
  983. <para>A second example of an abstract aspect, this time for handling exceptions in a uniform
  984. manner, is shown below:</para>
  985. <programlisting><![CDATA[
  986. abstract aspect ExceptionHandling<T extends Throwable> {
  987. /**
  988. * method to be implemented by sub-aspects to handle thrown exceptions
  989. */
  990. protected abstract void onException(T anException);
  991. /**
  992. * to be defined by sub-aspects to specify the scope of exception handling
  993. */
  994. protected abstract pointcut inExceptionHandlingScope();
  995. /**
  996. * soften T within the scope of the aspect
  997. */
  998. declare soft: T : inExceptionHandlingScope();
  999. /**
  1000. * bind an exception thrown in scope and pass it to the handler
  1001. */
  1002. after() throwing (T anException) : inExceptionHandlingScope() {
  1003. onException(anException);
  1004. }
  1005. }
  1006. ]]></programlisting>
  1007. <para>Notice how the type variable <literal>T extends Throwable</literal> allows the
  1008. components of the aspect to be designed to work together in a type-safe manner. The
  1009. following concrete sub-aspect shows how the abstract aspect might be extended to
  1010. handle <literal>IOExceptions</literal>.</para>
  1011. <programlisting><![CDATA[
  1012. public aspect IOExceptionHandling extends ExceptionHandling<IOException>{
  1013. protected pointcut inExceptionHandlingScope() :
  1014. call(* doIO*(..)) && within(org.xyz..*);
  1015. /**
  1016. * called whenever an IOException is thrown in scope.
  1017. */
  1018. protected void onException(IOException ex) {
  1019. System.err.println("handled exception: " + ex.getMessage());
  1020. throw new MyDomainException(ex);
  1021. }
  1022. }
  1023. ]]></programlisting>
  1024. </sect2>
  1025. </sect1>
  1026. </chapter>