Browse Source

Consistent MvcUriComponentsBuilder assertion handling

pull/493/merge
Juergen Hoeller 12 years ago
parent
commit
61b47ba7d9
  1. 51
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java

51
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) { private static String getTypeRequestMapping(Class<?> controllerType) {
Assert.notNull(controllerType, "'controllerType' must not be null"); Assert.notNull(controllerType, "'controllerType' must not be null");
RequestMapping annot = AnnotationUtils.findAnnotation(controllerType, RequestMapping.class); 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 "/"; return "/";
} }
if (annot.value().length > 1) { if (annot.value().length > 1 && logger.isWarnEnabled()) {
if (logger.isWarnEnabled()) { logger.warn("Multiple paths on controller " + controllerType.getName() + ", using first one");
logger.warn("Multiple paths on controller " + controllerType.getName() + ", using first one");
}
} }
return annot.value()[0]; return annot.value()[0];
} }
/** /**
* Create a {@link UriComponentsBuilder} from the mapping of a controller method * Create a {@link UriComponentsBuilder} from the mapping of a controller
* and an array of method argument values. This method delegates to * method and an array of method argument values. This method delegates
* {@link #fromMethod(java.lang.reflect.Method, Object...)}. * to {@link #fromMethod(java.lang.reflect.Method, Object...)}.
* @param controllerType the controller * @param controllerType the controller
* @param methodName the method name * @param methodName the method name
* @param argumentValues the argument values * @param argumentValues the argument values
* @return a UriComponentsBuilder instance, never {@code null} * @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) { public static UriComponentsBuilder fromMethodName(Class<?> controllerType, String methodName, Object... argumentValues) {
Method method = getMethod(controllerType, methodName, argumentValues); Method method = getMethod(controllerType, methodName, argumentValues);
return fromMethod(method, 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; Method match = null;
for (Method method : controllerType.getDeclaredMethods()) { for (Method method : controllerType.getDeclaredMethods()) {
if (method.getName().equals(methodName) && method.getParameterTypes().length == argumentValues.length) { if (method.getName().equals(methodName) && method.getParameterTypes().length == argumentValues.length) {
if (match != null) { 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()); Arrays.asList(argumentValues) + " arguments, controller " + controllerType.getName());
} }
match = method; match = method;
@ -212,13 +211,19 @@ public class MvcUriComponentsBuilder extends UriComponentsBuilder {
* are important for {@code @RequestParam} and {@code @PathVariable} arguments * are important for {@code @RequestParam} and {@code @PathVariable} arguments
* but may be passed as {@code null} otherwise. * but may be passed as {@code null} otherwise.
* @return the UriComponentsBuilder * @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) { public static UriComponentsBuilder fromMappingName(String name, Object... argumentValues) {
RequestMappingInfoHandlerMapping hm = getRequestMappingInfoHandlerMapping(); RequestMappingInfoHandlerMapping hm = getRequestMappingInfoHandlerMapping();
List<HandlerMethod> handlerMethods = hm.getHandlerMethodsForMappingName(name); List<HandlerMethod> handlerMethods = hm.getHandlerMethodsForMappingName(name);
Assert.state(handlerMethods != null, "Mapping name not found: " + name); if (handlerMethods == null) {
Assert.state(handlerMethods.size() == 1, "No unique match for mapping name " + name + ": " + handlerMethods); 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); return fromMethod(handlerMethods.get(0).getMethod(), argumentValues);
} }
@ -245,19 +250,19 @@ public class MvcUriComponentsBuilder extends UriComponentsBuilder {
private static String getMethodRequestMapping(Method method) { private static String getMethodRequestMapping(Method method) {
RequestMapping annot = AnnotationUtils.findAnnotation(method, RequestMapping.class); 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])) { if (ObjectUtils.isEmpty(annot.value()) || StringUtils.isEmpty(annot.value()[0])) {
return "/"; return "/";
} }
if (annot.value().length > 1) { if (annot.value().length > 1 && logger.isWarnEnabled()) {
if (logger.isWarnEnabled()) { logger.warn("Multiple paths on method " + method.toGenericString() + ", using first one");
logger.warn("Multiple paths on method " + method.toGenericString() + ", using first one");
}
} }
return annot.value()[0]; 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(); CompositeUriComponentsContributor contributor = getConfiguredUriComponentsContributor();
if (contributor == null) { if (contributor == null) {
logger.debug("Using default CompositeUriComponentsContributor"); logger.debug("Using default CompositeUriComponentsContributor");
@ -266,8 +271,10 @@ public class MvcUriComponentsBuilder extends UriComponentsBuilder {
int paramCount = method.getParameterTypes().length; int paramCount = method.getParameterTypes().length;
int argCount = args.length; int argCount = args.length;
Assert.isTrue(paramCount == argCount, "Number of method parameters " + paramCount + if (paramCount != argCount) {
" does not match number of argument values " + argCount); throw new IllegalArgumentException("Number of method parameters " + paramCount +
" does not match number of argument values " + argCount);
}
final Map<String, Object> uriVars = new HashMap<String, Object>(); final Map<String, Object> uriVars = new HashMap<String, Object>();
for (int i = 0; i < paramCount; i++) { for (int i = 0; i < paramCount; i++) {

Loading…
Cancel
Save