AspectJ - Avoid items 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 and 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 don't want the execution aspect to be detected again on B.
  • On the other hand, when we call b.execute () directly from the test, I want this aspect to catch b.execute ().

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

---- edit ----

, java FilterChain.doFilter(..) - . first doFilter ( , ThreadLocal fiter, - ).

,

+4
1

:

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


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

    // do soemthing
   }
}

, , .

+3

Source: https://habr.com/ru/post/1661835/


All Articles