Simple AspectJ question

Discussion in 'Java' started by Donnie, May 24, 2004.

  1. Donnie

    Donnie Guest

    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
    Donnie, May 24, 2004
    #1
    1. Advertising

  2. Donnie

    Bryce Guest

    On 24 May 2004 14:13:22 -0700, (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
    Bryce, May 25, 2004
    #2
    1. Advertising

  3. Donnie

    Donnie Guest

    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 <> wrote in message news:<>...
    > On 24 May 2004 14:13:22 -0700, (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 + "]");
    > }
    > }
    Donnie, May 26, 2004
    #3
  4. Donnie

    Bryce Guest

    On 25 May 2004 18:03:15 -0700, (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 <> wrote in message news:<>...
    >> On 24 May 2004 14:13:22 -0700, (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
    Bryce, May 26, 2004
    #4
  5. Donnie

    Donnie Guest

    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 <> wrote in message news:<>...
    > On 25 May 2004 18:03:15 -0700, (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 <> wrote in message news:<>...
    > >> On 24 May 2004 14:13:22 -0700, (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 + "]");
    > >> }
    > >> }
    Donnie, May 27, 2004
    #5
  6. Donnie

    Bryce Guest

    On 27 May 2004 08:53:57 -0700, (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/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");
    }


    --
    now with more cowbell
    Bryce, May 27, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Deepak Nayal

    Is AspectJ the BEST ?

    Deepak Nayal, Nov 25, 2003, in forum: Java
    Replies:
    0
    Views:
    384
    Deepak Nayal
    Nov 25, 2003
  2. Peter Litvak
    Replies:
    0
    Views:
    484
    Peter Litvak
    Jan 15, 2004
  3. Replies:
    1
    Views:
    337
    IchBin
    Mar 11, 2006
  4. Matthias Kaeppler

    Question regarding AspectJ wildcards

    Matthias Kaeppler, Apr 17, 2006, in forum: Java
    Replies:
    2
    Views:
    571
    Mark Thomas
    Apr 18, 2006
  5. Novice

    Basic AspectJ Question

    Novice, Feb 25, 2012, in forum: Java
    Replies:
    4
    Views:
    419
    Novice
    Feb 26, 2012
Loading...

Share This Page