Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] How to advice only non-anonymous classes in a hierarchy

Yes, I know. That's why I wrote that my solution is "far from being efficient". I just thought it's worth while providing a solution of some sort.

The thread that you refer to is very interesting, but it applies only to Runnable class (which, I admit, are very often anonymous), so it may not apply to all cases. For example, it won't apply to my example in this thread, no matter how bad this example is.

Matthew Webster wrote:



Jonathan,

There be dragons! Your solution relies on a naming convention used by the
Java compiler not the JVM Specification. The subject of targeting anonymous
classes with pointcuts has been discussed before:
http://dev.eclipse.org/mhonarc/lists/aspectj-users/msg01792.html. Trying to
refer to an anonymous class by name is a strange concept so use wildcards
instead. In the following example:

public class Test implements Runnable {

      public void run () {

      }

      public void test () {
            new Runnable () {
                  public void run() {

                  }
            };
      }
}

public aspect Aspect {

      pointcut run () :
            execution(void run()) && !within(Test.*);
      before () : run () {
            System.out.println(thisJoinPoint.getSignature());
      }
}

the pointcut will not match the inner class.

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/


Jonathan Amir <jamir@xxxxxxxxxxxxxx>@eclipse.org on 16/09/2004 23:15:35

Please respond to aspectj-users@xxxxxxxxxxx

Sent by:    aspectj-users-admin@xxxxxxxxxxx


To:    aspectj-users@xxxxxxxxxxx
cc:
Subject:    Re: [aspectj-users] How to advice only non-anonymous classes in
       a hierarchy


Have a look at the following code:

public class Test {
   public static void main(String[] args) {
     new MyTopLevelElement ().aMethod();
     new MyTopLevelElement () {
       public void aMethod () {
         System.out.println ("aMethod (overrided) from " +
getClass().getName());
       }
     }.aMethod();
   }
}

class MyTopLevelElement {
   public void aMethod () {
     System.out.println ("aMethod from " + getClass().getName());
   }
}

aspect NonAnonymous {
   pointcut nonAnonymous (): execution(void MyTopLevelElement+.aMethod());

   before (): nonAnonymous () {
     System.out.println ("aspectJ in action ...");
     System.out.println (thisJoinPoint.toLongString());
   }
}

Running this code yields the following output:

   aspectJ in action ...
   execution(public void MyTopLevelElement.aMethod())
   aMethod from MyTopLevelElement
   aspectJ in action ...
   execution(public void Test.1.aMethod())
   aMethod (overrided) from Test$1

So, inner classes have a dollar sign in their name (before the last token
in their fully qualified
name. However, it seems that this $ sign disappears when aspectJ is
running.

One solution to your problem is as follows (however, it is far from being
an efficient solution):

change the pointcut declaration at the above example to this:

   pointcut nonAnonymous (): execution(void MyTopLevelElement+.aMethod())
   && !execution (void *..*1.aMethod())
   && !execution (void *..*2.aMethod())
   && !execution (void *..*3.aMethod())
   && !execution (void *..*4.aMethod())
   && !execution (void *..*5.aMethod())
   && !execution (void *..*6.aMethod())
   && !execution (void *..*7.aMethod())
   && !execution (void *..*8.aMethod())
   && !execution (void *..*9.aMethod())
   && !execution (void *..*0.aMethod());

The new output now looks like this:

   aspectJ in action ...
   execution(public void MyTopLevelElement.aMethod())
   aMethod from MyTopLevelElement
   aMethod (overrided) from Test$1

This solution works even if the number associated with an inner class has
more than one digit.

Hope this helps ...

Jonathan






Marius Marin wrote:


Hello,

Is it possible in AspectJ to advice all the classes in
a class hierarchy but not the anonymous ones?
Define a pointcut like:
execution(void MyTopLevelAClass+.aMethod())
&& ??? //exclude the anonymous subclasses

Thanks,
M.



_______________________________
Do you Yahoo!?
Declare Yourself - Register online to vote today!
http://vote.yahoo.com
_______________________________________________
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



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


Back to the top