diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java index ef02c059161..b10566312dd 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java @@ -108,38 +108,37 @@ public class MvcUriComponentsBuilder extends UriComponentsBuilder { private static String getTypeRequestMapping(Class controllerType) { Assert.notNull(controllerType, "'controllerType' must not be null"); RequestMapping annot = AnnotationUtils.findAnnotation(controllerType, RequestMapping.class); - if ((annot == null) || ObjectUtils.isEmpty(annot.value()) || StringUtils.isEmpty(annot.value()[0])) { + if (annot == null || ObjectUtils.isEmpty(annot.value()) || StringUtils.isEmpty(annot.value()[0])) { return "/"; } - if (annot.value().length > 1) { - if (logger.isWarnEnabled()) { - logger.warn("Multiple paths on controller " + controllerType.getName() + ", using first one"); - } + if (annot.value().length > 1 && logger.isWarnEnabled()) { + logger.warn("Multiple paths on controller " + controllerType.getName() + ", using first one"); } return annot.value()[0]; } /** - * Create a {@link UriComponentsBuilder} from the mapping of a controller method - * and an array of method argument values. This method delegates to - * {@link #fromMethod(java.lang.reflect.Method, Object...)}. + * Create a {@link UriComponentsBuilder} from the mapping of a controller + * method and an array of method argument values. This method delegates + * to {@link #fromMethod(java.lang.reflect.Method, Object...)}. * @param controllerType the controller * @param methodName the method name * @param argumentValues the argument values * @return a UriComponentsBuilder instance, never {@code null} - * @throws IllegalStateException if there is no matching or more than one matching method + * @throws IllegalArgumentException if there is no matching or + * if there is more than one matching method */ public static UriComponentsBuilder fromMethodName(Class controllerType, String methodName, Object... argumentValues) { Method method = getMethod(controllerType, methodName, argumentValues); return fromMethod(method, argumentValues); } - private static Method getMethod(Class controllerType, String methodName, Object[] argumentValues) { + private static Method getMethod(Class controllerType, String methodName, Object... argumentValues) { Method match = null; for (Method method : controllerType.getDeclaredMethods()) { if (method.getName().equals(methodName) && method.getParameterTypes().length == argumentValues.length) { if (match != null) { - throw new IllegalStateException("Found two methods named '" + methodName + "' having " + + throw new IllegalArgumentException("Found two methods named '" + methodName + "' having " + Arrays.asList(argumentValues) + " arguments, controller " + controllerType.getName()); } match = method; @@ -212,13 +211,19 @@ public class MvcUriComponentsBuilder extends UriComponentsBuilder { * are important for {@code @RequestParam} and {@code @PathVariable} arguments * but may be passed as {@code null} otherwise. * @return the UriComponentsBuilder - * @throws IllegalStateException if the mapping name is not found or there is no unique match + * @throws IllegalArgumentException if the mapping name is not found or + * if there is no unique match + * @since 4.1 */ public static UriComponentsBuilder fromMappingName(String name, Object... argumentValues) { RequestMappingInfoHandlerMapping hm = getRequestMappingInfoHandlerMapping(); List handlerMethods = hm.getHandlerMethodsForMappingName(name); - Assert.state(handlerMethods != null, "Mapping name not found: " + name); - Assert.state(handlerMethods.size() == 1, "No unique match for mapping name " + name + ": " + handlerMethods); + if (handlerMethods == null) { + throw new IllegalArgumentException("Mapping name not found: " + name); + } + if (handlerMethods.size() != 1) { + throw new IllegalArgumentException("No unique match for mapping name " + name + ": " + handlerMethods); + } return fromMethod(handlerMethods.get(0).getMethod(), argumentValues); } @@ -245,19 +250,19 @@ public class MvcUriComponentsBuilder extends UriComponentsBuilder { private static String getMethodRequestMapping(Method method) { RequestMapping annot = AnnotationUtils.findAnnotation(method, RequestMapping.class); - Assert.notNull(annot, "No @RequestMapping on: " + method.toGenericString()); + if (annot == null) { + throw new IllegalArgumentException("No @RequestMapping on: " + method.toGenericString()); + } if (ObjectUtils.isEmpty(annot.value()) || StringUtils.isEmpty(annot.value()[0])) { return "/"; } - if (annot.value().length > 1) { - if (logger.isWarnEnabled()) { - logger.warn("Multiple paths on method " + method.toGenericString() + ", using first one"); - } + if (annot.value().length > 1 && logger.isWarnEnabled()) { + logger.warn("Multiple paths on method " + method.toGenericString() + ", using first one"); } return annot.value()[0]; } - private static UriComponents applyContributors(UriComponentsBuilder builder, Method method, Object[] args) { + private static UriComponents applyContributors(UriComponentsBuilder builder, Method method, Object... args) { CompositeUriComponentsContributor contributor = getConfiguredUriComponentsContributor(); if (contributor == null) { logger.debug("Using default CompositeUriComponentsContributor"); @@ -266,8 +271,10 @@ public class MvcUriComponentsBuilder extends UriComponentsBuilder { int paramCount = method.getParameterTypes().length; int argCount = args.length; - Assert.isTrue(paramCount == argCount, "Number of method parameters " + paramCount + - " does not match number of argument values " + argCount); + if (paramCount != argCount) { + throw new IllegalArgumentException("Number of method parameters " + paramCount + + " does not match number of argument values " + argCount); + } final Map uriVars = new HashMap(); for (int i = 0; i < paramCount; i++) {