Lin Lin - 14 days ago 5
Java Question

AspectJ - Avoid pointcuts calling themselves indirectly

I have the following aspect:

public aspect MyAspect {

before(String val): args(val, ..) &&
call(public * execute(java.lang.String, ..)) {
// Do something
}
}


And the following classes A & B, A uses B:

class A() {
public void execute(String a) {
// Do something...
B b = new B();
b.execute();
}
}

class B {
public void execute(String a) {
// Do something
}
}


And I have a test class:

public class TestClass {
public static void main(String[] args) {
A a = new A();
a.execute("someVal"); // I want the aspect to be called for the first execute only
B b = new B();
b.execute("someVal");
}
}



  • In case we call a.execute() I do not want the execute aspect to be caught again on B.

  • On the other hand, when we call b.execute() directly from the test I do want the aspect to catch b.execute().



How can this be achieved? I tried using cflowbelow (with negation), but it didn't work.

---- edit ----

To be more precise, let's take java's FilterChain.doFilter(..) - filters can call other filters. I only want to catch the first call to doFilter (I know this can be done using ThreadLocal in case of fiter, but the filter is only an example).

Thanks,

Lin

Answer

This worked for me:

pointcut intercept_call() : call(public * execute(java.lang.String, ..));


 before(String val): intercept_call() && !cflowbelow(intercept_call()) && args(val, ..)
{

    // do soemthing
   }
}

Don't know it does what you are looking for.