Newbie question about paramter exposure.

View: New views
4 Messages — Rating Filter:   Alert me  

Newbie question about paramter exposure.

by neo anderson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...
https://dev.eclipse.org/mailman/listinfo/aspectj-users

Re: Newbie question about paramter exposure.

by Andy Clement :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...>:

> 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@...
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>
_______________________________________________
aspectj-users mailing list
aspectj-users@...
https://dev.eclipse.org/mailman/listinfo/aspectj-users

Re: Newbie question about paramter exposure.

by neo anderson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Just want to practise to see if I understand it correctly.

The point of interest

        A.execute("ls -alh")

is reached.

That point of interest is specified in

        execution(* example.A.execute(String)) && args(command);

So the parameter value being passed in (i.e. ls -alh) is bound to the command
parameter in the pointcut p(String command). Thus the value of `command' (in pointcut p(String command)) now can be replaced with "ls -alh" (whilst method's executing).

And because an advice around(String command) is attached to the pointcut p(String command), when the method execution join point occurs, it becomes

        Object advice(String command): p("ls -alh"){
                ...
        }

And then when advice is invoked

        Object advice("ls -alh"): p("ls -alh"){
                ...
        }

Am I correct to the above explanation?

I discover my problem is because after reading the book e.g. Aspectj in Action, I still do not understand very well about the flow that goes between joint points, pointcuts and advices. In a traditional method call, I understand (maybe not precisely) that in the class Main, when executing `new A().execute("ls -alh")', the value `ls -alh' will pass in to the execute() method of the class A; therefore, in the execute() function of the class A, the variable `command' can be replaced using the value `ls -alh' and gets printed using System.out.println() method in execution.

But concept of aop looks like not the same way; so it makes me confused.

Please correct me if anything goes wrong.

Thanks for your help.

I really appreciate it.




Andy Clement wrote:
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@yahoo.co.uk>:
> 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@eclipse.org
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>
_______________________________________________
aspectj-users mailing list
aspectj-users@eclipse.org
https://dev.eclipse.org/mailman/listinfo/aspectj-users

Re: Newbie question about paramter exposure.

by neo anderson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I just found out that in Aspectj in Action, it explains the passing context from a join point to advice is as below:

The interpretation of context whilst using advice looks like

        Object advice(String command): p(command){
                ...
        }

1.) command passing in to pointcut p()
2.) the execution object command is passed to advice(String command)
3.) which then passes to the body {...}.



Just want to practise to see if I understand it correctly.

The point of interest

        A.execute("ls -alh")

is reached.

That point of interest is specified in

        execution(* example.A.execute(String)) && args(command);

So the parameter value being passed in (i.e. ls -alh) is bound to the command
parameter in the pointcut p(String command). Thus the value of `command' (in pointcut p(String command)) now can be replaced with "ls -alh" (whilst method's executing).

And because an advice around(String command) is attached to the pointcut p(String command), when the method execution join point occurs, it becomes

        Object advice(String command): p("ls -alh"){
                ...
        }

And then when advice is invoked

        Object advice("ls -alh"): p("ls -alh"){
                ...
        }

Am I correct to the above explanation?

I discover my problem is because after reading the book e.g. Aspectj in Action, I still do not understand very well about the flow that goes between joint points, pointcuts and advices. In a traditional method call, I understand (maybe not precisely) that in the class Main, when executing `new A().execute("ls -alh")', the value `ls -alh' will pass in to the execute() method of the class A; therefore, in the execute() function of the class A, the variable `command' can be replaced using the value `ls -alh' and gets printed using System.out.println() method in execution.

But concept of aop looks like not the same way; so it makes me confused.

Please correct me if anything goes wrong.

Thanks for your help.

I really appreciate it.




Andy Clement wrote:
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@yahoo.co.uk>:
> 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@eclipse.org
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>
_______________________________________________
aspectj-users mailing list
aspectj-users@eclipse.org
https://dev.eclipse.org/mailman/listinfo/aspectj-users