Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Newbie question about paramter exposure.

Hi,

Your description is ok:

> therefore, execution of the method `new A().execute("ls -alh")' triggers the around() advice, which passes in a value `ls -lah' (as the variable command). That value `ls -alh' passes to
> pointcut p(), which also takes a variable named command. And that pointcut p() tries to capture the execution of a method specified as `* example.A.execute(String)' and its argument
> must be `command.'

let me write it another way to see if that helps.  Think about the
events that occur when the program runs:
Main.main() starts executing
it calls the Main() constructor
it calls the process() method
Main.process() method starts executing
it calls the A() constructor
it calls the execute("ls -alh") method
A.execute("ls -alh")  method starts executing *
it accesses the field System.out
it calls the method println()

all those are the joinpoints in your program flow and your pointcut is
choosing the one you are interested in.  You have used 'execution(*
execute(String))' which has selected the one I've marked * above.  In
your pointcut you've also said you are interested in the parameter
when the method executes, so you have used 'args' to bind it - so when
the program runs and your point of interest is reached, the parameter
value being passed in is bound to the command parameter in your
pointcut.  You have chosen to have around() advice attached to that
pointcut so when the method-execution join point occurs, your around
advice is invoked with the bound parameter value.  Unlike a regular
method call, the weaving infrastructure is looking after binding the
pointcut/advice parameter for you.

Andy



2009/10/27 Neo Anderson <javadeveloper999@xxxxxxxxxxx>:
> Hi
>
> I am newbie to aspectj and have a question regarding to the usage of parameters exposed in the pointcut, advice, etc. Following is the source code:
>
> package aspectj;
>
> public aspect P{
>        public pointcut p(String command): execution(* example.A.execute(String)) && args(command);
>
>        Object around(String command): p(command){
>                System.out.println("around() advice : command:"+command);
>                return proceed(command);
>        }
> }
>
> package example;
>
> public class A{
>        public void execute(String c){
>                System.out.println("[A.java][execute] command:"+c);
>        }
> }
>
> package example;
>
> public class Main{
>
>        public static void main(String args[]){
>                new Main().process();
>        }
>
>        void process(){
>                new A().execute("ls -alh");
>        }
>
> }
>
> The output :
>
> around() advice : command:ls -alh
> [A.java][execute] command:ls -alh
>
>
> Thought I can get the code worked, I do not know how to explain the parameter exposed in the pointcut (e.g. p(String command)) and advice (e.g. around(String command)).
>
> What I understand is that
>
> Since the execution flow is
>
> new A().execute("ls -lah") [Main.java] -> public void execute(String c) [A.java]
>
> therefore, execution of the method `new A().execute("ls -alh")' triggers the around() advice, which passes in a value `ls -lah' (as the variable command). That value `ls -alh' passes to pointcut p(), which also takes a variable named command. And that pointcut p() tries to capture the execution of a method specified as `* example.A.execute(String)' and its argument must be `command.'
>
> Is this explanation correct?
>
> I found out there is something not right for me, but I am not aware the part that I do not understand.
>
> I appreciate any advice.
>
> Thank you very much.
>
>
> Send instant messages to your online friends http://uk.messenger.yahoo.com
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>


Back to the top