2011-05-05

AspectJ: this() vs. target()

this() and target() are AspectJ poincuts for collecting context. What is the difference between them?

this()


That is the Java this object at the matching point.

target()


That is the Java object on which the method is called (or the object whose field is accessed).


… So, what’s the difference in practice? It turns out, it depends on the execution model you use - whether it’s execution or call.

Execution example


execution( void Dog.bark() ) && this( thisObject ) && target( targetObject )
thisObject will be a dog instance. targetObject will be the same dog instance. (If you’re wondering why, it depends on how AspectJ works under the hood - for execution it builds in your advice into the advised method.)

Call example


call( void Dog.bark() ) && this( thisObject ) && target( targetObject )
thisObject will be the instance that called dog.bark(). It could be any class that made this call. targetObject will, still, be a dog instance. (If you’re wondering why, it depends on how AspectJ works under the hood - for call it builds in your advice at the method call, not inside it.)

No comments:

Post a Comment