Polishing

This commit is contained in:
Juergen Hoeller 2016-12-01 14:13:23 +01:00
parent f16d453805
commit 5fee5f39ea
14 changed files with 95 additions and 85 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@ -30,8 +30,8 @@ import org.springframework.util.StringUtils;
* Helper class for calculating property matches, according to a configurable
* distance. Provide the list of potential matches and an easy way to generate
* an error message. Works for both java bean properties and fields.
* <p>
* Mainly for use within the framework and in particular the binding facility
*
* <p>Mainly for use within the framework and in particular the binding facility.
*
* @author Alef Arendsen
* @author Arjen Poutsma
@ -43,14 +43,12 @@ import org.springframework.util.StringUtils;
*/
public abstract class PropertyMatches {
//---------------------------------------------------------------------
// Static section
//---------------------------------------------------------------------
/** Default maximum property distance: 2 */
public static final int DEFAULT_MAX_DISTANCE = 2;
// Static factory methods
/**
* Create PropertyMatches for the given bean property.
* @param propertyName the name of the property to find possible matches for
@ -90,9 +88,7 @@ public abstract class PropertyMatches {
}
//---------------------------------------------------------------------
// Instance section
//---------------------------------------------------------------------
// Instance state
private final String propertyName;
@ -107,18 +103,19 @@ public abstract class PropertyMatches {
this.possibleMatches = possibleMatches;
}
/**
* Return the name of the requested property.
*/
public String getPropertyName() {
return propertyName;
return this.propertyName;
}
/**
* Return the calculated possible matches.
*/
public String[] getPossibleMatches() {
return possibleMatches;
return this.possibleMatches;
}
/**
@ -127,6 +124,9 @@ public abstract class PropertyMatches {
*/
public abstract String buildErrorMessage();
// Implementation support for subclasses
protected void appendHintMessage(StringBuilder msg) {
msg.append("Did you mean ");
for (int i = 0; i < this.possibleMatches.length; i++) {
@ -184,9 +184,12 @@ public abstract class PropertyMatches {
return d[s1.length()][s2.length()];
}
// Concrete subclasses
private static class BeanPropertyMatches extends PropertyMatches {
private BeanPropertyMatches(String propertyName, Class<?> beanClass, int maxDistance) {
public BeanPropertyMatches(String propertyName, Class<?> beanClass, int maxDistance) {
super(propertyName, calculateMatches(propertyName,
BeanUtils.getPropertyDescriptors(beanClass), maxDistance));
}
@ -231,12 +234,12 @@ public abstract class PropertyMatches {
}
return msg.toString();
}
}
private static class FieldPropertyMatches extends PropertyMatches {
private FieldPropertyMatches(String propertyName, Class<?> beanClass, int maxDistance) {
public FieldPropertyMatches(String propertyName, Class<?> beanClass, int maxDistance) {
super(propertyName, calculateMatches(propertyName, beanClass, maxDistance));
}
@ -255,7 +258,6 @@ public abstract class PropertyMatches {
return StringUtils.toStringArray(candidates);
}
@Override
public String buildErrorMessage() {
String propertyName = getPropertyName();
@ -270,7 +272,6 @@ public abstract class PropertyMatches {
}
return msg.toString();
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2016 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,12 +27,12 @@ import org.springframework.core.convert.support.GenericConversionService;
/**
* A factory providing convenient access to a ConversionService configured with
* converters appropriate for most environments. Set the {@link #setConverters
* "converters"} property to supplement the default converters.
* converters appropriate for most environments. Set the
* {@link #setConverters "converters"} property to supplement the default converters.
*
* <p>This implementation creates a {@link DefaultConversionService}. Subclasses
* may override {@link #createConversionService()} in order to return a
* {@link GenericConversionService} instance of their choosing.
* <p>This implementation creates a {@link DefaultConversionService}.
* Subclasses may override {@link #createConversionService()} in order to return
* a {@link GenericConversionService} instance of their choosing.
*
* <p>Like all {@code FactoryBean} implementations, this class is suitable for
* use when configuring a Spring application context using Spring {@code <beans>}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2016 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.
@ -49,7 +49,6 @@ import org.springframework.util.CollectionUtils;
* @author Rob Harrop
* @author Juergen Hoeller
* @since 1.2
* @see FactoryBean
* @see JMXConnectorServer
* @see MBeanServer
*/

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@ -206,8 +206,8 @@ public class ScriptFactoryPostProcessor extends InstantiationAwareBeanPostProces
@Override
public void setBeanFactory(BeanFactory beanFactory) {
if (!(beanFactory instanceof ConfigurableBeanFactory)) {
throw new IllegalStateException("ScriptFactoryPostProcessor doesn't work with a BeanFactory "
+ "which does not implement ConfigurableBeanFactory: " + beanFactory.getClass());
throw new IllegalStateException("ScriptFactoryPostProcessor doesn't work with " +
"non-ConfigurableBeanFactory: " + beanFactory.getClass());
}
this.beanFactory = (ConfigurableBeanFactory) beanFactory;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 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.
@ -29,13 +29,12 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
*
* @author Stephane Nicoll
*/
public class ExpressionCachingIntegrationTests {
@SuppressWarnings("unchecked")
@Test // SPR-11692
@SuppressWarnings("unchecked")
public void expressionIsCacheBasedOnActualMethod() {
ConfigurableApplicationContext context =
new AnnotationConfigApplicationContext(SharedConfig.class, Spr11692Config.class);
@ -50,9 +49,9 @@ public class ExpressionCachingIntegrationTests {
}
@Configuration
static class Spr11692Config {
@Bean
public BaseDao<User> userDao() {
return new UserDaoImpl();
@ -65,11 +64,14 @@ public class ExpressionCachingIntegrationTests {
}
private static interface BaseDao<T> {
private interface BaseDao<T> {
T persist(T t);
}
private static class UserDaoImpl implements BaseDao<User> {
@Override
@CachePut(value = "users", key = "#user.id")
public User persist(User user) {
@ -77,7 +79,9 @@ public class ExpressionCachingIntegrationTests {
}
}
private static class OrderDaoImpl implements BaseDao<Order> {
@Override
@CachePut(value = "orders", key = "#order.id")
public Order persist(Order order) {
@ -85,35 +89,41 @@ public class ExpressionCachingIntegrationTests {
}
}
private static class User {
private final String id;
private User(String id) {
public User(String id) {
this.id = id;
}
@SuppressWarnings("unused")
public String getId() {
return id;
return this.id;
}
}
private static class Order {
private final String id;
private Order(String id) {
public Order(String id) {
this.id = id;
}
@SuppressWarnings("unused")
public String getId() {
return id;
return this.id;
}
}
@Configuration
@EnableCaching
static class SharedConfig extends CachingConfigurerSupport {
@Override
@Bean
public CacheManager cacheManager() {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 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.
@ -72,7 +72,7 @@ public final class Property {
this.readMethod = readMethod;
this.writeMethod = writeMethod;
this.methodParameter = resolveMethodParameter();
this.name = (name == null ? resolveName() : name);
this.name = (name != null ? name : resolveName());
}

View File

@ -32,6 +32,7 @@ abstract class ConversionUtils {
public static Object invokeConverter(GenericConverter converter, Object source, TypeDescriptor sourceType,
TypeDescriptor targetType) {
try {
return converter.convert(source, sourceType, targetType);
}
@ -43,7 +44,9 @@ abstract class ConversionUtils {
}
}
public static boolean canConvertElements(TypeDescriptor sourceElementType, TypeDescriptor targetElementType, ConversionService conversionService) {
public static boolean canConvertElements(TypeDescriptor sourceElementType, TypeDescriptor targetElementType,
ConversionService conversionService) {
if (targetElementType == null) {
// yes
return true;
@ -57,11 +60,11 @@ abstract class ConversionUtils {
return true;
}
else if (sourceElementType.getType().isAssignableFrom(targetElementType.getType())) {
// maybe;
// maybe
return true;
}
else {
// no;
// no
return false;
}
}

View File

@ -67,8 +67,8 @@ public class GenericConversionService implements ConfigurableConversionService {
private static final GenericConverter NO_OP_CONVERTER = new NoOpConverter("NO_OP");
/**
* Used as a cache entry when no converter is available. This converter is never
* returned.
* Used as a cache entry when no converter is available.
* This converter is never returned.
*/
private static final GenericConverter NO_MATCH = new NoOpConverter("NO_MATCH");

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@ -151,8 +151,7 @@ public class SimpleClientHttpRequestFactory implements ClientHttpRequestFactory,
*/
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException {
Assert.state(this.taskExecutor != null,
"Asynchronous execution requires an AsyncTaskExecutor to be set");
Assert.state(this.taskExecutor != null, "Asynchronous execution requires TaskExecutor to be set");
HttpURLConnection connection = openConnection(uri.toURL(), this.proxy);
prepareConnection(connection, httpMethod.name());

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@ -44,14 +44,14 @@ import org.springframework.util.Assert;
@SuppressWarnings("serial")
public class UriTemplate implements Serializable {
private final String uriTemplate;
private final UriComponents uriComponents;
private final List<String> variableNames;
private final Pattern matchPattern;
private final String uriTemplate;
/**
* Construct a new {@code UriTemplate} with the given URI String.
@ -173,7 +173,6 @@ public class UriTemplate implements Serializable {
private final Pattern pattern;
private TemplateInfo(List<String> vars, Pattern pattern) {
this.variableNames = vars;
this.pattern = pattern;
@ -187,7 +186,7 @@ public class UriTemplate implements Serializable {
return this.pattern;
}
private static TemplateInfo parse(String uriTemplate) {
public static TemplateInfo parse(String uriTemplate) {
int level = 0;
List<String> variableNames = new ArrayList<String>();
StringBuilder pattern = new StringBuilder();
@ -216,8 +215,7 @@ public class UriTemplate implements Serializable {
else {
if (idx + 1 == variable.length()) {
throw new IllegalArgumentException(
"No custom regular expression specified after ':' " +
"in \"" + variable + "\"");
"No custom regular expression specified after ':' in \"" + variable + "\"");
}
String regex = variable.substring(idx + 1, variable.length());
pattern.append('(');
@ -238,7 +236,7 @@ public class UriTemplate implements Serializable {
}
private static String quote(StringBuilder builder) {
return builder.length() != 0 ? Pattern.quote(builder.toString()) : "";
return (builder.length() > 0 ? Pattern.quote(builder.toString()) : "");
}
}

View File

@ -119,7 +119,6 @@ public class AsyncRestTemplateIntegrationTests extends AbstractJettyServerTestCa
assertNull("Invalid content", entity.getBody());
}
@Test
public void getNoContentTypeHeader() throws Exception {
Future<ResponseEntity<byte[]>> futureEntity = template.getForEntity(baseUrl + "/get/nocontenttype", byte[].class);
@ -127,7 +126,6 @@ public class AsyncRestTemplateIntegrationTests extends AbstractJettyServerTestCa
assertArrayEquals("Invalid content", helloWorld.getBytes("UTF-8"), responseEntity.getBody());
}
@Test
public void getNoContent() throws Exception {
Future<ResponseEntity<String>> responseFuture = template.getForEntity(baseUrl + "/status/nocontent", String.class);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2016 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.
@ -86,7 +86,8 @@ import org.springframework.web.context.support.ServletContextResource;
* @see #setDefaultUrl
* @see #setAllowedResources
* @see #setApplyLastModified
* @deprecated in favor of the ResourceHttpRequestHandler
* @deprecated as of Spring 4.3.5, in favor of
* {@link org.springframework.web.servlet.resource.ResourceHttpRequestHandler}
*/
@SuppressWarnings("serial")
@Deprecated
@ -177,8 +178,8 @@ public class ResourceServlet extends HttpServletBean {
}
/**
* Return a PathMatcher to use for matching the "allowedResources" URL pattern.
* Default is AntPathMatcher.
* Return a {@link PathMatcher} to use for matching the "allowedResources" URL pattern.
* <p>The default is {@link AntPathMatcher}.
* @see #setAllowedResources
* @see org.springframework.util.AntPathMatcher
*/
@ -195,7 +196,7 @@ public class ResourceServlet extends HttpServletBean {
protected final void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// determine URL of resource to include
// Determine URL of resource to include...
String resourceUrl = determineResourceUrl(request);
if (resourceUrl != null) {
@ -222,7 +223,7 @@ public class ResourceServlet extends HttpServletBean {
}
}
// no resource URL specified -> try to include default URL.
// No resource URL specified -> try to include default URL.
else if (!includeDefaultUrl(request, response)) {
throw new ServletException("No target resource URL found for request");
}
@ -272,18 +273,18 @@ public class ResourceServlet extends HttpServletBean {
if (this.contentType != null) {
response.setContentType(this.contentType);
}
String[] resourceUrls =
StringUtils.tokenizeToStringArray(resourceUrl, RESOURCE_URL_DELIMITERS);
for (int i = 0; i < resourceUrls.length; i++) {
// check whether URL matches allowed resources
if (this.allowedResources != null && !this.pathMatcher.match(this.allowedResources, resourceUrls[i])) {
throw new ServletException("Resource [" + resourceUrls[i] +
String[] resourceUrls = StringUtils.tokenizeToStringArray(resourceUrl, RESOURCE_URL_DELIMITERS);
for (String url : resourceUrls) {
// Check whether URL matches allowed resources
if (this.allowedResources != null && !this.pathMatcher.match(this.allowedResources, url)) {
throw new ServletException("Resource [" + url +
"] does not match allowed pattern [" + this.allowedResources + "]");
}
if (logger.isDebugEnabled()) {
logger.debug("Including resource [" + resourceUrls[i] + "]");
logger.debug("Including resource [" + url + "]");
}
RequestDispatcher rd = request.getRequestDispatcher(resourceUrls[i]);
RequestDispatcher rd = request.getRequestDispatcher(url);
rd.include(request, response);
}
}
@ -311,8 +312,8 @@ public class ResourceServlet extends HttpServletBean {
if (resourceUrl != null) {
String[] resourceUrls = StringUtils.tokenizeToStringArray(resourceUrl, RESOURCE_URL_DELIMITERS);
long latestTimestamp = -1;
for (int i = 0; i < resourceUrls.length; i++) {
long timestamp = getFileTimestamp(resourceUrls[i]);
for (String url : resourceUrls) {
long timestamp = getFileTimestamp(url);
if (timestamp > latestTimestamp) {
latestTimestamp = timestamp;
}
@ -338,8 +339,10 @@ public class ResourceServlet extends HttpServletBean {
return lastModifiedTime;
}
catch (IOException ex) {
logger.warn("Couldn't retrieve last-modified timestamp of [" + resource +
"] - using ResourceServlet startup time");
if (logger.isWarnEnabled()) {
logger.warn("Couldn't retrieve last-modified timestamp of " + resource +
" - using ResourceServlet startup time");
}
return -1;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2016 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.
@ -25,11 +25,10 @@ package org.springframework.web.socket;
public interface WebSocketMessage<T> {
/**
* Returns the message payload. This will never be {@code null}.
* Return the message payload (never {@code null}).
*/
T getPayload();
/**
* Return the number of bytes contained in the message.
*/

View File

@ -80,7 +80,7 @@ public abstract class AbstractSockJsSession implements SockJsSession {
private static final Set<String> disconnectedClientExceptions;
static {
Set<String> set = new HashSet<String>(2);
Set<String> set = new HashSet<String>(4);
set.add("ClientAbortException"); // Tomcat
set.add("EOFException"); // Tomcat
set.add("EofException"); // Jetty
@ -208,7 +208,7 @@ public abstract class AbstractSockJsSession implements SockJsSession {
writeFrameInternal(SockJsFrame.closeFrame(status.getCode(), status.getReason()));
}
catch (Throwable ex) {
logger.debug("Failure while send SockJS close frame", ex);
logger.debug("Failure while sending SockJS close frame", ex);
}
}
updateLastActiveTime();