Skip to content

Commit 7411681

Browse files
committed
Support no-arg HandlerMethod and new beforeExecute #150
Call beforeExecute for no-arg HandlerMethod during preHandle and refactor advice dispatch. preHandle now throws Exception and detects HandlerMethod instances (Java pattern matching); when a handler method has no parameters it invokes beforeExecute immediately. Extracted a new beforeExecute(NativeWebRequest, HandlerMethod, Object...) to centralize invoking HandlerMethodAdvice.beforeExecuteMethod for all advices, and refactored the parameter-level beforeExecute to resolve arguments and delegate to the new method. Added ArrayUtils.isEmpty import and updated the test to declare throws Exception to match the changed signature.
1 parent 88f3e0a commit 7411681

2 files changed

Lines changed: 20 additions & 10 deletions

File tree

microsphere-spring-webmvc/src/main/java/io/microsphere/spring/webmvc/method/support/InterceptingHandlerMethodProcessor.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import static io.microsphere.logging.LoggerFactory.getLogger;
5353
import static io.microsphere.spring.beans.BeanUtils.getSortedBeans;
5454
import static io.microsphere.spring.web.util.RequestAttributesUtils.getHandlerMethodArguments;
55+
import static io.microsphere.util.ArrayUtils.isEmpty;
5556

5657
/**
5758
* The {@link HandlerMethod} processor that callbacks {@link HandlerMethodAdvice} based on
@@ -166,7 +167,13 @@ public void handleReturnValue(@Nullable Object returnValue, MethodParameter retu
166167
}
167168

168169
@Override
169-
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
170+
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
171+
if (handler instanceof HandlerMethod handlerMethod) {
172+
MethodParameter[] methodParameters = handlerMethod.getMethodParameters();
173+
if (isEmpty(methodParameters)) { // No-arg Handler Method
174+
beforeExecute(new ServletWebRequest(request), handlerMethod);
175+
}
176+
}
170177
return true;
171178
}
172179

@@ -300,16 +307,19 @@ private void afterResolveArgument(MethodParameter parameter, Object argument, Me
300307

301308
private void beforeExecute(MethodParameter parameter, MethodParameterContext methodParameterContext,
302309
NativeWebRequest webRequest, Object argument) throws Exception {
310+
Object[] arguments = resolveArguments(webRequest, parameter, argument);
311+
int parameterCount = methodParameterContext.parameterCount;
312+
int parameterIndex = parameter.getParameterIndex();
313+
if (parameterIndex == parameterCount - 1) {
314+
HandlerMethod handlerMethod = methodParameterContext.method;
315+
beforeExecute(webRequest, handlerMethod, arguments);
316+
}
317+
}
318+
319+
private void beforeExecute(NativeWebRequest webRequest, HandlerMethod handlerMethod, Object... arguments) throws Exception {
303320
for (int i = 0; i < handlerMethodAdvices.size(); i++) {
304321
HandlerMethodAdvice advice = handlerMethodAdvices.get(i);
305-
int parameterCount = methodParameterContext.parameterCount;
306-
Object[] arguments = resolveArguments(webRequest, parameter, argument);
307-
308-
int parameterIndex = parameter.getParameterIndex();
309-
if (parameterIndex == parameterCount - 1) {
310-
HandlerMethod handlerMethod = methodParameterContext.method;
311-
advice.beforeExecuteMethod(handlerMethod, arguments, webRequest);
312-
}
322+
advice.beforeExecuteMethod(handlerMethod, arguments, webRequest);
313323
}
314324
}
315325

microsphere-spring-webmvc/src/test/java/io/microsphere/spring/webmvc/method/support/InterceptingHandlerMethodProcessorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ void testResolveArguments() {
150150
}
151151

152152
@Test
153-
void testPreHandle() {
153+
void testPreHandle() throws Exception {
154154
assertTrue(this.processor.preHandle(null, null, null));
155155
}
156156

0 commit comments

Comments
 (0)