Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Simple AspectJ question

Reply
Thread Tools

Simple AspectJ question

 
 
Donnie
Guest
Posts: n/a
 
      05-24-2004
Hi,

I am trying to learn Aspect Oriented Programming using AspectJ. I'm
having trouble getting the value of a class attribute in an aspect. In
the example listed below, I would like to retrieve the value of the
attribute x after the assignment.

public class Example {
public x;

public int setx (int y) {
this.x = y;
}

public static void main(String[] arguments) {
Example e = new Example();
e.setx(1);
}
}

public aspect AspectExample {
pointcut pcSetx() : execution(* Example.setx(..));

after() : pcSetx() {
int y;
y = //insert expression to retrieve the value of Example.x
System.out.println(y);
}
}

Thanks,
Donnie
 
Reply With Quote
 
 
 
 
Bryce
Guest
Posts: n/a
 
      05-25-2004
On 24 May 2004 14:13:22 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (Donnie) wrote:

>Hi,
>
>I am trying to learn Aspect Oriented Programming using AspectJ. I'm
>having trouble getting the value of a class attribute in an aspect. In
>the example listed below, I would like to retrieve the value of the
>attribute x after the assignment.
>
>public class Example {
> public x;
>
> public int setx (int y) {
> this.x = y;
> }
>
> public static void main(String[] arguments) {
> Example e = new Example();
> e.setx(1);
> }
>}
>
>public aspect AspectExample {
> pointcut pcSetx() : execution(* Example.setx(..));
>
> after() : pcSetx() {
> int y;
> y = //insert expression to retrieve the value of Example.x
> System.out.println(y);
> }
>}


First off, your code doesn't compile.

But after fixing it, try the following:

public class Example {
public int x;

public void setX(int y) {
x = y;
}

public static void main(String [] args) {
Example e = new Example();

e.setX(3);
}
}

public aspect AspectExample {
pointcut pcSetX(int y) : execution(* Example.setX(..)) && args(y);

after(int y) returning : pcSetX(y) {
System.out.println("After pcSetX + [" + y + "]");
}
}

--
now with more cowbell
 
Reply With Quote
 
 
 
 
Donnie
Guest
Posts: n/a
 
      05-26-2004
Bryce,

Thanks for the reply. I'm sorry about the typographical errors in the
program. I should have copied it from the editor. Your code worked
great, but I need a little more advice. Is it possible to get access
to the entire object (in your example "e") in the aspect? For
instance,

public class Example {
public int x;
public int z;

public void setX(int y) {
x = y;
}

public static void main(String [] args) {
Example e = new Example();

e.setX(3);
}
}


public aspect AspectExample {
pointcut pcSetX(int y) : execution(* Example.setX(..)) && args(y);

after(int y) returning : pcSetX(y) {
System.out.println("After pcSetX + [" + y + "]");
//line of code to print z
//line to print out - System.out.println(e.toString());
}
}

Thanks again.


Bryce <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..
> On 24 May 2004 14:13:22 -0700, (E-Mail Removed) (Donnie) wrote:
>
> >Hi,
> >
> >I am trying to learn Aspect Oriented Programming using AspectJ. I'm
> >having trouble getting the value of a class attribute in an aspect. In
> >the example listed below, I would like to retrieve the value of the
> >attribute x after the assignment.
> >
> >public class Example {
> > public x;
> >
> > public int setx (int y) {
> > this.x = y;
> > }
> >
> > public static void main(String[] arguments) {
> > Example e = new Example();
> > e.setx(1);
> > }
> >}
> >
> >public aspect AspectExample {
> > pointcut pcSetx() : execution(* Example.setx(..));
> >
> > after() : pcSetx() {
> > int y;
> > y = //insert expression to retrieve the value of Example.x
> > System.out.println(y);
> > }
> >}

>
> First off, your code doesn't compile.
>
> But after fixing it, try the following:
>
> public class Example {
> public int x;
>
> public void setX(int y) {
> x = y;
> }
>
> public static void main(String [] args) {
> Example e = new Example();
>
> e.setX(3);
> }
> }
>
> public aspect AspectExample {
> pointcut pcSetX(int y) : execution(* Example.setX(..)) && args(y);
>
> after(int y) returning : pcSetX(y) {
> System.out.println("After pcSetX + [" + y + "]");
> }
> }

 
Reply With Quote
 
