Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Newbie Questions: Biggest Issue

Thank you VERY much for clearing that up, Andy! NOW I can stop spinning my wheels and get on with learning AspectJ!

Given that the code in the example I was imitating didn't have any corresponding source code for the Java classes, I made an educated guess on what it was doing and obviously guessed wrong. I wasn't able to find any explanation of the exact syntax of the "after returning" statement and I thought "target()" must refer to what the method was returning. I didn't realize you specified that differently, as per your code. The proper syntax and what each parameter in that syntax means is documented somewhere, right? Preferably with examples so that I can be really clear exactly what is meant by each part of each statement....

--
Rhino

On 2012-02-24 18:13, Andy Clement wrote:
Hi,
    after(int num, String needle, String haystack) returning:
        target(num)
&&  args(needle, haystack)
&&  call(* Test01.count(..)) {

        System.out.println("** needle = " + needle + "; haystack = " +
haystack + "; num = " + num); //$NON-NLS-1$
    }
Your problem is target(num).  The target pointcut captures the target
of the method invocation (so in this case the instance of Test01 on
which count is running).  Test01 is not an int so your target()
doesn't match.  If you want to capture the return value of a call, you
want this:

after(String needle, String haystack) returning(int num):
   args(needle,haystack)&&  call(* Test01.count(..));

cheers,
Andy.


On 24 February 2012 14:17, Rhino<rhino1@xxxxxxxxxxxx>  wrote:
Greetings!

I'm just beginning with AspectJ today so I hope you can all forgive a newbie
  with some questions. I'll ask them in sequential emails to keep the size of
each question manageable....

While the idea behind AspectJ seems worthwhile and I'm already starting to
get a sense of the potential, I'm getting bogged down in beginner issues.

The biggest problem I'm having is that I can't access values from one of my
methods.

I'm trying to imitate this example from the AspectJ Language Guide in my own
code:

after(FigureElement fe, int x, int y) returning:
call(void FigureElement.setXY(int, int))
&&  target(fe)
&&  args(x, y) {
System.out.println(fe + " moved to (" + x + ", " + y + ")");
  }

This is my play program:

============================================
package test;

import ca.maximal.common.utilities.CountUtils;

public class Test01 {

    final String CLASS_NAME = getClass().getName();

    public static void main(String[] args) {

        @SuppressWarnings("unused")
        Test01 countStuff = new Test01();
    }

    public Test01() {

        String needle = "a";
        String haystack = "supercalifragilisticexpialadocious";
        int occurrences = count(needle, haystack);
        System.out.println("needle = " + needle + "; haystack = " + haystack
+ "; count = " + occurrences);
    }

    private static int count(String needle, String haystack) {

        int count = CountUtils.count(needle, haystack);

        return count;
    }
}
============================================

This is my Aspect:

============================================
package test;


/**
  * This class
  *
  *<p>More</p>
  *
  * @version 1.0
  * @since Feb 24, 2012
  */
public aspect LogCount {

    pointcut howManyNeedles() : execution(* Test01.count(..));

//    pointcut count2() : call(int count(String, String));

    /* First Before */
    before() : howManyNeedles() {
        System.out.println("About to count...."); //$NON-NLS-1$
    }

    /* First After */
    after() returning() : howManyNeedles() {
        System.out.println("...Needles counted");  //$NON-NLS-1$
    }

    /* Second After */
    after(int num, String needle, String haystack) returning:
        target(num)
&&  args(needle, haystack)
&&  call(* Test01.count(..)) {

        System.out.println("** needle = " + needle + "; haystack = " +
haystack + "; num = " + num); //$NON-NLS-1$
    }

}
============================================

The "First Before" and "First After" pointcuts work fine. But the "Second
After" resolutely refuses to work despite many different variations on the
call() statement. I've tried everything I can think of but NOTHING works.

I'm sure this is something very simple but I'm darned if I can figure it
out. I wonder if someone on this mailing list can help me see what's wrong?

--
Rhino
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users




Back to the top