4

I want to create a pointcut that matches any method in my Web controller that contains a ModelMap:

pointcut addMenu(ModelMap modelMap) : 
    execution (public String example.web.MyController.*(..)) && args (modelMap);

before(ModelMap modelMap) : addMenu(modelMap) {
    // Do stuff with modelMap...
}

My problem is that this only matches methods with ONLY the ModelMap parameter, others are not matched because they contain too many parameters. For example, this is not intercepted, due to the "req" parameter:

public String request(HttpServletRequest req, ModelMap modelMap) {
    // Handle request
}

Is there any way to match all methods with a ModelMap parameter, without having to add a pointcut delegate for every possible parameter combination?

seanhodges
  • 17,426
  • 15
  • 71
  • 93

1 Answers1

4

You can use wildcards * or .. to express the arguments in a flexible way.

pointcut addMenu(ModelMap modelMap) : 
    execution (public String example.web.MyController.*(..)) && args (*, modelMap);

See AspectJ: parameter in a pointcut

Community
  • 1
  • 1
ewernli
  • 38,045
  • 5
  • 92
  • 123
  • Thanks. That comes close to what I want, but it causes the pointcut to no longer match my "request(ModelMap modelMap)" methods, because it is expecting more than one argument. – seanhodges Feb 08 '10 at 16:32
  • Yes, that the problem related in the linked post in my answer. Either try with ".." or make two pointcuts "*, modelMap" and "modelMap". – ewernli Feb 08 '10 at 16:51