Bryce
Guest
Posts: n/a
 
      05-26-2004
On 25 May 2004 18:03:15 -0700, (E-Mail Removed) (Donnie) wrote:

>Bryce,
>
>Thanks for the reply. I'm sorry about the typographical errors in the
>program. I should have copied it from the editor. Your code worked
>great, but I need a little more advice. Is it possible to get access
>to the entire object (in your example "e") in the aspect? For
>instance,


No problem. I'm just starting to learn AOP so I'm happy to pass onmy
limited knowledge.

Yes, you can get access to the entire object. Here's the modified
code:

public aspect AspectExample {
pointcut pcSetX(int y, Example e) : execution(* Example.setX(..))
&& args(y) && target(e);

after(int y, Example e) returning : pcSetX(y, e) {
System.out.println("After pcSetX + [" + y + "]");
System.out.println("Example.x [" + e.x + "]");
}
}

Note the "target(e)".

Hope this helps.

>public class Example {
> public int x;
> public int z;
>
> public void setX(int y) {
> x = y;
> }
>
> public static void main(String [] args) {
> Example e = new Example();
>
> e.setX(3);
> }
>}
>
>
>public aspect AspectExample {
> pointcut pcSetX(int y) : execution(* Example.setX(..)) && args(y);
>
> after(int y) returning : pcSetX(y) {
> System.out.println("After pcSetX + [" + y + "]");
> //line of code to print z
> //line to print out - System.out.println(e.toString());
> }
> }
>
>Thanks again.
>
>
>Bryce <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..
>> On 24 May 2004 14:13:22 -0700, (E-Mail Removed) (Donnie) wrote:
>>
>> >Hi,
>> >
>> >I am trying to learn Aspect Oriented Programming using AspectJ. I'm
>> >having trouble getting the value of a class attribute in an aspect. In
>> >the example listed below, I would like to retrieve the value of the
>> >attribute x after the assignment.
>> >
>> >public class Example {
>> > public x;
>> >
>> > public int setx (int y) {
>> > this.x = y;
>> > }
>> >
>> > public static void main(String[] arguments) {
>> > Example e = new Example();
>> > e.setx(1);
>> > }
>> >}
>> >
>> >public aspect AspectExample {
>> > pointcut pcSetx() : execution(* Example.setx(..));
>> >
>> > after() : pcSetx() {
>> > int y;
>> > y = //insert expression to retrieve the value of Example.x
>> > System.out.println(y);
>> > }
>> >}

>>
>> First off, your code doesn't compile.
>>
>> But after fixing it, try the following:
>>
>> public class Example {
>> public int x;
>>
>> public void setX(int y) {
>> x = y;
>> }
>>
>> public static void main(String [] args) {
>> Example e = new Example();
>>
>> e.setX(3);
>> }
>> }
>>
>> public aspect AspectExample {
>> pointcut pcSetX(int y) : execution(* Example.setX(..)) && args(y);
>>
>> after(int y) returning : pcSetX(y) {
>> System.out.println("After pcSetX + [" + y + "]");
>> }
>> }



--
now with more cowbell
 
Reply With Quote
 
Donnie
Guest
Posts: n/a
 
      05-27-2004
Bryce,

That's exactly what I was trying to figure out! I hope you will answer
two final questions. 1) Since you are obviously much further along in
learning AOP, what book(s) would you recommend? 2) How would the code
that you provided change if the method being examined was a
constructor of the class Example?

Thanks again,
Donnie

Bryce <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..
> On 25 May 2004 18:03:15 -0700, (E-Mail Removed) (Donnie) wrote:
>
> >Bryce,
> >
> >Thanks for the reply. I'm sorry about the typographical errors in the
> >program. I should have copied it from the editor. Your code worked
> >great, but I need a little more advice. Is it possible to get access
> >to the entire object (in your example "e") in the aspect? For
> >instance,

