Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Signature represents top-level class



Sarah,

As Ramnivas points out the Signature describes the defining type of the
currently executing join point. This becomes important when overriding
methods:

package com.sarah;

public class MyImpl extends MyParent {

      public void someMethod() {
      }

      public void invoke() {
            super.invoke();
      }
      public static void main(String[] args) {
            new MyImpl().invoke();
      }
}

package com.sarah;

public aspect MyAspect {
    pointcut MyImpl(com.sarah.MyImpl impl)
        : execution(* *(..)) && this(impl);

    before(com.sarah.MyImpl impl) : MyImpl(impl) {
        System.out.println(thisJoinPoint.getSignature().toLongString() + "
" + impl);
    }
}

Running this modified version of your example gives:

public void com.sarah.MyImpl.invoke() com.sarah.MyImpl@187aeca
public void com.sarah.MyParent.invoke() com.sarah.MyImpl@187aeca

This is why it is important to trace both static (Signtaure) and dynamic
(this) information so that you know where you are and who you are.

Matthew Webster
AOSD Project
Java Technology Centre, MP146
IBM Hursley Park, Winchester,  SO21 2JN, England
Telephone: +44 196 2816139 (external) 246139 (internal)
Email: Matthew Webster/UK/IBM @ IBMGB, matthew_webster@xxxxxxxxxx
http://w3.hursley.ibm.com/~websterm/


Ramnivas Laddad <ramnivas@xxxxxxxxxxxxxxx>@eclipse.org on 25/09/2004
00:40:55

Please respond to aspectj-users@xxxxxxxxxxx

Sent by:    aspectj-users-admin@xxxxxxxxxxx


To:    aspectj-users@xxxxxxxxxxx
cc:
Subject:    Re: [aspectj-users] Signature represents top-level class


Sarah,

Since the invoke method is defined in MyParent, the signature (which is
anchored to the join point) prints MyParent.

If you want to print the exact type of the object on which the method is
invoked you could capture the 'this' object as context and print its
class name. Here are two possible implementations:

public aspect MyAspect {
    pointcut MyImpl(Object theThis)
        : execution(* *(..)) && this(com.sarah.MyImpl) && this(theThis);

    before() : MyImpl() {
        System.out.println(thisJoinPoint.getSignature().toLongString());
             System.out.println(theThis.getClass());
    }
}


Or

public aspect MyAspect {
    pointcut MyImpl()
        : execution(* *(..)) && this(com.sarah.MyImpl);

    before() : MyImpl() {
        System.out.println(thisJoinPoint.getSignature().toLongString());
             System.out.println(thisJoinPoint.getThis().getClass());
    }
}


-Ramnivas

===
Ramnivas Laddad,
Author, AspectJ in Action
http://ramnivas.com



Sarah Haskins wrote:

>I was hoping someone might be able to explain a) why when I call
thisJoinPoint.getSignature().toLongString() I get the full method signature
for the top-level parent and b) how I might instead obtain the signature of
the actual instance on which the pointcut matched.
>
>Let me provide some code in case I'm not being clear.
>
>I have an Aspect like such...
>
>public aspect MyAspect {
>    pointcut MyImpl()
>        : execution(* *(..)) && this(com.sarah.MyImpl);
>
>    before() : MyImpl() {
>        System.out.println(thisJoinPoint.getSignature().toLongString());
>    }
>}
>
>And I have a top-level parent class like...
>
>public abstract class MyParent {
>    public abstract void someMethod();
>    public void invoke() {
>       ... do some simple stuff...
>    }
>}
>
>And I have an implementation of the abstract class like...
>
>public class MyImpl extends MyParent {
>    public void someMethod() {
>        // whatever
>    }
>}
>
>When I run this code and call myImpl.invoke(), the Aspect prints out...
> public void com.sarah.MyParent.invoke()
>
>Ideally I'd like to get public void com.sarah.MyImpl.invoke() instead.
>
>Apologies if this is common knowledge, but I can't seem to find
information on this anywhere.  (BTW, I'm subscribed to this email list, but
I get rejected when I try to search old emails from the website.  Anyone
know about that too?)
>
>Thanks all!
>
>------------------------------
>Sarah Haskins
>Software Development
>sarah.haskins@xxxxxxxx
>Sakonnet Technology
>594 Broadway, Suite 1008
>New York NY 10012
>www.sknt.com
>+1 917 237 38__ direct
>+1 212 343 3103 fax
>
>
>_______________________________________________________
>This message is for the named recipient's use only.  It may contain
sensitive and private proprietary information.  No confidentiality is
waived or lost by any incorrect transmission.  If you are not the intended
recipient, please immediately delete it and all copies of it from your
system, destroy any hard copies of it and notify the sender.  You must not,
directly or indirectly, use, disclose, distribute, print, or copy any part
of this message if you are not the intended recipient.  Sakonnet
Technology, LLC and its subsidiaries reserve the right to monitor all
e-mail communications through their networks. Any views expressed in this
message are those of the individual sender, except where the message states
otherwise and the sender is authorized to state them to be the views of any
such entity.  Unless otherwise stated, any pricing information given in
this message is indicative only, is subject to change and does not
constitute an offer to deal at any price quoted. Any reference to the terms
of executed transactions should be treated as preliminary only and subject
to our formal written confirmation.
>
>_______________________________________________
>aspectj-users mailing list
>aspectj-users@xxxxxxxxxxx
>http://dev.eclipse.org/mailman/listinfo/aspectj-users
>
>
>

_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/aspectj-users





Back to the top