Support bridged methods in MvcUriComponentsBuilder
Issue: SPR-12977
This commit is contained in:
parent
9637b12f89
commit
205e5dfd6f
|
|
@ -21,6 +21,7 @@ import java.util.Arrays;
|
|||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
|
|
@ -49,6 +50,7 @@ import org.springframework.util.Assert;
|
|||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.PathMatcher;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.util.ReflectionUtils.MethodFilter;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
|
@ -56,6 +58,7 @@ import org.springframework.web.context.request.RequestAttributes;
|
|||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.method.HandlerMethodSelector;
|
||||
import org.springframework.web.method.annotation.RequestParamMethodArgumentResolver;
|
||||
import org.springframework.web.method.support.CompositeUriComponentsContributor;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
|
|
@ -412,25 +415,28 @@ public class MvcUriComponentsBuilder {
|
|||
return paths[0];
|
||||
}
|
||||
|
||||
|
||||
private static Method getMethod(Class<?> controllerType, String methodName, Object... args) {
|
||||
Method match = null;
|
||||
for (Method method : controllerType.getDeclaredMethods()) {
|
||||
if (method.getName().equals(methodName) && method.getParameterTypes().length == args.length) {
|
||||
if (match != null) {
|
||||
throw new IllegalArgumentException(String.format(
|
||||
"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;
|
||||
private static Method getMethod(Class<?> controllerType, final String methodName, final Object... args) {
|
||||
MethodFilter selector = new MethodFilter() {
|
||||
@Override
|
||||
public boolean matches(Method method) {
|
||||
String name = method.getName();
|
||||
int argLength = method.getParameterTypes().length;
|
||||
return (name.equals(methodName) && argLength == args.length);
|
||||
}
|
||||
};
|
||||
Set<Method> methods = HandlerMethodSelector.selectMethods(controllerType, selector);
|
||||
if (methods.size() == 1) {
|
||||
return methods.iterator().next();
|
||||
}
|
||||
if (match == null) {
|
||||
else if (methods.size() > 1) {
|
||||
throw new IllegalArgumentException(String.format(
|
||||
"Found two methods named '%s' accepting arguments %s in controller %s: [%s]",
|
||||
methodName, Arrays.asList(args), controllerType.getName(), methods));
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("No method named '" + methodName + "' with " + args.length +
|
||||
" arguments found in controller " + controllerType.getName());
|
||||
}
|
||||
return match;
|
||||
}
|
||||
|
||||
private static UriComponents applyContributors(UriComponentsBuilder builder, Method method, Object... args) {
|
||||
|
|
|
|||
|
|
@ -16,6 +16,10 @@
|
|||
|
||||
package org.springframework.web.servlet.mvc.method.annotation;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.*;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
|
|
@ -25,13 +29,10 @@ import java.util.Arrays;
|
|||
import java.util.List;
|
||||
|
||||
import org.hamcrest.Matchers;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.format.ISODateTimeFormat;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
|
@ -57,10 +58,6 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
|
|||
import org.springframework.web.util.UriComponents;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder}.
|
||||
*
|
||||
|
|
@ -69,6 +66,7 @@ import static org.springframework.web.servlet.mvc.method.annotation.MvcUriCompon
|
|||
* @author Rossen Stoyanchev
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public class MvcUriComponentsBuilderTests {
|
||||
|
||||
private final MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
|
|
@ -172,12 +170,12 @@ public class MvcUriComponentsBuilderTests {
|
|||
assertThat(queryParams.get("offset"), contains("10"));
|
||||
}
|
||||
|
||||
// SPR-12977
|
||||
|
||||
@Test
|
||||
// TODO [SPR-12977] Enable fromMethodNameWithBridgedMethod() test.
|
||||
@Ignore("Disabled until SPR-12977 is resolved")
|
||||
public void fromMethodNameWithBridgedMethod() throws Exception {
|
||||
UriComponents uriComponents = fromMethodName(PersonCrudController.class, "get", new Long(42)).build();
|
||||
assertThat(uriComponents.toUriString(), is("http://localhost/get/42"));
|
||||
UriComponents uriComponents = fromMethodName(PersonCrudController.class, "get", (long) 42).build();
|
||||
assertThat(uriComponents.toUriString(), is("http://localhost/42"));
|
||||
}
|
||||
|
||||
// SPR-11391
|
||||
|
|
|
|||
Loading…
Reference in New Issue