Fixed accidental use of Java 8 getParameterCount(), plus polishing of related classes

Issue: SPR-11245
This commit is contained in:
Juergen Hoeller
2013-12-18 18:08:32 +01:00
parent 234272eb8f
commit 260bbe319d
5 changed files with 114 additions and 109 deletions

View File

@@ -18,7 +18,6 @@ package org.springframework.web.method.annotation;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.servlet.ServletException;
import org.springframework.beans.factory.config.BeanExpressionContext;
@@ -61,8 +60,13 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle
private final BeanExpressionContext expressionContext;
private Map<MethodParameter, NamedValueInfo> namedValueInfoCache =
new ConcurrentHashMap<MethodParameter, NamedValueInfo>(256);
private Map<MethodParameter, NamedValueInfo> namedValueInfoCache = new ConcurrentHashMap<MethodParameter, NamedValueInfo>(256);
public AbstractNamedValueMethodArgumentResolver() {
this.configurableBeanFactory = null;
this.expressionContext = null;
}
/**
* @param beanFactory a bean factory to use for resolving ${...} placeholder
@@ -71,14 +75,13 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle
*/
public AbstractNamedValueMethodArgumentResolver(ConfigurableBeanFactory beanFactory) {
this.configurableBeanFactory = beanFactory;
this.expressionContext = (beanFactory != null) ? new BeanExpressionContext(beanFactory, new RequestScope()) : null;
this.expressionContext = (beanFactory != null ? new BeanExpressionContext(beanFactory, new RequestScope()) : null);
}
@Override
public final Object resolveArgument(
MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory)
throws Exception {
public final Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
Class<?> paramType = parameter.getParameterType();
NamedValueInfo namedValueInfo = getNamedValueInfo(parameter);
@@ -217,7 +220,7 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle
private final String defaultValue;
protected NamedValueInfo(String name, boolean required, String defaultValue) {
public NamedValueInfo(String name, boolean required, String defaultValue) {
this.name = name;
this.required = required;
this.defaultValue = defaultValue;

View File

@@ -21,7 +21,6 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
@@ -80,35 +79,45 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod
private final boolean useDefaultResolution;
/**
* @param useDefaultResolution in default resolution mode a method argument
* that is a simple type, as defined in {@link BeanUtils#isSimpleProperty},
* is treated as a request parameter even if it it isn't annotated, the
* request parameter name is derived from the method parameter name.
*/
public RequestParamMethodArgumentResolver(boolean useDefaultResolution) {
this.useDefaultResolution = useDefaultResolution;
}
/**
* @param beanFactory a bean factory used for resolving ${...} placeholder
* and #{...} SpEL expressions in default values, or {@code null} if default
* values are not expected to contain expressions
* @param useDefaultResolution in default resolution mode a method argument
* that is a simple type, as defined in {@link BeanUtils#isSimpleProperty},
* is treated as a request parameter even if it itsn't annotated, the
* is treated as a request parameter even if it it isn't annotated, the
* request parameter name is derived from the method parameter name.
*/
public RequestParamMethodArgumentResolver(ConfigurableBeanFactory beanFactory,
boolean useDefaultResolution) {
public RequestParamMethodArgumentResolver(ConfigurableBeanFactory beanFactory, boolean useDefaultResolution) {
super(beanFactory);
this.useDefaultResolution = useDefaultResolution;
}
/**
* Supports the following:
* <ul>
* <li>@RequestParam-annotated method arguments.
* This excludes {@link Map} params where the annotation doesn't
* specify a name. See {@link RequestParamMapMethodArgumentResolver}
* instead for such params.
* <li>Arguments of type {@link MultipartFile}
* unless annotated with @{@link RequestPart}.
* <li>Arguments of type {@code javax.servlet.http.Part}
* unless annotated with @{@link RequestPart}.
* <li>In default resolution mode, simple type arguments
* even if not with @{@link RequestParam}.
* <li>@RequestParam-annotated method arguments.
* This excludes {@link Map} params where the annotation doesn't
* specify a name. See {@link RequestParamMapMethodArgumentResolver}
* instead for such params.
* <li>Arguments of type {@link MultipartFile}
* unless annotated with @{@link RequestPart}.
* <li>Arguments of type {@code javax.servlet.http.Part}
* unless annotated with @{@link RequestPart}.
* <li>In default resolution mode, simple type arguments
* even if not with @{@link RequestParam}.
* </ul>
*/
@Override
@@ -149,7 +158,6 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod
@Override
protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest webRequest) throws Exception {
Object arg;
HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
@@ -243,9 +251,9 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod
builder.queryParam(name);
}
else if (value instanceof Collection) {
for (Object v : (Collection<?>) value) {
v = formatUriValue(conversionService, TypeDescriptor.nested(parameter, 1), v);
builder.queryParam(name, v);
for (Object element : (Collection<?>) value) {
element = formatUriValue(conversionService, TypeDescriptor.nested(parameter, 1), element);
builder.queryParam(name, element);
}
}
else {
@@ -254,18 +262,17 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod
}
protected String formatUriValue(ConversionService cs, TypeDescriptor sourceType, Object value) {
return (cs != null) ?
(String) cs.convert(value, sourceType, STRING_TYPE_DESCRIPTOR) : null;
return (cs != null ? (String) cs.convert(value, sourceType, STRING_TYPE_DESCRIPTOR) : null);
}
private class RequestParamNamedValueInfo extends NamedValueInfo {
private static class RequestParamNamedValueInfo extends NamedValueInfo {
private RequestParamNamedValueInfo() {
public RequestParamNamedValueInfo() {
super("", false, ValueConstants.DEFAULT_NONE);
}
private RequestParamNamedValueInfo(RequestParam annotation) {
public RequestParamNamedValueInfo(RequestParam annotation) {
super(annotation.value(), annotation.required(), annotation.defaultValue());
}
}

View File

@@ -16,17 +16,18 @@
package org.springframework.web.method.support;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.springframework.core.MethodParameter;
import org.springframework.core.convert.ConversionService;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.util.Assert;
import org.springframework.web.util.UriComponentsBuilder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
* A {@link UriComponentsContributor} containing a list of other contributors
* to delegate and also encapsulating a specific {@link ConversionService} to
@@ -37,7 +38,7 @@ import java.util.Map;
*/
public class CompositeUriComponentsContributor implements UriComponentsContributor {
private final List<UriComponentsContributor> contributors = new ArrayList<UriComponentsContributor>();
private final List<UriComponentsContributor> contributors = new LinkedList<UriComponentsContributor>();
private final ConversionService conversionService;
@@ -46,10 +47,24 @@ public class CompositeUriComponentsContributor implements UriComponentsContribut
* Create an instance from a collection of {@link UriComponentsContributor}s or
* {@link HandlerMethodArgumentResolver}s. Since both of these tend to be implemented
* by the same class, the most convenient option is to obtain the configured
* {@code HandlerMethodArgumentResolvers} in {@code RequestMappingHandlerAdapter} and
* provide that to this constructor.
* {@code HandlerMethodArgumentResolvers} in {@code RequestMappingHandlerAdapter}
* and provide that to this constructor.
* @param contributors a collection of {@link UriComponentsContributor}
* or {@link HandlerMethodArgumentResolver}s.
* or {@link HandlerMethodArgumentResolver}s.
*/
public CompositeUriComponentsContributor(UriComponentsContributor... contributors) {
Collections.addAll(this.contributors, contributors);
this.conversionService = new DefaultFormattingConversionService();
}
/**
* Create an instance from a collection of {@link UriComponentsContributor}s or
* {@link HandlerMethodArgumentResolver}s. Since both of these tend to be implemented
* by the same class, the most convenient option is to obtain the configured
* {@code HandlerMethodArgumentResolvers} in {@code RequestMappingHandlerAdapter}
* and provide that to this constructor.
* @param contributors a collection of {@link UriComponentsContributor}
* or {@link HandlerMethodArgumentResolver}s.
*/
public CompositeUriComponentsContributor(Collection<?> contributors) {
this(contributors, null);
@@ -70,17 +85,14 @@ public class CompositeUriComponentsContributor implements UriComponentsContribut
* need to be formatted as Strings before being added to the URI
*/
public CompositeUriComponentsContributor(Collection<?> contributors, ConversionService conversionService) {
Assert.notNull(contributors, "'uriComponentsContributors' must not be null");
for (Object contributor : contributors) {
if (contributor instanceof UriComponentsContributor) {
this.contributors.add((UriComponentsContributor) contributor);
}
}
this.conversionService = (conversionService != null) ?
conversionService : new DefaultFormattingConversionService();
this.conversionService =
(conversionService != null ? conversionService : new DefaultFormattingConversionService());
}
@@ -90,8 +102,8 @@ public class CompositeUriComponentsContributor implements UriComponentsContribut
@Override
public boolean supportsParameter(MethodParameter parameter) {
for (UriComponentsContributor c : this.contributors) {
if (c.supportsParameter(parameter)) {
for (UriComponentsContributor contributor : this.contributors) {
if (contributor.supportsParameter(parameter)) {
return true;
}
}
@@ -102,9 +114,9 @@ public class CompositeUriComponentsContributor implements UriComponentsContribut
public void contributeMethodArgument(MethodParameter parameter, Object value,
UriComponentsBuilder builder, Map<String, Object> uriVariables, ConversionService conversionService) {
for (UriComponentsContributor c : this.contributors) {
if (c.supportsParameter(parameter)) {
c.contributeMethodArgument(parameter, value, builder, uriVariables, conversionService);
for (UriComponentsContributor contributor : this.contributors) {
if (contributor.supportsParameter(parameter)) {
contributor.contributeMethodArgument(parameter, value, builder, uriVariables, conversionService);
break;
}
}