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.

covariance.xml 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <chapter id="covariance" xreflabel="Covariance">
  2. <title>Covariance</title>
  3. <sect1 id="covariance-inJava5">
  4. <title>Covariance in Java 5</title>
  5. <para>
  6. Java 5 (and hence AspectJ 5) allows you to narrow the return type
  7. in an overriding method. For example:
  8. </para>
  9. <programlisting><![CDATA[
  10. class A {
  11. public A whoAreYou() {...}
  12. }
  13. class B extends A {
  14. // override A.whoAreYou *and* narrow the return type.
  15. public B whoAreYou() {...}
  16. }
  17. ]]></programlisting>
  18. </sect1>
  19. <sect1 id="covariance-and-join-point-matching">
  20. <title>Covariant methods and Join Point matching</title>
  21. <para>The join point matching rules for <literal>call</literal>
  22. and <literal>execution</literal> pointcut designators are extended
  23. to match against covariant methods.</para>
  24. <para>
  25. Given the classes <literal>A</literal> and <literal>B</literal>
  26. as defined in the previous section, and the program fragment
  27. </para>
  28. <programlisting><![CDATA[
  29. A a = new A();
  30. B b = new B();
  31. a.whoAreYou();
  32. b.whoAreYou();
  33. ]]></programlisting>
  34. <para>The signatures for the call join point <literal>a.whoAreYou()</literal> are
  35. simply:</para>
  36. <programlisting><![CDATA[
  37. A A.whoAreYou()
  38. ]]></programlisting>
  39. <para>The signatures for the call join point <literal>b.whoAreYou()</literal> are:
  40. </para>
  41. <programlisting><![CDATA[
  42. A A.whoAreYou()
  43. B B.whoAreYou()
  44. ]]></programlisting>
  45. <para>Following the join point matching rules given in <xref linkend="jpsigs"/>,</para>
  46. <variablelist>
  47. <varlistentry>
  48. <term>call(* whoAreYou())</term>
  49. <listitem>
  50. <para>Matches both calls, (since each call join point has at least
  51. one matching signature).
  52. </para>
  53. </listitem>
  54. </varlistentry>
  55. <varlistentry>
  56. <term>call(* A.whoAreYou())</term>
  57. <listitem>
  58. <para>Matches both calls, (since each call join point has at least
  59. one matching signature).
  60. </para>
  61. </listitem>
  62. </varlistentry>
  63. <varlistentry>
  64. <term>call(A whoAreYou())</term>
  65. <listitem>
  66. <para>Matches both calls, (since each call join point has at least
  67. one matching signature).
  68. </para>
  69. </listitem>
  70. </varlistentry>
  71. <varlistentry>
  72. <term>call(A B.whoAreYou())</term>
  73. <listitem>
  74. <para>Does not match anything - neither of the call join points
  75. has a signature matched by this pattern. A lint warning is
  76. given for the call <literal>a.whoAreYou()</literal> ("does not match
  77. because declaring type is A, if match required use target(B)").
  78. </para>
  79. </listitem>
  80. </varlistentry>
  81. <varlistentry>
  82. <term>call(A+ B.whoAreYou())</term>
  83. <listitem>
  84. <para>Matches the call to <literal>b.whoAreYou()</literal> since
  85. the signature pattern matches the signature <literal>B B.whoAreYou()</literal>.
  86. A lint warning is given for the call <literal>a.whoAreYou()</literal> ("does not match
  87. because declaring type is A, if match required use target(B)").
  88. </para>
  89. </listitem>
  90. </varlistentry>
  91. <varlistentry>
  92. <term>call(B A.whoAreYou())</term>
  93. <listitem>
  94. <para>Does not match anything since neither join point has a
  95. signature matched by this pattern.
  96. </para>
  97. </listitem>
  98. </varlistentry>
  99. <varlistentry>
  100. <term>call(B whoAreYou())</term>
  101. <listitem>
  102. <para>Matches the call to <literal>b.whoAreYou()</literal> only.
  103. </para>
  104. </listitem>
  105. </varlistentry>
  106. <varlistentry>
  107. <term>call(B B.whoAreYou())</term>
  108. <listitem>
  109. <para>Matches the call to <literal>b.whoAreYou()</literal> only.
  110. </para>
  111. </listitem>
  112. </varlistentry>
  113. </variablelist>
  114. <para>The rule for signature matching at call and execution join points
  115. is unchanged from AspectJ 1.2: a call or execution pointcut matches if
  116. the signature pattern matches at least one of the signatures of the
  117. join point, and if the modifiers of the method or constructor are matched
  118. by any modifier pattern or annotation pattern that may be present.</para>
  119. </sect1>
  120. </chapter>