Skip to main content

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

Thanks Jonathan,

But what I look for is a solution that does not imply
a signature-based pointcut, if any. 

I think the previous versions of Aspectj couldn't
advise the methods from anonymous classes - I wonder
whether it is possible to have this enabled in the
last verion. 

Marius

--- Jonathan Amir <jamir@xxxxxxxxxxxxxx> wrote:

> 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 advise 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!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


Back to the top