Skip to content

Commit 260bbe3

Browse files
committed
Fixed accidental use of Java 8 getParameterCount(), plus polishing of related classes
Issue: SPR-11245
1 parent 234272e commit 260bbe3

File tree

5 files changed

+114
-109
lines changed

5 files changed

+114
-109
lines changed

spring-web/src/main/java/org/springframework/web/method/annotation/AbstractNamedValueMethodArgumentResolver.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.util.Map;
2020
import java.util.concurrent.ConcurrentHashMap;
21-
2221
import javax.servlet.ServletException;
2322

2423
import org.springframework.beans.factory.config.BeanExpressionContext;
@@ -61,8 +60,13 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle
6160

6261
private final BeanExpressionContext expressionContext;
6362

64-
private Map<MethodParameter, NamedValueInfo> namedValueInfoCache =
65-
new ConcurrentHashMap<MethodParameter, NamedValueInfo>(256);
63+
private Map<MethodParameter, NamedValueInfo> namedValueInfoCache = new ConcurrentHashMap<MethodParameter, NamedValueInfo>(256);
64+
65+
66+
public AbstractNamedValueMethodArgumentResolver() {
67+
this.configurableBeanFactory = null;
68+
this.expressionContext = null;
69+
}
6670

6771
/**
6872
* @param beanFactory a bean factory to use for resolving ${...} placeholder
@@ -71,14 +75,13 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle
7175
*/
7276
public AbstractNamedValueMethodArgumentResolver(ConfigurableBeanFactory beanFactory) {
7377
this.configurableBeanFactory = beanFactory;
74-
this.expressionContext = (beanFactory != null) ? new BeanExpressionContext(beanFactory, new RequestScope()) : null;
78+
this.expressionContext = (beanFactory != null ? new BeanExpressionContext(beanFactory, new RequestScope()) : null);
7579
}
7680

