Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AspectJ pointcut to method call in specific methods

Tags:

java

aspectj

I want to create a pointcut to target a call to a method 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 to have a point cut on the call to foo() in methods bar1() and bar3()

I was thinking something like

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

before() : fooOperation() {
  //do something else
}

however, that doesnt seem to work. any ideas?

thanks

like image 644
kabal Avatar asked Jun 28 '11 00:06

kabal


People also ask

Is a method get executed when a specific Joinpoint with matching pointcut is reached in the application?

Advice: Advices are actions taken for a particular join point. In terms of programming, they are methods that get executed when a certain join point with matching pointcut is reached in the application. You can think of Advices as Struts2 interceptors or Servlet Filters.

What methods does the pointcut expression reference?

Pointcuts use class, field, constructor, and method expressions to specify the actual joinpoint that should be intercepted/watched.

What is pointcut in AspectJ?

AspectJ provides primitive pointcuts that capture join points at these times. These pointcuts use the dynamic types of their objects to pick out join points. They may also be used to expose the objects used for discrimination. this(Type or Id) target(Type or Id)

What is aspect advice pointcut Joinpoint and advice arguments in AOP?

An important term in AOP is advice. It is the action taken by an aspect at a particular join-point. Joinpoint is a point of execution of the program, such as executing a method or handling an exception. In Spring AOP, a joinpoint always represents a method execution.


2 Answers

Maybe withincode will work:

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

Alternatively you could try the cflow pointcut:

pointcut bar1(): call(* Child.bar1());
pointcut bar3(): call(* Child.bar3());

call(public void Parent.foo()) && (cflow(bar1()) || cflow(bar3());

Look here for a pointcut reference

like image 120
Dario Seidl Avatar answered Sep 29 '22 13:09

Dario Seidl


Think what you want is instead of doing the execution clauses (which have the added disadvantage of requiring additions for each new caller), is to use target, e.g. something like:

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

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

like image 37
Rob Avatar answered Sep 29 '22 13:09

Rob