Improve ex. msg for duplicate methods in MvcUriComponentsBuilder

This commit adds additional context to the exception message generated
when two candidate methods are discovered in MvcUriComponentsBuilder's
getMethod(Class<?>, String, Object...).

Issue: SPR-12977
This commit is contained in:
Sam Brannen 2015-05-04 13:43:04 +02:00
parent 859751b9d9
commit e9c4db34ec
1 changed files with 9 additions and 6 deletions

View File

@ -81,6 +81,7 @@ import org.springframework.web.util.UriComponentsBuilder;
* *
* @author Oliver Gierke * @author Oliver Gierke
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @author Sam Brannen
* @since 4.0 * @since 4.0
*/ */
public class MvcUriComponentsBuilder { public class MvcUriComponentsBuilder {
@ -218,11 +219,11 @@ public class MvcUriComponentsBuilder {
* // Inline style with static import of "MvcUriComponentsBuilder.on" * // Inline style with static import of "MvcUriComponentsBuilder.on"
* *
* MvcUriComponentsBuilder.fromMethodCall( * MvcUriComponentsBuilder.fromMethodCall(
* on(CustomerController.class).showAddresses("US")).buildAndExpand(1); * on(AddressController.class).getAddressesForCountry("US")).buildAndExpand(1);
* *
* // Longer form useful for repeated invocation (and void controller methods) * // Longer form useful for repeated invocation (and void controller methods)
* *
* CustomerController controller = MvcUriComponentsBuilder.on(CustomController.class); * AddressController controller = MvcUriComponentsBuilder.on(AddressController.class);
* controller.addAddress(null); * controller.addAddress(null);
* builder = MvcUriComponentsBuilder.fromMethodCall(controller); * builder = MvcUriComponentsBuilder.fromMethodCall(controller);
* controller.getAddressesForCountry("US") * controller.getAddressesForCountry("US")
@ -417,15 +418,17 @@ public class MvcUriComponentsBuilder {
for (Method method : controllerType.getDeclaredMethods()) { for (Method method : controllerType.getDeclaredMethods()) {
if (method.getName().equals(methodName) && method.getParameterTypes().length == args.length) { if (method.getName().equals(methodName) && method.getParameterTypes().length == args.length) {
if (match != null) { if (match != null) {
throw new IllegalArgumentException("Found two methods named '" + methodName + "' having " + throw new IllegalArgumentException(String.format(
Arrays.asList(args) + " arguments, controller " + controllerType.getName()); "Found two methods named '%s' accepting arguments %s in controller %s: [%s] and [%s]",
methodName, Arrays.asList(args), controllerType.getName(), match.toGenericString(),
method.toGenericString()));
} }
match = method; match = method;
} }
} }
if (match == null) { if (match == null) {
throw new IllegalArgumentException("No method '" + methodName + "' with " + args.length + throw new IllegalArgumentException("No method named '" + methodName + "' with " + args.length +
" parameters found in " + controllerType.getName()); " arguments found in controller " + controllerType.getName());
} }
return match; return match;
} }