views:

41

answers:

1

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?

+1  A: 

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 http://stackoverflow.com/questions/265680/aspectj-parameter-in-a-pointcut

ewernli
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
Yes, that the problem related in the linked post in my answer. Either try with ".." or make two pointcuts "*, modelMap" and "modelMap".
ewernli
Making the 2 pointcuts works, thanks.
seanhodges