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

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


Back to the top