Polishing

This commit is contained in:
Juergen Hoeller
2014-09-17 02:35:41 +02:00
parent d1c720c07b
commit 15320db414
10 changed files with 52 additions and 41 deletions

View File

@@ -17,6 +17,7 @@
package org.springframework.context.support;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.tests.sample.beans.TestBean;
@@ -28,8 +29,9 @@ import static org.junit.Assert.*;
*/
public class SimpleThreadScopeTests {
private ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"simpleThreadScopeTests.xml", getClass());
private final ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("simpleThreadScopeTests.xml", getClass());
@Test
public void getFromScope() throws Exception {
@@ -45,19 +47,15 @@ public class SimpleThreadScopeTests {
public void getMultipleInstances() throws Exception {
final TestBean[] beans = new TestBean[2];
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
beans[0] = applicationContext.getBean("threadScopedObject",
TestBean.class);
beans[0] = applicationContext.getBean("threadScopedObject", TestBean.class);
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
beans[1] = applicationContext.getBean("threadScopedObject",
TestBean.class);
beans[1] = applicationContext.getBean("threadScopedObject", TestBean.class);
}
});
thread1.start();
@@ -67,7 +65,6 @@ public class SimpleThreadScopeTests {
assertNotNull(beans[0]);
assertNotNull(beans[1]);
assertNotSame(beans[0], beans[1]);
}

View File

