Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] 'IF' pointcut to test some arguments

In what way "doesn't work as I expected in this case" - are you saying
that you still see the gutter annotations in AJDT when you wouldn't
expect to? Or does it not work when you run it?  This program works,
using your pointcut:

aspect X {
  Object around(Object obj, String myString):
               if("value".equals(myString)) &&
               args(obj, myString) &&
               call(static Object MyClass.methodA(Object, String)) {
   System.err.println("advice running");
   return obj;
  }
}


public class MyClass {

  public static void main(String []argv) {
    methodA(42,"str1");
    methodA(42,"value");
    methodA(42,"value");
  }

  public static Object methodA(Object a,String f) {
    System.err.println(f);
    return a;
  }
}

when compiled:

C:\temp>ajc -1.5 -showWeaveInfo MyClass.java
Join point 'method-call(java.lang.Object
MyClass.methodA(java.lang.Object, java.lang.String))' in Type
'MyClass' (MyClass.java:17) advised by around advice from 'X'
(MyClass.java:3) [with runtime test]

Join point 'method-call(java.lang.Object
MyClass.methodA(java.lang.Object, java.lang.String))' in Type
'MyClass' (MyClass.java:18) advised by around advice from 'X'
(MyClass.java:3) [with runtime test]

Join point 'method-call(java.lang.Object
MyClass.methodA(java.lang.Object, java.lang.String))' in Type
'MyClass' (MyClass.java:19) advised by around advice from 'X'
(MyClass.java:3) [with runtime test]

All 3 calls are advised, but I can see the suffix 'with runtime test'
indicating that my advice execution is conditional, and the condition
in this case will be checking the second parameter is the string
"value".

When I run it:

C:\temp>java MyClass
str1
advice running
advice running

It behaves as expected, my advice fires twice.  The kind of test you
want to perform is executed *at runtime* - and so all the places that
match the rest of the pointcut are advised and the message 'runtime
test' indicates the advice still may not execute when the program is
run.  In AJDT you can tell matches are conditional like this by
observing that they have a tiny '?' on them.

Andy.


On 24/04/06, Chanwit Kaewkasi <chanwit@xxxxxxxxx> wrote:
> Hello all,
>
> With AspectJ 1.5, can I use the 'if' pointcut like this:
>         Object around(Object obj, String myString):
>                 if("value".equals(myString)) &&
>                 args(obj, myString) &&
>                 call(static Object MyClass.methodA(Object, String)) {
>                         ...
>         }
> to have a conditional advice?
>
> I've tested the above code, but it seems that the IF pointcut doesn't
> work as I expected in this case.
>
> Best regards,
>
> Chanwit
>
> --
> Chanwit Kaewkasi
> Center for Novel Computing
> School of Computer Science
> The University of Manchester
> Oxford Road
> Manchester
> M13 9PL, UK
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>


Back to the top