11

I have several implementations of SomeInterface. The question is what is the pointcut for the method executeSomething in all implementation of SomeInterface.

public class SomeImplementation implements SomeInterface {

    public String executeSomething(String parameter) {
        // Do something
    }

}

public class AnotherImplementation implements SomeInterface {

    public String executeSomething(String parameter) {
        // Do something different way
    }

}
Lenar
  • 419
  • 4
  • 19
  • 1
    Are you really interested in all implementations of the interface, wouldn't be "all classess having method with name X" be sufficient? If so you could use `@Pointcut("execution(* *.*.executeSomething(..))")`, right? – Marcin Pietraszek Dec 25 '17 at 09:22
  • "all classes having method with name X" is OK if there are no other classes (that are not implementations of the interface). Thank you for your suggestion but ideally I need all implementations – Lenar Dec 25 '17 at 18:45

1 Answers1

12

Pointcuts for that method can be either method-execution or method-call pointcuts. The most specific pointcuts for your requirement would look like this:

execution(public String SomeInterface+.executeSomething(String))
call(public String SomeInterface+.executeSomething(String))

Some explanation on these pointcut types:

  • the type pattern used in both these pointcuts mean: all public methods that return String that are defined in SomeInterface or any subtype of it, being named executeSomething and accepting a single String argument. This is the most specific type pattern that can be defined for your case and it will match only implementations of the String SomeInterface.executeSomething(String) method.
  • execution type pointcuts match join points that correspond to when a particular method body is executed
  • call type pointcuts match join points that correspond to when a particular method is called (i.e. the join point is located at the caller side)

Execution type pointcuts are used more often, but call type pointcuts are very useful too in some cases.

See The AspectJ Language/Join Points and Pointcuts chapter in the AspectJ Programming Guide for further reference.

Nándor Előd Fekete
  • 6,988
  • 1
  • 22
  • 47