[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[List Home]
|
Re: [aspectj-users] Newbie question about paramter exposure.
|
- From: Andy Clement <andrew.clement@xxxxxxxxx>
- Date: Tue, 27 Oct 2009 06:51:29 -0700
- Delivered-to: aspectj-users@eclipse.org
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:content-type :content-transfer-encoding; bh=Ev79EOcqFQroVkJqm55m7oet4uD4tg1oKytk62C+qdI=; b=cB7s0pqGtaWzZteWCxx2L/kCcw+VpGRvgIRsTpYekr6QKv6wg81AzJMl2mPp3nfVR9 gawkeQq80xv6u3sEKHfFAWlcPqeZAg9OdmLm5Crzrax/6SY2PNbMdE2MwLpWeJlCm5N6 exuVPAfCceyD1VJ+TnCVLhbPF91J98WMC1YOE=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=m1wjh+GoU50Bz2ftUU4SrgqYFpMIBirJOp3d07PCzncQAta8qJWe0F5dQOCRXgjU48 Oi9/GNQawEAL7HKmlzuPrZ74B9aWjJZmMk3qi3PeHm3R9NQQ6aQTeVymXV8vWIxOu9nI 0MtoID9U22aDwupz1JbruxzUl7G6lEMXH8AY4=
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
>