|
From: Juergen H. <jho...@us...> - 2008-08-18 12:30:27
|
Update of /cvsroot/springframework/spring/tiger/src/org/springframework/web/bind/annotation/support In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv12569/tiger/src/org/springframework/web/bind/annotation/support Modified Files: HandlerMethodInvoker.java Added Files: HandlerMethodInvocationException.java Log Message: standard method argument subtypes (such as a Principal subclass) fail with a descriptive exception if incompatible Index: HandlerMethodInvoker.java =================================================================== RCS file: /cvsroot/springframework/spring/tiger/src/org/springframework/web/bind/annotation/support/HandlerMethodInvoker.java,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** HandlerMethodInvoker.java 7 Aug 2008 11:00:43 -0000 1.11 --- HandlerMethodInvoker.java 18 Aug 2008 12:30:23 -0000 1.12 *************** *** 36,39 **** --- 36,40 ---- import org.springframework.ui.ExtendedModelMap; import org.springframework.ui.Model; + import org.springframework.util.ClassUtils; import org.springframework.util.ReflectionUtils; import org.springframework.validation.BindingResult; *************** *** 111,134 **** throws Exception { ! boolean debug = logger.isDebugEnabled(); ! for (Method attributeMethod : this.methodResolver.getModelAttributeMethods()) { ! Object[] args = resolveHandlerArguments(attributeMethod, handler, webRequest, implicitModel); ! if (debug) { ! logger.debug("Invoking model attribute method: " + attributeMethod); } ! Object attrValue = doInvokeMethod(attributeMethod, handler, args); ! String attrName = AnnotationUtils.findAnnotation(attributeMethod, ModelAttribute.class).value(); ! if ("".equals(attrName)) { ! Class resolvedType = GenericTypeResolver.resolveReturnType(attributeMethod, handler.getClass()); ! attrName = Conventions.getVariableNameForReturnType(attributeMethod, resolvedType, attrValue); } ! implicitModel.addAttribute(attrName, attrValue); } ! ! Object[] args = resolveHandlerArguments(handlerMethod, handler, webRequest, implicitModel); ! if (debug) { ! logger.debug("Invoking request handler method: " + handlerMethod); } - return doInvokeMethod(handlerMethod, handler, args); } --- 112,141 ---- throws Exception { ! try { ! boolean debug = logger.isDebugEnabled(); ! for (Method attributeMethod : this.methodResolver.getModelAttributeMethods()) { ! Object[] args = resolveHandlerArguments(attributeMethod, handler, webRequest, implicitModel); ! if (debug) { ! logger.debug("Invoking model attribute method: " + attributeMethod); ! } ! Object attrValue = doInvokeMethod(attributeMethod, handler, args); ! String attrName = AnnotationUtils.findAnnotation(attributeMethod, ModelAttribute.class).value(); ! if ("".equals(attrName)) { ! Class resolvedType = GenericTypeResolver.resolveReturnType(attributeMethod, handler.getClass()); ! attrName = Conventions.getVariableNameForReturnType(attributeMethod, resolvedType, attrValue); ! } ! implicitModel.addAttribute(attrName, attrValue); } ! ! Object[] args = resolveHandlerArguments(handlerMethod, handler, webRequest, implicitModel); ! if (debug) { ! logger.debug("Invoking request handler method: " + handlerMethod); } ! return doInvokeMethod(handlerMethod, handler, args); } ! catch (IllegalStateException ex) { ! // Throw exception with full handler method context... ! throw new HandlerMethodInvocationException(handlerMethod, ex); } } *************** *** 445,448 **** --- 452,456 ---- throws Exception { + // Invoke custom argument resolvers if present... if (this.customArgumentResolvers != null) { for (WebArgumentResolver argumentResolver : this.customArgumentResolvers) { *************** *** 453,457 **** } } ! return resolveStandardArgument(methodParameter.getParameterType(), webRequest); } --- 461,474 ---- } } ! ! // Resolution of standard parameter types... ! Class paramType = methodParameter.getParameterType(); ! Object value = resolveStandardArgument(paramType, webRequest); ! if (value != WebArgumentResolver.UNRESOLVED && !ClassUtils.isAssignableValue(paramType, value)) { ! throw new IllegalStateException("Standard argument type [" + paramType.getName() + ! "] resolved to incompatible value of type [" + (value != null ? value.getClass() : null) + ! "]. Consider declaring the argument type in a less specific fashion."); ! } ! return value; } --- NEW FILE: HandlerMethodInvocationException.java --- /* * Copyright 2002-2008 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.web.bind.annotation.support; import java.lang.reflect.Method; import org.springframework.core.NestedRuntimeException; /** * Exception indicating that the execution of an annotated MVC handler method failed. * * @author Juergen Hoeller * @since 2.5.6 * @see HandlerMethodInvoker#invokeHandlerMethod */ public class HandlerMethodInvocationException extends NestedRuntimeException { /** * Create a new HandlerMethodInvocationException for the given Method handle and cause. * @param handlerMethod the handler method handle * @param cause the cause of the invocation failure */ public HandlerMethodInvocationException(Method handlerMethod, Throwable cause) { super("Failed to invoke handler method [" + handlerMethod + "]", cause); } } |