Question

How can I match all methods available for a specific class?

For example I have following class hierarachy

      A
    / | \
   B  C  D
  /   |   \
 E    F    I
      |
      T

I need to match all methods that are available in T, so these are methods defined in T itself and also inherited methods from classes A, C, F.

I came to following pointcut, but not quite happy with that because it matches lots of other classes not related to T:

execution(* A+.*(..)) && this(T) 

As I understand with such pointcut aspectj will modify each method defined in classes A, B, C, D, E, F, I, T. I would like not to affect classes out of T family. It would be ideally if only T is affected (inherited methods are overridden with aspectj code). There are lots of classes and I want to minimize impact on the rest of system, i.e. I need to modify behavior only of class T and do not want these modification to affect other classes.

I could rewrite pointcut to following: (execution(* A.*(..)) || execution(* C.*(..)) || ... <all parent classes>) && this(T). But this makes me to make sure that hierarchy is correct and update it whenever actual hierarchy changes.

Thanks.

Was it helpful?

Solution

Your assumption is wrong, I just tried and investigated the byte code. The advice will only be woven into A, C, F, T because this() can be determined statically during compile time.

Update: I forgot to mention that your pointcut only matches methods derived from A and its subclasses. If you, as you say in the headline, really want to match all inherited methods, use execution(* *(..)) && this(T).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top