81+
7782
@Override
78-
public final Object resolveArgument(
79-
MethodParameter parameter, ModelAndViewContainer mavContainer,
80-
NativeWebRequest webRequest, WebDataBinderFactory binderFactory)
81-
throws Exception {
83+
public final Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
84+
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
8285

8386
Class<?> paramType = parameter.getParameterType();
8487
NamedValueInfo namedValueInfo = getNamedValueInfo(parameter);
@@ -217,7 +220,7 @@ protected static class NamedValueInfo {
217220

218221
private final String defaultValue;
219222

220-
protected NamedValueInfo(String name, boolean required, String defaultValue) {
223+
public NamedValueInfo(String name, boolean required, String defaultValue) {
221224
this.name = name;
222225
this.required = required;
223226
this.defaultValue = defaultValue;

spring-web/src/main/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolver.java

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.Collection;
2222
import java.util.List;
2323
import java.util.Map;
24-
2524
import javax.servlet.ServletException;
2625
import javax.servlet.http.HttpServletRequest;
2726

@@ -80,35 +79,45 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod
8079

8180
private final boolean useDefaultResolution;
8281

82+
83+
/**
84+
* @param useDefaultResolution in default resolution mode a method argument
85+
* that is a simple type, as defined in {@link BeanUtils#isSimpleProperty},
86+
* is treated as a request parameter even if it it isn't annotated, the
87+
* request parameter name is derived from the method parameter name.
88+
*/
89+
public RequestParamMethodArgumentResolver(boolean useDefaultResolution) {
90+
this.useDefaultResolution = useDefaultResolution;
91+
}
92+
8393
/**
8494
* @param beanFactory a bean factory used for resolving ${...} placeholder
8595
* and #{...} SpEL expressions in default values, or {@code null} if default
8696
* values are not expected to contain expressions
8797
* @param useDefaultResolution in default resolution mode a method argument
8898
* that is a simple type, as defined in {@link BeanUtils#isSimpleProperty},
89-
* is treated as a request parameter even if it itsn't annotated, the
99+
* is treated as a request parameter even if it it isn't annotated, the
90100
* request parameter name is derived from the method parameter name.
91101
*/
92-
public RequestParamMethodArgumentResolver(ConfigurableBeanFactory beanFactory,
93-
boolean useDefaultResolution) {
94-
102+
public RequestParamMethodArgumentResolver(ConfigurableBeanFactory beanFactory, boolean useDefaultResolution) {
95103
super(beanFactory);
96104
this.useDefaultResolution = useDefaultResolution;
97105
}
98106

107+
99108
/**
100109
* Supports the following:
101110
* <ul>
102-
* <li>@RequestParam-annotated method arguments.
103-
* This excludes {@link Map} params where the annotation doesn't
104-
* specify a name. See {@link RequestParamMapMethodArgumentResolver}
105-
* instead for such params.
106-
* <li>Arguments of type {@link MultipartFile}
107-
* unless annotated with @{@link RequestPart}.
108-
* <li>Arguments of type {@code javax.servlet.http.Part}
109-
* unless annotated with @{@link RequestPart}.
110-
* <li>In default resolution mode, simple type arguments
111-
* even if not with @{@link RequestParam}.
111+
* <li>@RequestParam-annotated method arguments.
112+
* This excludes {@link Map} params where the annotation doesn't
113+
* specify a name. See {@link RequestParamMapMethodArgumentResolver}
114+
* instead for such params.
115+
* <li>Arguments of type {@link MultipartFile}
116+
* unless annotated with @{@link RequestPart}.
117+
* <li>Arguments of type {@code javax.servlet.http.Part}
118+
* unless annotated with @{@link RequestPart}.
119+
* <li>In default resolution mode, simple type arguments
120+
* even if not with @{@link RequestParam}.
112121
* </ul>
113122
*/
114123
@Override
@@ -149,7 +158,6 @@ protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) {
149158

150159
@Override
151160
protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest webRequest) throws Exception {
152-
153161
Object arg;
154162

155163
HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
@@ -243,9 +251,9 @@ public void contributeMethodArgument(MethodParameter parameter, Object value,
243251
builder.queryParam(name);
244252
}
245253
else if (value instanceof Collection) {
246-
for (Object v : (Collection<?>) value) {
247-
v = formatUriValue(conversionService, TypeDescriptor.nested(parameter, 1), v);
248-
builder.queryParam(name, v);
254+
for (Object element : (Collection<?>) value) {
255+
element = formatUriValue(conversionService, TypeDescriptor.nested(parameter, 1), element);
256+
builder.queryParam(name, element);
249257
}
250258
}
251259
else {
@@ -254,18 +262,17 @@ else if (value instanceof Collection) {
254262
}
255263

256264
protected String formatUriValue(ConversionService cs, TypeDescriptor sourceType, Object value) {
257-
return (cs != null) ?
258-
(String) cs.convert(value, sourceType, STRING_TYPE_DESCRIPTOR) : null;
265+
return (cs != null ? (String) cs.convert(value, sourceType, STRING_TYPE_DESCRIPTOR) : null);
259266
}
260267

261268

262-
private class RequestParamNamedValueInfo extends NamedValueInfo {
269+
private static class RequestParamNamedValueInfo extends NamedValueInfo {
263270

264-
private RequestParamNamedValueInfo() {
271+
public RequestParamNamedValueInfo() {
265272
super("", false, ValueConstants.DEFAULT_NONE);
266273
}
267274

268-
private RequestParamNamedValueInfo(RequestParam annotation) {
275+
public RequestParamNamedValueInfo(RequestParam annotation) {
269276
super(annotation.value(), annotation.required(), annotation.defaultValue());
270277
}
271278
}

spring-web/src/main/java/org/springframework/web/method/support/CompositeUriComponentsContributor.java

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@
1616

1717
package org.springframework.web.method.support;
1818

19+
import java.util.Collection;
20+
import java.util.Collections;
21+
import java.util.LinkedList;
22+
import java.util.List;
23+
import java.util.Map;
24+
1925
import org.springframework.core.MethodParameter;
2026
import org.springframework.core.convert.ConversionService;
2127
import org.springframework.format.support.DefaultFormattingConversionService;
2228
import org.springframework.util.Assert;
2329
import org.springframework.web.util.UriComponentsBuilder;
2430

25-
import java.util.ArrayList;
26-
import java.util.Collection;
27-
import java.util.List;
28-
import java.util.Map;
29-
3031
/**
3132
* A {@link UriComponentsContributor} containing a list of other contributors
3233
* to delegate and also encapsulating a specific {@link ConversionService} to
@@ -37,7 +38,7 @@
3738
*/
3839
public class CompositeUriComponentsContributor implements UriComponentsContributor {
3940

40-
private final List<UriComponentsContributor> contributors = new ArrayList<UriComponentsContributor>();
41+
private final List<UriComponentsContributor> contributors = new LinkedList<UriComponentsContributor>();
4142

4243
private final ConversionService conversionService;
4344

@@ -46,10 +47,24 @@ public class CompositeUriComponentsContributor implements UriComponentsContribut
4647
* Create an instance from a collection of {@link UriComponentsContributor}s or
4748
* {@link HandlerMethodArgumentResolver}s. Since both of these tend to be implemented
4849
* by the same class, the most convenient option is to obtain the configured
49-
* {@code HandlerMethodArgumentResolvers} in {@code RequestMappingHandlerAdapter} and
50-
* provide that to this constructor.
50+
* {@code HandlerMethodArgumentResolvers} in {@code RequestMappingHandlerAdapter}
51+
* and provide that to this constructor.
5152
* @param contributors a collection of {@link UriComponentsContributor}
52-
* or {@link HandlerMethodArgumentResolver}s.
53+
* or {@link HandlerMethodArgumentResolver}s.
54+
*/
55+
public CompositeUriComponentsContributor(UriComponentsContributor... contributors) {
56+
Collections.addAll(this.contributors, contributors);
57+
this.conversionService = new DefaultFormattingConversionService();
58+
}
59+
60+
/**
61+
* Create an instance from a collection of {@link UriComponentsContributor}s or
62+
* {@link HandlerMethodArgumentResolver}s. Since both of these tend to be implemented
63+
* by the same class, the most convenient option is to obtain the configured
64+
* {@code HandlerMethodArgumentResolvers} in {@code RequestMappingHandlerAdapter}
65+
* and provide that to this constructor.
66+
* @param contributors a collection of {@link UriComponentsContributor}
67+
* or {@link HandlerMethodArgumentResolver}s.
5368
*/
5469
public CompositeUriComponentsContributor(Collection<?> contributors) {
5570
this(contributors, null);
@@ -70,17 +85,14 @@ public CompositeUriComponentsContributor(Collection<?> contributors) {
7085
* need to be formatted as Strings before being added to the URI
7186
*/
7287
public CompositeUriComponentsContributor(Collection<?> contributors, ConversionService conversionService) {
73-
7488
Assert.notNull(contributors, "'uriComponentsContributors' must not be null");
75-
7689
for (Object contributor : contributors) {
7790
if (contributor instanceof UriComponentsContributor) {
7891
this.contributors.add((UriComponentsContributor) contributor);
7992
}
8093
}
81-
82-
this.conversionService = (conversionService != null) ?
83-
conversionService : new DefaultFormattingConversionService();
94+
this.conversionService =
95+
(conversionService != null ? conversionService : new DefaultFormattingConversionService());
8496
}
8597

8698

@@ -90,8 +102,8 @@ public boolean hasContributors() {
90102

91103
@Override
92104
public boolean supportsParameter(MethodParameter parameter) {
93-
for (UriComponentsContributor c : this.contributors) {
94-
if (c.supportsParameter(parameter)) {
105+
for (UriComponentsContributor contributor : this.contributors) {
106+
if (contributor.supportsParameter(parameter)) {
95107
return true;
96108
}
97109
}
@@ -102,9 +114,9 @@ public boolean supportsParameter(MethodParameter parameter) {
102114
public void contributeMethodArgument(MethodParameter parameter, Object value,
103115
UriComponentsBuilder builder, Map<String, Object> uriVariables, ConversionService conversionService) {
104116

105-
for (UriComponentsContributor c : this.contributors) {
106-
if (c.supportsParameter(parameter)) {
107-
c.contributeMethodArgument(parameter, value, builder, uriVariables, conversionService);
117+
for (UriComponentsContributor contributor : this.contributors) {
118+
if (contributor.supportsParameter(parameter)) {
119+
contributor.contributeMethodArgument(parameter, value, builder, uriVariables, conversionService);
108120
break;
109121
}
110122
}

0 commit comments

Comments
 (0)