>
> No problem. I'm just starting to learn AOP so I'm happy to pass onmy
> limited knowledge.
>
> Yes, you can get access to the entire object. Here's the modified
> code:
>
> public aspect AspectExample {
> pointcut pcSetX(int y, Example e) : execution(* Example.setX(..))
> && args(y) && target(e);
>
> after(int y, Example e) returning : pcSetX(y, e) {
> System.out.println("After pcSetX + [" + y + "]");
> System.out.println("Example.x [" + e.x + "]");
> }
> }
>
> Note the "target(e)".
>
> Hope this helps.
>
> >public class Example {
> > public int x;
> > public int z;
> >
> > public void setX(int y) {
> > x = y;
> > }
> >
> > public static void main(String [] args) {
> > Example e = new Example();
> >
> > e.setX(3);
> > }
> >}
> >
> >
> >public aspect AspectExample {
> > pointcut pcSetX(int y) : execution(* Example.setX(..)) && args(y);
> >
> > after(int y) returning : pcSetX(y) {
> > System.out.println("After pcSetX + [" + y + "]");
> > //line of code to print z
> > //line to print out - System.out.println(e.toString());
> > }
> > }
> >
> >Thanks again.
> >
> >
> >Bryce <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..
> >> On 24 May 2004 14:13:22 -0700, (E-Mail Removed) (Donnie) wrote:
> >>
> >> >Hi,
> >> >
> >> >I am trying to learn Aspect Oriented Programming using AspectJ. I'm
> >> >having trouble getting the value of a class attribute in an aspect. In
> >> >the example listed below, I would like to retrieve the value of the
> >> >attribute x after the assignment.
> >> >
> >> >public class Example {
> >> > public x;
> >> >
> >> > public int setx (int y) {
> >> > this.x = y;
> >> > }
> >> >
> >> > public static void main(String[] arguments) {
> >> > Example e = new Example();
> >> > e.setx(1);
> >> > }
> >> >}
> >> >
> >> >public aspect AspectExample {
> >> > pointcut pcSetx() : execution(* Example.setx(..));
> >> >
> >> > after() : pcSetx() {
> >> > int y;
> >> > y = //insert expression to retrieve the value of Example.x
> >> > System.out.println(y);
> >> > }
> >> >}
> >>
> >> First off, your code doesn't compile.
> >>
> >> But after fixing it, try the following:
> >>
> >> public class Example {
> >> public int x;
> >>
> >> public void setX(int y) {
> >> x = y;
> >> }
> >>
> >> public static void main(String [] args) {
> >> Example e = new Example();
> >>
> >> e.setX(3);
> >> }
> >> }
> >>
> >> public aspect AspectExample {
> >> pointcut pcSetX(int y) : execution(* Example.setX(..)) && args(y);
> >>
> >> after(int y) returning : pcSetX(y) {
> >> System.out.println("After pcSetX + [" + y + "]");
> >> }
> >> }

 
Reply With Quote
 
Bryce
Guest
Posts: n/a
 
      05-27-2004
On 27 May 2004 08:53:57 -0700, (E-Mail Removed) (Donnie) wrote:

>That's exactly what I was trying to figure out! I hope you will answer
>two final questions.


I'll try...

> 1) Since you are obviously much further along in
>learning AOP, what book(s) would you recommend?


I'm reading 2 books currently:

AspectJ in Action from Manning publications:
http://www.manning.com/laddad

and

Mastering AspectJ
http://www.amazon.com/exec/obidos/tg...glance&s=books
http://tinyurl.com/2sqz9

However, I have found the documents on the AspectJ website to be more
than adequate
http://eclipse.org/aspectj/
and click "Documentation"

>2) How would the code
>that you provided change if the method being examined was a
>constructor of the class Example?


pointcut pcConstructor() : call(Example.new(..));

before() : pcConstructor() {
System.out.println("Before Constructor");
}

after() returning : pcConstructor() {
System.out.println("After Constructor");
}


--
now with more cowbell
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Basic AspectJ Question Novice Java 4 02-26-2012 02:34 PM
Question regarding AspectJ wildcards Matthias Kaeppler Java 2 04-18-2006 05:56 PM
AspectJ newbie question arturs_jurnieks@yahoo.com Java 1 03-11-2006 12:46 AM
AspectJ compiler and incrimental compilation? Peter Litvak Java 0 01-15-2004 03:45 PM
Is AspectJ the BEST ? Deepak Nayal Java 0 11-25-2003 01:32 PM



Advertisments