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



Marius,

There is a rule that AO requires good OO so if a robust join point does not
exist you should create one. Until AspectJ supports metadata the only
option you have is refactoring which can improve the understandability of
your code as well. In the case of an anonymous inner class the approach is
to give it a name. So in your application your menu listener could be
called "ExitCommand" and you can write "&& !within(ExitCommand)" in your
pointcut. The only 2 advantages that anonymous classes have that I am aware
of are syntactic sugaring and a guarantee they are only instantiated once.
The former is a small price to pay for the benefits of AO while the latter
could probably be policed with another aspect.

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/


"Marius M." <marin_marius@xxxxxxxxx>@eclipse.org on 20/09/2004 17:01:46

Please respond to aspectj-users@xxxxxxxxxxx

Sent by:    aspectj-users-admin@xxxxxxxxxxx


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


Matthew,

Thank you for the pointer.

I don't believe the anonymous classes are useless  or
should be named. However, they are not reusable
elements so it is likely to have a different
implementation for them than for the named classes in
the same hierarchy. This is actually the case I
discuss: a menu where each item has an associated
command. Having a common interface for all the
commands is a good solution, imo. In the same time,
some commands (like 'exit the application') do not
need a named command class, being very simple. While
all the named commands can be advised for a
pre-condition check, this is not the case for the
anonymous classes in the same hierarchy.
It is nice that it is possible to advise the whole
hierarchy, but then I have to exclude the
non-anonymous classes on a signature/naming-basis
(specify their enclosing classes).

On the other hand, I thing the hierarchy-based
pointcut  can be "stronger/more consistent" than the
signature-based one.


Marius



--- Matthew Webster <matthew_webster@xxxxxxxxxx>
wrote:

>
>
>
>
> Marius,
>
> What you are trying to do may be possible in future
> versions of AspectJ
> with an extension to the pointcut language to select
> join points based on
> metadata
>
(http://dev.eclipse.org/mhonarc/lists/aspectj-dev/msg01047.html,
>
http://dev.eclipse.org/mhonarc/lists/aspectj-users/msg02283.html)
> because
> the Java compiler tags inner classes. However may I
> ask why you are trying
> to explicitly exclude anonymous classes from your
> pointcut matching?
>
> 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/
>
>
> "Marius M." <marin_marius@xxxxxxxxx>@eclipse.org on
> 17/09/2004 10:55:33
>
> Please respond to aspectj-users@xxxxxxxxxxx
>
> Sent by:    aspectj-users-admin@xxxxxxxxxxx
>
>
> To:    aspectj-users@xxxxxxxxxxx
> cc:
> Subject:    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
> _______________________________________________
> 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
>

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/aspectj-users





Back to the top