@@ -215,7 +215,7 @@ public class GenericConversionService implements ConfigurableConversionService {
}
// subclassing hooks
// Protected template methods
/**
* Template method to convert a null source.
@@ -588,6 +588,7 @@ public class GenericConversionService implements ConfigurableConversionService {
private void addInterfacesToClassHierarchy(Class<?> type, boolean asArray,
List<Class<?>> hierarchy, Set<Class<?>> visited) {
for (Class<?> implementedInterface : type.getInterfaces()) {
addToClassHierarchy(hierarchy.size(), implementedInterface, asArray, hierarchy, visited);
}
@@ -595,6 +596,7 @@ public class GenericConversionService implements ConfigurableConversionService {
private void addToClassHierarchy(int index, Class<?> type, boolean asArray,
List<Class<?>> hierarchy, Set<Class<?>> visited) {
if (asArray) {
type = Array.newInstance(type, 0).getClass();
}

View File

@@ -125,6 +125,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
private static final boolean gsonPresent =
ClassUtils.isPresent("com.google.gson.Gson", RestTemplate.class.getClassLoader());
private final List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
private ResponseErrorHandler errorHandler = new DefaultResponseErrorHandler();

View File

@@ -48,6 +48,7 @@ public abstract class AbstractCookieValueMethodArgumentResolver extends Abstract
super(beanFactory);
}
@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.hasParameterAnnotation(CookieValue.class);
@@ -60,15 +61,17 @@ public abstract class AbstractCookieValueMethodArgumentResolver extends Abstract
}
@Override
protected void handleMissingValue(String cookieName, MethodParameter parameter) throws ServletRequestBindingException {
throw new ServletRequestBindingException(
"Missing cookie named '" + cookieName + "' for method parameter type " + parameter.getParameterType().getSimpleName());
protected void handleMissingValue(String name, MethodParameter parameter) throws ServletRequestBindingException {
throw new ServletRequestBindingException("Missing cookie '" + name +
"' for method parameter of type " + parameter.getParameterType().getSimpleName());
}
private static class CookieValueNamedValueInfo extends NamedValueInfo {
private CookieValueNamedValueInfo(CookieValue annotation) {
super(annotation.value(), annotation.required(), annotation.defaultValue());
}
}
}

View File

@@ -48,6 +48,7 @@ public class ExpressionValueMethodArgumentResolver extends AbstractNamedValueMet
super(beanFactory);
}
@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.hasParameterAnnotation(Value.class);
@@ -70,10 +71,12 @@ public class ExpressionValueMethodArgumentResolver extends AbstractNamedValueMet
throw new UnsupportedOperationException("@Value is never required: " + parameter.getMethod());
}
private static class ExpressionValueNamedValueInfo extends NamedValueInfo {
private ExpressionValueNamedValueInfo(Value annotation) {
super("@Value", false, annotation.value());
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -52,6 +52,7 @@ public class RequestHeaderMethodArgumentResolver extends AbstractNamedValueMetho
super(beanFactory);
}
@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.hasParameterAnnotation(RequestHeader.class) &&
@@ -77,14 +78,16 @@ public class RequestHeaderMethodArgumentResolver extends AbstractNamedValueMetho
@Override
protected void handleMissingValue(String name, MethodParameter parameter) throws ServletRequestBindingException {
throw new ServletRequestBindingException("Missing header '" + name +
"' for method parameter type " + parameter.getParameterType().getSimpleName());
throw new ServletRequestBindingException("Missing request header '" + name +
"' for method parameter of type " + parameter.getParameterType().getSimpleName());
}
private static class RequestHeaderNamedValueInfo extends NamedValueInfo {
private RequestHeaderNamedValueInfo(RequestHeader annotation) {
super(annotation.value(), annotation.required(), annotation.defaultValue());
}
}
}

View File

@@ -152,17 +152,16 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod
@Override
protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) {
RequestParam annotation = parameter.getParameterAnnotation(RequestParam.class);
return (annotation != null) ? new RequestParamNamedValueInfo(annotation) : new RequestParamNamedValueInfo();
RequestParam ann = parameter.getParameterAnnotation(RequestParam.class);
return (ann != null ? new RequestParamNamedValueInfo(ann) : new RequestParamNamedValueInfo());
}
@Override
protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest webRequest) throws Exception {
Object arg;
HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
MultipartHttpServletRequest multipartRequest =
WebUtils.getNativeRequest(servletRequest, MultipartHttpServletRequest.class);
WebUtils.getNativeRequest(servletRequest, MultipartHttpServletRequest.class);
Object arg;
if (MultipartFile.class.equals(parameter.getParameterType())) {
assertIsMultipartRequest(servletRequest);
@@ -255,29 +254,29 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod
}
@Override
public void contributeMethodArgument(MethodParameter param, Object value,
public void contributeMethodArgument(MethodParameter parameter, Object value,
UriComponentsBuilder builder, Map<String, Object> uriVariables, ConversionService conversionService) {
Class<?> paramType = param.getParameterType();
Class<?> paramType = parameter.getParameterType();
if (Map.class.isAssignableFrom(paramType) || MultipartFile.class.equals(paramType) ||
"javax.servlet.http.Part".equals(paramType.getName())) {
return;
}
RequestParam annot = param.getParameterAnnotation(RequestParam.class);
String name = (annot == null || StringUtils.isEmpty(annot.value()) ? param.getParameterName() : annot.value());
RequestParam ann = parameter.getParameterAnnotation(RequestParam.class);
String name = (ann == null || StringUtils.isEmpty(ann.value()) ? parameter.getParameterName() : ann.value());
if (value == null) {
builder.queryParam(name);
}
else if (value instanceof Collection) {
for (Object element : (Collection<?>) value) {
element = formatUriValue(conversionService, TypeDescriptor.nested(param, 1), element);
element = formatUriValue(conversionService, TypeDescriptor.nested(parameter, 1), element);
builder.queryParam(name, element);
}
}
else {
builder.queryParam(name, formatUriValue(conversionService, new TypeDescriptor(param), value));
builder.queryParam(name, formatUriValue(conversionService, new TypeDescriptor(parameter), value));
}
}

View File

@@ -116,7 +116,7 @@ public class MatrixVariableMethodArgumentResolver extends AbstractNamedValueMeth
@Override
protected void handleMissingValue(String name, MethodParameter parameter) throws ServletRequestBindingException {
throw new ServletRequestBindingException("Missing matrix variable '" + name +
"' for method parameter type " + parameter.getParameterType().getSimpleName());
"' for method parameter of type " + parameter.getParameterType().getSimpleName());
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -103,7 +103,7 @@ public class PathVariableMethodArgumentResolver extends AbstractNamedValueMethod
@Override
protected void handleMissingValue(String name, MethodParameter parameter) throws ServletRequestBindingException {
throw new ServletRequestBindingException("Missing URI template variable '" + name +
"' for method parameter type " + parameter.getParameterType().getSimpleName());
"' for method parameter of type " + parameter.getParameterType().getSimpleName());
}
@Override
@@ -122,16 +122,16 @@ public class PathVariableMethodArgumentResolver extends AbstractNamedValueMethod
}
@Override
public void contributeMethodArgument(MethodParameter param, Object value,
UriComponentsBuilder builder, Map<String, Object> uriVariables, ConversionService cs) {
public void contributeMethodArgument(MethodParameter parameter, Object value,
UriComponentsBuilder builder, Map<String, Object> uriVariables, ConversionService conversionService) {
if (Map.class.isAssignableFrom(param.getParameterType())) {
if (Map.class.isAssignableFrom(parameter.getParameterType())) {
return;
}
PathVariable annot = param.getParameterAnnotation(PathVariable.class);
String name = (StringUtils.isEmpty(annot.value()) ? param.getParameterName() : annot.value());
value = formatUriValue(cs, new TypeDescriptor(param), value);
PathVariable ann = parameter.getParameterAnnotation(PathVariable.class);
String name = (ann == null || StringUtils.isEmpty(ann.value()) ? parameter.getParameterName() : ann.value());
value = formatUriValue(conversionService, new TypeDescriptor(parameter), value);
uriVariables.put(name, value);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -27,8 +27,8 @@ import org.springframework.web.util.UrlPathHelper;
import org.springframework.web.util.WebUtils;
/**
* An {@link org.springframework.web.method.annotation.AbstractCookieValueMethodArgumentResolver} that resolves cookie
* values from an {@link HttpServletRequest}.
* An {@link org.springframework.web.method.annotation.AbstractCookieValueMethodArgumentResolver}
* that resolves cookie values from an {@link HttpServletRequest}.
*
* @author Rossen Stoyanchev
* @since 3.1
@@ -37,17 +37,19 @@ public class ServletCookieValueMethodArgumentResolver extends AbstractCookieValu
private UrlPathHelper urlPathHelper = new UrlPathHelper();
public ServletCookieValueMethodArgumentResolver(ConfigurableBeanFactory beanFactory) {
super(beanFactory);
}
public void setUrlPathHelper(UrlPathHelper urlPathHelper) {
this.urlPathHelper = urlPathHelper;
}
@Override
protected Object resolveName(String cookieName, MethodParameter parameter, NativeWebRequest webRequest)
throws Exception {
protected Object resolveName(String cookieName, MethodParameter parameter, NativeWebRequest webRequest) throws Exception {
HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
Cookie cookieValue = WebUtils.getCookie(servletRequest, cookieName);
if (Cookie.class.isAssignableFrom(parameter.getParameterType())) {
@@ -60,4 +62,5 @@ public class ServletCookieValueMethodArgumentResolver extends AbstractCookieValu
return null;
}
}
}