Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] A question about advice



Cong,

In that case you have the same problem you are faced with regular Java
where the information you need is not available from the method arguments
and you cannot or do not wish to modify the method signature. Rather than
modify the join point as Eric has suggested it may be more convenient to
require the aspect to query the information it needs through the use of a
context parameter (which we can add for free witha pointcut). We can use a
sightly modified "this" to facilitate the query:

   public class UserRecord
   {
       private UserList usrlist = new UserList();

      public void run () {

           User u1 = new User("Alice");
           User u2 = new User("Bob");

           usrlist.add(u1);

      }

      public UserList getUsrlist() {
            return usrlist;
      }

      public static void main(String args[]) {
            new UserRecord().run();
       }
   }

   class User
   {
      public User (String s) {

      }
   }

   class UserList
   {
      public void add (User u) {

      }

      public boolean isMember (User u) {
            return true;
      }
   }

   aspect Aspect_one
   {
      pointcut test (UserRecord ur) :
            call(User.new(..)) && this(ur);

      after(UserRecord ur) returning(User usr) : test(ur)
     {
             System.out.println("hello there");
       if (ur.getUsrlist().isMember(usr))
               System.out.println("The user is already in the list!");
      }

   }

As you should be aware local variables are not available as context to
advice unless they are the subject of a join point.

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/

Cong <yuncong@xxxxxxxxx>@eclipse.org on 15/03/2005 15:01:00

Please respond to aspectj-users@xxxxxxxxxxx

Sent by:    aspectj-users-admin@xxxxxxxxxxx


To:    aspectj-users@xxxxxxxxxxx
cc:
Subject:    Re: [aspectj-users] A question about advice


Thanks, Matthew. In the example I gave, it is true that I should
define a join point at add user method. I didn't because I think
there're some scenarios that you may want to use an object which is
not directly related to the join point.

Cong


On Tue, 15 Mar 2005 09:27:41 +0000, Matthew Webster
<matthew_webster@xxxxxxxxxx> wrote:
>
>
> A pointcut e.g. this/target/args can only be used to extract context that
> is a property of the join point. In your example "UserList" is not a
> property of the User constructor execution join point. A better aspect
> would be one that intercepted the addition of a User object to a list:
>
>   public class UserRecord
>   {
>      public static void main(String args[])
>      {
>           UserList usrlist = new UserList();
>
>           User u1 = new User("Alice");
>           User u2 = new User("Bob");
>
>           usrlist.add(u1);
>       }
>   }
>
>   class User
>   {
>      public User (String s) {
>
>      }
>   }
>
>   class UserList
>   {
>      public void add (User u) {
>
>      }
>
>      public boolean isMember (User u) {
>            return true;
>      }
>   }
>
>   aspect Aspect_one
>   {
>      pointcut test (UserList ul, User u) :
>            call(void add(User)) && target(ul) && args(u);
>
>      before(UserList usrlist, User usr) : test(usrlist,usr)
>     {
>             System.out.println("hello there");
>       if (usrlist.isMember(usr))
>               System.out.println("The user is already in the list!");
>      }
>
>   }
>
> If you want to prevent the addition of duplicates you will need to use
> around advice with conditional "proceed" or throw a RuntimeException.
>
> 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/
>
> Cong <yuncong@xxxxxxxxx>@eclipse.org on 15/03/2005 04:25:25
>
> Please respond to aspectj-users@xxxxxxxxxxx
>
> Sent by:    aspectj-users-admin@xxxxxxxxxxx
>
> To:    aspectj-users@xxxxxxxxxxx
> cc:
> Subject:    Re: [aspectj-users] A question about advice
>
>
> Thanks for your answer, Eric. For the first solution, if an updated
> usrlist is needed, then I have to define any method call that modifies
> the usrlist as join points. It seems not a good choice. For your
> second solution, I don't understand it quite well.
> "UserRecord.aspectOf().setUserList(usrlist)", to my understanding,
> aspectOf() is used with an aspect. But UserRecord is a class. ... a
> little bit confused *-)
>
> On Mon, 14 Mar 2005 20:53:25 -0600, Eric Bodden <eric@xxxxxxxxx> wrote:
> > > This aspect contains an error - usrlist is not defined. And my
> > > question is that is there anyway for the aspect code to get the
> > > reference of the usrlist object defined in the main()?
> > There are several ways to do so. You could intercept the creation of
the
> > list and store a reference in the aspect. Also you could explicitly
spass
> > the reference to the aspect using something like
> > "UserRecord.aspectOf().setUserList(usrlist)", which of course implies
> that
> > you have a matching method defined in the aspect.
> >
> > Eric
> >
> > _______________________________________________
> > 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
>
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/aspectj-users




Back to the top