Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Bizarre behavior with args pointcut


Hi Antti,

>   Now I would assume that both pieces of before advice in the above aspect
>  match the same join points as the only difference is that the first collects
>  context whereas the latter does not.


I'm afraid that is a bad assumption. Keep in mind that "args" is not just for grabbing context, it is a primitive pointcut that can be used to pick out join points such as method and constructor executions or exception handlers where there is a related argument or arguments. When you include args(d) in the definition of your changeOperations pointcut you are *restricting matches* to only those join points that have one argument of type double. That is why the credit(double) and debit(double) method executions were matched but not the transfer(Account, double) method.


For more on this, please refer to the section on primitive pointcuts in the AspectJ Programming Guide here ...

http://dev.eclipse.org/viewcvs/indextech.cgi/~checkout~/aspectj-home/doc/progguide/semantics-pointcuts.html#d0e5019


Best regards,
George
________________________________________
George C. Harley



Antti Karanta <Antti.Karanta@xxxxxxxxxxxxxx>
Sent by: aspectj-users-admin@xxxxxxxxxxx

31/03/2004 13:27

Please respond to
aspectj-users

To
aspectj-users@xxxxxxxxxxx
cc
Subject
[aspectj-users] Bizarre behavior with args pointcut







                                  Hi!

 I ran into bizarre behavior. A pointcut misses a method depending whether or
not I use args to collect context.

 I made a simplified example about this:

<-- clip -->

public class Account {

 public static void main(String[] args) {
   Account a1 = new Account();
   Account a2 = new Account();
   a1.credit(100);
   a2.credit(50);
   a1.debit(10);
   a1.transfer(a2, 30);
 }

  public void credit(double amount) {}

  public boolean debit(double amount) {
       return true;
  }

  public boolean transfer(Account target, double amount) {
     if(this.debit(amount)) {
        target.credit(amount);
        return true;
     }
     return false;
  }


}

aspect ContextCollectingAspect {

  pointcut changeOperations(Account a, double d) :
     execution(* Account.*(..,double,..))
     && this(a)
     && args(d);

 // this is the same as above, except it doesn't collect any context
 before() : execution(* Account.*(..,double,..)) {
     System.out.println(">>> Going into: " + thisJoinPointStaticPart);
  }

  before(Account a, double d): changeOperations(a, d) {
     System.out.println(">>> Checking preconditions for " + a +
        " in join point " + thisJoinPointStaticPart);
  }
}

</-- clip -->

 Now I would assume that both pieces of before advice in the above aspect
match the same join points as the only difference is that the first collects
context whereas the latter does not. However, the program prints:

<-- clip -->

>>> Going into: execution(void Account.credit(double))
>>> Checking preconditions for Account@1729854 in join point execution(void
Account.credit(double))
>>> Going into: execution(void Account.credit(double))
>>> Checking preconditions for Account@6eb38a in join point execution(void
Account.credit(double))
>>> Going into: execution(boolean Account.debit(double))
>>> Checking preconditions for Account@1729854 in join point execution(boolean
Account.debit(double))
>>> Going into: execution(boolean Account.transfer(Account, double))
>>> Going into: execution(boolean Account.debit(double))
>>> Checking preconditions for Account@1729854 in join point execution(boolean
Account.debit(double))
>>> Going into: execution(void Account.credit(double))
>>> Checking preconditions for Account@6eb38a in join point execution(void
Account.credit(double))

</-- clip -->

 As you can see, the context collecting pointcut misses the transfer
execution join point.

 After a bit of thinking I changed the context collection a little:

     && args(..,d,..);

 Resulting in:
Account.java:34 uses more than one .. in args (compiler limitation)
1 error

 So I changed it into:

     && args(..,d);

 and it works fine. It also seems to work even if the double is not the last
argument, even though it looks to me as if it shouldn't.

 Is there some logic to having to use the form args(..,d) when collecting
context? Why does the form args(d) not work here?
               

--
Antti Karanta <Antti.Karanta@xxxxxxxxxxxxxx>
Konsultti
FCS Partners Oyj
ph +358407634648
http://www.fcspartners.fi

_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/aspectj-users


Back to the top