AspectJ points to method invocation by specific methods

I want to create a pointcut to call a method call from specific methods.

take the following:

class Parent { public foo() { //do something } } class Child extends Parent { public bar1() { foo(); } public bar2() { foo(); } public bar3() { foo(); } } 

I would like the call point in foo () in the methods bar1 () and bar3 ()

I thought something like

 pointcut fooOperation(): call(public void Parent.foo() && (execution(* Child.bar1()) || execution(* Child.bar3()) ); before() : fooOperation() { //do something else } 

however, it does not seem to work. any ideas?

thanks

+6
source share
2 answers

Maybe withincode will work:

 call(public void Parent.foo()) && (withincode(* Child.bar1()) || withincode(* Child.bar3()) ); 

Alternatively, you can try pointcut cflow :

 pointcut bar1(): call(* Child.bar1()); pointcut bar3(): call(* Child.bar3()); call(public void Parent.foo()) && (cflow(bar1()) || cflow(bar3()); 

Look here for a pointcut link

+3
source

Think about what you want, instead of fulfilling the fulfillment conditions (which have an additional drawback requiring adding for each new caller), is to use a goal, for example. sort of:

 target(Child) && call(public void Parent.foo()). 

Somewhat surprisingly, I found the pointcut links in the eclipse documentation quite useful. They are here .

+2
source

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


All Articles