<chapter id="covariance" xreflabel="Covariance">

  <title>Covariance</title>
  
      <sect1 id="covariance-inJava5">
        <title>Covariance in Java 5</title>
        
        <para>
          Java 5 (and hence AspectJ 5) allows you to narrow the return type
          in an overriding method. For example:
        </para>

		<programlisting><![CDATA[
		class A {
		  public A whoAreYou() {...}
		}
		
		class B extends A {
		  // override A.whoAreYou *and* narrow the return type.
		  public B whoAreYou() {...}
		}
		]]></programlisting>
        
      </sect1>
      
      <sect1 id="covariance-and-join-point-matching">
          <title>Covariant methods and Join Point matching</title>
          
          <para>The join point matching rules for <literal>call</literal>
          and <literal>execution</literal> pointcut designators are extended
          to match against covariant methods.</para>
          
          <para>
              Given the classes <literal>A</literal> and <literal>B</literal>
              as defined in the previous section, and the program fragment
          </para>

		<programlisting><![CDATA[
		A a = new A();
		B b = new B();
		a.whoAreYou();
		b.whoAreYou();
		]]></programlisting>
		
		<para>The signatures for the call join point <literal>a.whoAreYou()</literal> are
		simply:</para>

		<programlisting><![CDATA[
		A A.whoAreYou()
		]]></programlisting>
		
		<para>The signatures for the call join point <literal>b.whoAreYou()</literal> are:
		</para>

		<programlisting><![CDATA[
		A A.whoAreYou()
		B B.whoAreYou()
		]]></programlisting>
		
		<para>Following the join point matching rules given in <xref linkend="jpsigs"/>,</para>
		
		<variablelist>

        <varlistentry>
          <term>call(* whoAreYou())</term>
          <listitem>
            <para>Matches both calls, (since each call join point has at least
            one matching signature).
            </para>
          </listitem>
        </varlistentry>

        <varlistentry>
          <term>call(* A.whoAreYou())</term>
          <listitem>
            <para>Matches both calls, (since each call join point has at least
            one matching signature).
            </para>
          </listitem>
        </varlistentry>

        <varlistentry>
          <term>call(A whoAreYou())</term>
          <listitem>
            <para>Matches both calls, (since each call join point has at least
            one matching signature).
            </para>
          </listitem>
        </varlistentry>
		
        <varlistentry>
          <term>call(A B.whoAreYou())</term>
          <listitem>
            <para>Does not match anything - neither of the call join points
            has a signature matched by this pattern.  A lint warning is
            given for the call <literal>a.whoAreYou()</literal> ("does not match
            because declaring type is A, if match required use target(B)").
            </para>
          </listitem>
        </varlistentry>
		
        <varlistentry>
          <term>call(A+ B.whoAreYou())</term>
          <listitem>
            <para>Matches the call to <literal>b.whoAreYou()</literal> since
            the signature pattern matches the signature <literal>B B.whoAreYou()</literal>. 
            A lint warning is given for the call <literal>a.whoAreYou()</literal> ("does not match
            because declaring type is A, if match required use target(B)").
            </para>
          </listitem>
        </varlistentry>
				
        <varlistentry>
          <term>call(B A.whoAreYou())</term>
          <listitem>
            <para>Does not match anything since neither join point has a 
            signature matched by this pattern.
            </para>
          </listitem>
        </varlistentry>

        <varlistentry>
          <term>call(B whoAreYou())</term>
          <listitem>
            <para>Matches the call to <literal>b.whoAreYou()</literal> only.
            </para>
          </listitem>
        </varlistentry>

        <varlistentry>
          <term>call(B B.whoAreYou())</term>
          <listitem>
            <para>Matches the call to <literal>b.whoAreYou()</literal> only.
            </para>
          </listitem>
        </varlistentry>

		</variablelist>
		
		<para>The rule for signature matching at call and execution join points
		is unchanged from AspectJ 1.2: a call or execution pointcut matches if
		the signature pattern matches at least one of the signatures of the
		join point, and if the modifiers of the method or constructor are matched
		by any modifier pattern or annotation pattern that may be present.</para>
		
		</sect1>
</chapter>

