Skip to content

Commit ba6d187

Browse files
committed
Consistent MvcUriComponentsBuilder assertion handling
(cherry picked from commit 61b47ba)
1 parent 54636b3 commit ba6d187

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -104,38 +104,37 @@ public static UriComponentsBuilder fromController(Class<?> controllerType) {
104104
private static String getTypeRequestMapping(Class<?> controllerType) {
105105
Assert.notNull(controllerType, "'controllerType' must not be null");
106106
RequestMapping annot = AnnotationUtils.findAnnotation(controllerType, RequestMapping.class);
107-
if ((annot == null) || ObjectUtils.isEmpty(annot.value()) || StringUtils.isEmpty(annot.value()[0])) {
107+
if (annot == null || ObjectUtils.isEmpty(annot.value()) || StringUtils.isEmpty(annot.value()[0])) {
108108
return "/";
109109
}
110-
if (annot.value().length > 1) {
111-
if (logger.isWarnEnabled()) {
112-
logger.warn("Multiple paths on controller " + controllerType.getName() + ", using first one");
113-
}
110+
if (annot.value().length > 1 && logger.isWarnEnabled()) {
111+
logger.warn("Multiple paths on controller " + controllerType.getName() + ", using first one");
114112
}
115113
return annot.value()[0];
116114
}
117115

118116
/**
119-
* Create a {@link UriComponentsBuilder} from the mapping of a controller method
120-
* and an array of method argument values. This method delegates to
121-
* {@link #fromMethod(java.lang.reflect.Method, Object...)}.
117+
* Create a {@link UriComponentsBuilder} from the mapping of a controller
118+
* method and an array of method argument values. This method delegates
119+
* to {@link #fromMethod(java.lang.reflect.Method, Object...)}.
122120
* @param controllerType the controller
123121
* @param methodName the method name
124122
* @param argumentValues the argument values
125123
* @return a UriComponentsBuilder instance, never {@code null}
126-
* @throws IllegalStateException if there is no matching or more than one matching method
124+
* @throws IllegalArgumentException if there is no matching or
125+
* if there is more than one matching method
127126
*/
128127
public static UriComponentsBuilder fromMethodName(Class<?> controllerType, String methodName, Object... argumentValues) {
129128
Method method = getMethod(controllerType, methodName, argumentValues);
130129
return fromMethod(method, argumentValues);
131130
}
132131

133-
private static Method getMethod(Class<?> controllerType, String methodName, Object[] argumentValues) {
132+
private static Method getMethod(Class<?> controllerType, String methodName, Object... argumentValues) {
134133
Method match = null;
135134
for (Method method : controllerType.getDeclaredMethods()) {
136135
if (method.getName().equals(methodName) && method.getParameterTypes().length == argumentValues.length) {
137136
if (match != null) {
138-
throw new IllegalStateException("Found two methods named '" + methodName + "' having " +
137+
throw new IllegalArgumentException("Found two methods named '" + methodName + "' having " +
139138
Arrays.asList(argumentValues) + " arguments, controller " + controllerType.getName());
140139
}
141140
match = method;
@@ -212,19 +211,19 @@ public static UriComponentsBuilder fromMethod(Method method, Object... argumentV
212211

213212
private static String getMethodRequestMapping(Method method) {
214213
RequestMapping annot = AnnotationUtils.findAnnotation(method, RequestMapping.class);
215-
Assert.notNull(annot, "No @RequestMapping on: " + method.toGenericString());
214+
if (annot == null) {
215+
throw new IllegalArgumentException("No @RequestMapping on: " + method.toGenericString());
216+
}
216217
if (ObjectUtils.isEmpty(annot.value()) || StringUtils.isEmpty(annot.value()[0])) {
217218
return "/";
218219
}
219-
if (annot.value().length > 1) {
220-
if (logger.isWarnEnabled()) {
221-
logger.warn("Multiple paths on method " + method.toGenericString() + ", using first one");
222-
}
220+
if (annot.value().length > 1 && logger.isWarnEnabled()) {
221+
logger.warn("Multiple paths on method " + method.toGenericString() + ", using first one");
223222
}
224223
return annot.value()[0];
225224
}
226225

227-
private static UriComponents applyContributors(UriComponentsBuilder builder, Method method, Object[] args) {
226+
private static UriComponents applyContributors(UriComponentsBuilder builder, Method method, Object... args) {
228227
CompositeUriComponentsContributor contributor = getConfiguredUriComponentsContributor();
229228
if (contributor == null) {
230229
logger.debug("Using default CompositeUriComponentsContributor");
@@ -233,8 +232,10 @@ private static UriComponents applyContributors(UriComponentsBuilder builder, Met
233232

234233
int paramCount = method.getParameterTypes().length;
235234
int argCount = args.length;
236-
Assert.isTrue(paramCount == argCount, "Number of method parameters " + paramCount +
237-
" does not match number of argument values " + argCount);
235+
if (paramCount != argCount) {
236+
throw new IllegalArgumentException("Number of method parameters " + paramCount +
237+
" does not match number of argument values " + argCount);
238+
}
238239

239240
final Map<String, Object> uriVars = new HashMap<String, Object>();
240241
for (int i = 0; i < paramCount; i++) {

0 commit comments

Comments
 (0)