Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

This() vs Target() aspectj

Can anyone explain the difference between this() and target() pointcuts in aspectj. I tried finding this elsewhere but there doesnt seem to be a clear cut answer. Thank You

like image 835
Sam Avatar asked Dec 16 '15 07:12

Sam


People also ask

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 Joint Point and pointcut?

JoinPoint: Joinpoint are points in your program execution where flow of execution got changed like Exception catching, Calling other method. PointCut: PointCut are basically those Joinpoints where you can put your advice(or call aspect). So basically PointCuts are the subset of JoinPoints.

Which method first executed in AOP?

Executing method on the target class Thus, Spring AOP injects a proxy instead of an actual instance of the target class. When we start the Spring or Spring Boot application, we see Spring executes the advice before the actual method.

What is advice in AspectJ?

AspectJ supports three kinds of advice. The kind of advice determines how it interacts with the join points it is defined over. Thus AspectJ divides advice into that which runs before its join points, that which runs after its join points, and that which runs in place of (or "around") its join points.


1 Answers

At a matching join point, this() is the object you are in, target() is the object you are invoking/referencing. The confusion may arise because in the case of an execution() pointcut matching on a joint point they are the same thing - the object containing the execution join point which matched is the same as the object running the method you are matching on. But in the case of a call() join point they are different. The object making the call is different from the object on which the method is being called.

class A {
  public void m() {
    B b = new B();
    b.n();
  }
}
class B {
  public void n() {
  }
}

For that setup, the pointcut execution(* m(..)) will match on join point A.m() and have a this() of type A and target() of type A (and they will be the same instance of A). However the pointcut call(* n(..)) will match at the call site in method A.m() where it calls n() and at that point this() will be the instance of A making the call whilst target() will be the instance of B that the method is being invoked upon.

like image 81
Andy Clement Avatar answered Sep 30 '22 05:09

Andy Clement