Simple AspectJ question

D

Donnie

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
 
B

Bryce

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 + "]");
}
}
 
D

Donnie

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 said:
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 + "]");
}
}
 
B

Bryce

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 said:
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 + "]");
}
}
 
D

Donnie

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 said:
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 said:
On 24 May 2004 14:13:22 -0700, (e-mail address 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 + "]");
}
}
 
B

Bryce

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/t...f=sr_1_2/102-5639709-5614568?v=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");
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top