[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [aspectj-users] A question about advice
|
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