Polishing

This commit is contained in:
Juergen Hoeller 2016-12-12 22:49:40 +01:00
parent aef1460a64
commit e49813f2c4
8 changed files with 80 additions and 75 deletions

View File

@ -281,7 +281,7 @@ public abstract class Conventions {
/** /**
* Retrieves the {@code Class} of an element in the {@code Collection}. * Retrieves the {@code Class} of an element in the {@code Collection}.
* The exact element for which the {@code Class} is retreived will depend * The exact element for which the {@code Class} is retrieved will depend
* on the concrete {@code Collection} implementation. * on the concrete {@code Collection} implementation.
*/ */
private static <E> E peekAhead(Collection<E> collection) { private static <E> E peekAhead(Collection<E> collection) {

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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -36,10 +36,12 @@ final class CollectionToStringConverter implements ConditionalGenericConverter {
private final ConversionService conversionService; private final ConversionService conversionService;
public CollectionToStringConverter(ConversionService conversionService) { public CollectionToStringConverter(ConversionService conversionService) {
this.conversionService = conversionService; this.conversionService = conversionService;
} }
@Override @Override
public Set<ConvertiblePair> getConvertibleTypes() { public Set<ConvertiblePair> getConvertibleTypes() {
return Collections.singleton(new ConvertiblePair(Collection.class, String.class)); return Collections.singleton(new ConvertiblePair(Collection.class, String.class));
@ -47,7 +49,8 @@ final class CollectionToStringConverter implements ConditionalGenericConverter {
@Override @Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return ConversionUtils.canConvertElements(sourceType.getElementTypeDescriptor(), targetType, this.conversionService); return ConversionUtils.canConvertElements(
sourceType.getElementTypeDescriptor(), targetType, this.conversionService);
} }
@Override @Override
@ -56,7 +59,7 @@ final class CollectionToStringConverter implements ConditionalGenericConverter {
return null; return null;
} }
Collection<?> sourceCollection = (Collection<?>) source; Collection<?> sourceCollection = (Collection<?>) source;
if (sourceCollection.size() == 0) { if (sourceCollection.isEmpty()) {
return ""; return "";
} }
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
@ -65,7 +68,8 @@ final class CollectionToStringConverter implements ConditionalGenericConverter {
if (i > 0) { if (i > 0) {
sb.append(DELIMITER); sb.append(DELIMITER);
} }
Object targetElement = this.conversionService.convert(sourceElement, sourceType.elementTypeDescriptor(sourceElement), targetType); Object targetElement = this.conversionService.convert(
sourceElement, sourceType.elementTypeDescriptor(sourceElement), targetType);
sb.append(targetElement); sb.append(targetElement);
i++; i++;
} }

View File

@ -131,14 +131,14 @@ public class GenericConversionService implements ConfigurableConversionService {
@Override @Override
public boolean canConvert(Class<?> sourceType, Class<?> targetType) { public boolean canConvert(Class<?> sourceType, Class<?> targetType) {
Assert.notNull(targetType, "targetType to convert to cannot be null"); Assert.notNull(targetType, "Target type to convert to cannot be null");
return canConvert((sourceType != null ? TypeDescriptor.valueOf(sourceType) : null), return canConvert((sourceType != null ? TypeDescriptor.valueOf(sourceType) : null),
TypeDescriptor.valueOf(targetType)); TypeDescriptor.valueOf(targetType));
} }
@Override @Override
public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) { public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) {
Assert.notNull(targetType, "targetType to convert to cannot be null"); Assert.notNull(targetType, "Target type to convert to cannot be null");
if (sourceType == null) { if (sourceType == null) {
return true; return true;
} }
@ -147,9 +147,9 @@ public class GenericConversionService implements ConfigurableConversionService {
} }
/** /**
* Return whether conversion between the sourceType and targetType can be bypassed. * Return whether conversion between the source type and the target type can be bypassed.
* <p>More precisely, this method will return true if objects of sourceType can be * <p>More precisely, this method will return true if objects of sourceType can be
* converted to the targetType by returning the source object unchanged. * converted to the target type by returning the source object unchanged.
* @param sourceType context about the source type to convert from * @param sourceType context about the source type to convert from
* (may be {@code null} if source is {@code null}) * (may be {@code null} if source is {@code null})
* @param targetType context about the target type to convert to (required) * @param targetType context about the target type to convert to (required)
@ -158,7 +158,7 @@ public class GenericConversionService implements ConfigurableConversionService {
* @since 3.2 * @since 3.2
*/ */
public boolean canBypassConvert(TypeDescriptor sourceType, TypeDescriptor targetType) { public boolean canBypassConvert(TypeDescriptor sourceType, TypeDescriptor targetType) {
Assert.notNull(targetType, "targetType to convert to cannot be null"); Assert.notNull(targetType, "Target type to convert to cannot be null");
if (sourceType == null) { if (sourceType == null) {
return true; return true;
} }
@ -169,20 +169,20 @@ public class GenericConversionService implements ConfigurableConversionService {
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T convert(Object source, Class<T> targetType) { public <T> T convert(Object source, Class<T> targetType) {
Assert.notNull(targetType, "targetType to convert to cannot be null"); Assert.notNull(targetType, "Target type to convert to cannot be null");
return (T) convert(source, TypeDescriptor.forObject(source), TypeDescriptor.valueOf(targetType)); return (T) convert(source, TypeDescriptor.forObject(source), TypeDescriptor.valueOf(targetType));
} }
@Override @Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
Assert.notNull(targetType, "targetType to convert to cannot be null"); Assert.notNull(targetType, "Target type to convert to cannot be null");
if (sourceType == null) { if (sourceType == null) {
Assert.isTrue(source == null, "source must be [null] if sourceType == [null]"); Assert.isTrue(source == null, "Source must be [null] if source type == [null]");
return handleResult(null, targetType, convertNullSource(null, targetType)); return handleResult(null, targetType, convertNullSource(null, targetType));
} }
if (source != null && !sourceType.getObjectType().isInstance(source)) { if (source != null && !sourceType.getObjectType().isInstance(source)) {
throw new IllegalArgumentException("source to convert from must be an instance of " + throw new IllegalArgumentException("Source to convert from must be an instance of [" +
sourceType + "; instead it was a " + source.getClass().getName()); sourceType + "]; instead it was a [" + source.getClass().getName() + "]");
} }
GenericConverter converter = getConverter(sourceType, targetType); GenericConverter converter = getConverter(sourceType, targetType);
if (converter != null) { if (converter != null) {
@ -194,9 +194,9 @@ public class GenericConversionService implements ConfigurableConversionService {
/** /**
* Convenience operation for converting a source object to the specified targetType, * Convenience operation for converting a source object to the specified targetType,
* where the targetType is a descriptor that provides additional conversion context. * where the target type is a descriptor that provides additional conversion context.
* Simply delegates to {@link #convert(Object, TypeDescriptor, TypeDescriptor)} and * Simply delegates to {@link #convert(Object, TypeDescriptor, TypeDescriptor)} and
* encapsulates the construction of the sourceType descriptor using * encapsulates the construction of the source type descriptor using
* {@link TypeDescriptor#forObject(Object)}. * {@link TypeDescriptor#forObject(Object)}.
* @param source the source object * @param source the source object
* @param targetType the target type * @param targetType the target type
@ -223,8 +223,8 @@ public class GenericConversionService implements ConfigurableConversionService {
* {@link java.util.Optional#empty()} instance if the target type is * {@link java.util.Optional#empty()} instance if the target type is
* {@code java.util.Optional}. Subclasses may override this to return * {@code java.util.Optional}. Subclasses may override this to return
* custom {@code null} objects for specific target types. * custom {@code null} objects for specific target types.
* @param sourceType the sourceType to convert from * @param sourceType the source type to convert from
* @param targetType the targetType to convert to * @param targetType the target type to convert to
* @return the converted null object * @return the converted null object
*/ */
protected Object convertNullSource(TypeDescriptor sourceType, TypeDescriptor targetType) { protected Object convertNullSource(TypeDescriptor sourceType, TypeDescriptor targetType) {
@ -268,7 +268,7 @@ public class GenericConversionService implements ConfigurableConversionService {
/** /**
* Return the default converter if no converter is found for the given sourceType/targetType pair. * Return the default converter if no converter is found for the given sourceType/targetType pair.
* <p>Returns a NO_OP Converter if the sourceType is assignable to the targetType. * <p>Returns a NO_OP Converter if the source type is assignable to the target type.
* Returns {@code null} otherwise, indicating no suitable converter could be found. * Returns {@code null} otherwise, indicating no suitable converter could be found.
* @param sourceType the source type to convert from * @param sourceType the source type to convert from
* @param targetType the target type to convert to * @param targetType the target type to convert to

View File

@ -66,13 +66,13 @@ final class ObjectToOptionalConverter implements ConditionalGenericConverter {
else if (source instanceof Optional) { else if (source instanceof Optional) {
return source; return source;
} }
else if (targetType.getResolvableType() == null) { else if (targetType.getResolvableType() != null) {
return Optional.of(source);
}
else {
Object target = this.conversionService.convert(source, sourceType, new GenericTypeDescriptor(targetType)); Object target = this.conversionService.convert(source, sourceType, new GenericTypeDescriptor(targetType));
return Optional.ofNullable(target); return Optional.ofNullable(target);
} }
else {
return Optional.of(source);
}
} }

View File

@ -129,7 +129,7 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
private int queryTimeout = -1; private int queryTimeout = -1;
/** /**
* If this variable is set to true then all results checking will be bypassed for any * If this variable is set to true, then all results checking will be bypassed for any
* callable statement processing. This can be used to avoid a bug in some older Oracle * callable statement processing. This can be used to avoid a bug in some older Oracle
* JDBC drivers like 10.1.0.2. * JDBC drivers like 10.1.0.2.
*/ */

View File

@ -185,44 +185,6 @@ public final class ModelFactory {
return result; return result;
} }
/**
* Derives the model attribute name for a method parameter based on:
* <ol>
* <li>The parameter {@code @ModelAttribute} annotation value
* <li>The parameter type
* </ol>
* @return the derived name; never {@code null} or an empty string
*/
public static String getNameForParameter(MethodParameter parameter) {
ModelAttribute ann = parameter.getParameterAnnotation(ModelAttribute.class);
String name = (ann != null ? ann.value() : null);
return StringUtils.hasText(name) ? name : Conventions.getVariableNameForParameter(parameter);
}
/**
* Derive the model attribute name for the given return value using one of:
* <ol>
* <li>The method {@code ModelAttribute} annotation value
* <li>The declared return type if it is more specific than {@code Object}
* <li>The actual return value type
* </ol>
* @param returnValue the value returned from a method invocation
* @param returnType the return type of the method
* @return the model name, never {@code null} nor empty
*/
public static String getNameForReturnValue(Object returnValue, MethodParameter returnType) {
ModelAttribute ann = returnType.getMethodAnnotation(ModelAttribute.class);
if (ann != null && StringUtils.hasText(ann.value())) {
return ann.value();
}
else {
Method method = returnType.getMethod();
Class<?> containingClass = returnType.getContainingClass();
Class<?> resolvedType = GenericTypeResolver.resolveReturnType(method, containingClass);
return Conventions.getVariableNameForReturnType(method, resolvedType, returnValue);
}
}
/** /**
* Promote model attributes listed as {@code @SessionAttributes} to the session. * Promote model attributes listed as {@code @SessionAttributes} to the session.
* Add {@link BindingResult} attributes where necessary. * Add {@link BindingResult} attributes where necessary.
@ -250,10 +212,8 @@ public final class ModelFactory {
List<String> keyNames = new ArrayList<>(model.keySet()); List<String> keyNames = new ArrayList<>(model.keySet());
for (String name : keyNames) { for (String name : keyNames) {
Object value = model.get(name); Object value = model.get(name);
if (isBindingCandidate(name, value)) { if (isBindingCandidate(name, value)) {
String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + name; String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + name;
if (!model.containsAttribute(bindingResultKey)) { if (!model.containsAttribute(bindingResultKey)) {
WebDataBinder dataBinder = this.dataBinderFactory.createBinder(request, value, name); WebDataBinder dataBinder = this.dataBinderFactory.createBinder(request, value, name);
model.put(bindingResultKey, dataBinder.getBindingResult()); model.put(bindingResultKey, dataBinder.getBindingResult());
@ -270,7 +230,7 @@ public final class ModelFactory {
return false; return false;
} }
Class<?> attrType = (value != null) ? value.getClass() : null; Class<?> attrType = (value != null ? value.getClass() : null);
if (this.sessionAttributesHandler.isHandlerSessionAttribute(attributeName, attrType)) { if (this.sessionAttributesHandler.isHandlerSessionAttribute(attributeName, attrType)) {
return true; return true;
} }
@ -280,13 +240,53 @@ public final class ModelFactory {
} }
/**
* Derive the model attribute name for a method parameter based on:
* <ol>
* <li>the parameter {@code @ModelAttribute} annotation value
* <li>the parameter type
* </ol>
* @param parameter a descriptor for the method parameter
* @return the derived name (never {@code null} or empty String)
*/
public static String getNameForParameter(MethodParameter parameter) {
ModelAttribute ann = parameter.getParameterAnnotation(ModelAttribute.class);
String name = (ann != null ? ann.value() : null);
return (StringUtils.hasText(name) ? name : Conventions.getVariableNameForParameter(parameter));
}
/**
* Derive the model attribute name for the given return value based on:
* <ol>
* <li>the method {@code ModelAttribute} annotation value
* <li>the declared return type if it is more specific than {@code Object}
* <li>the actual return value type
* </ol>
* @param returnValue the value returned from a method invocation
* @param returnType a descriptor for the return type of the method
* @return the derived name (never {@code null} or empty String)
*/
public static String getNameForReturnValue(Object returnValue, MethodParameter returnType) {
ModelAttribute ann = returnType.getMethodAnnotation(ModelAttribute.class);
if (ann != null && StringUtils.hasText(ann.value())) {
return ann.value();
}
else {
Method method = returnType.getMethod();
Class<?> containingClass = returnType.getContainingClass();
Class<?> resolvedType = GenericTypeResolver.resolveReturnType(method, containingClass);
return Conventions.getVariableNameForReturnType(method, resolvedType, returnValue);
}
}
private static class ModelMethod { private static class ModelMethod {
private final InvocableHandlerMethod handlerMethod; private final InvocableHandlerMethod handlerMethod;
private final Set<String> dependencies = new HashSet<>(); private final Set<String> dependencies = new HashSet<>();
private ModelMethod(InvocableHandlerMethod handlerMethod) { public ModelMethod(InvocableHandlerMethod handlerMethod) {
this.handlerMethod = handlerMethod; this.handlerMethod = handlerMethod;
for (MethodParameter parameter : handlerMethod.getMethodParameters()) { for (MethodParameter parameter : handlerMethod.getMethodParameters()) {
if (parameter.hasParameterAnnotation(ModelAttribute.class)) { if (parameter.hasParameterAnnotation(ModelAttribute.class)) {

View File

@ -195,7 +195,7 @@ final class HierarchicalUriComponents extends UriComponents {
String hostTo = encodeUriComponent(this.host, charset, getHostType()); String hostTo = encodeUriComponent(this.host, charset, getHostType());
PathComponent pathTo = this.path.encode(charset); PathComponent pathTo = this.path.encode(charset);
MultiValueMap<String, String> paramsTo = encodeQueryParams(charset); MultiValueMap<String, String> paramsTo = encodeQueryParams(charset);
String fragmentTo = encodeUriComponent(this.getFragment(), charset, Type.FRAGMENT); String fragmentTo = encodeUriComponent(getFragment(), charset, Type.FRAGMENT);
return new HierarchicalUriComponents(schemeTo, userInfoTo, hostTo, this.port, return new HierarchicalUriComponents(schemeTo, userInfoTo, hostTo, this.port,
pathTo, paramsTo, fragmentTo, true, false); pathTo, paramsTo, fragmentTo, true, false);
} }
@ -339,7 +339,7 @@ final class HierarchicalUriComponents extends UriComponents {
String portTo = expandUriComponent(this.port, uriVariables); String portTo = expandUriComponent(this.port, uriVariables);
PathComponent pathTo = this.path.expand(uriVariables); PathComponent pathTo = this.path.expand(uriVariables);
MultiValueMap<String, String> paramsTo = expandQueryParams(uriVariables); MultiValueMap<String, String> paramsTo = expandQueryParams(uriVariables);
String fragmentTo = expandUriComponent(this.getFragment(), uriVariables); String fragmentTo = expandUriComponent(getFragment(), uriVariables);
return new HierarchicalUriComponents(schemeTo, userInfoTo, hostTo, portTo, return new HierarchicalUriComponents(schemeTo, userInfoTo, hostTo, portTo,
pathTo, paramsTo, fragmentTo, false, false); pathTo, paramsTo, fragmentTo, false, false);

View File

@ -19,6 +19,7 @@ package org.springframework.web.socket.server.jetty;
import java.io.IOException; import java.io.IOException;
import java.security.Principal; import java.security.Principal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
@ -176,8 +177,8 @@ public class JettyRequestUpgradeStrategy implements RequestUpgradeStrategy, Serv
Assert.isInstanceOf(ServletServerHttpResponse.class, response); Assert.isInstanceOf(ServletServerHttpResponse.class, response);
HttpServletResponse servletResponse = ((ServletServerHttpResponse) response).getServletResponse(); HttpServletResponse servletResponse = ((ServletServerHttpResponse) response).getServletResponse();
Assert.isTrue(this.factoryAdapter.getFactory() Assert.isTrue(this.factoryAdapter.getFactory().isUpgradeRequest(servletRequest, servletResponse),
.isUpgradeRequest(servletRequest, servletResponse), "Not a WebSocket handshake"); "Not a WebSocket handshake");
JettyWebSocketSession session = new JettyWebSocketSession(attributes, user); JettyWebSocketSession session = new JettyWebSocketSession(attributes, user);
JettyWebSocketHandlerAdapter handlerAdapter = new JettyWebSocketHandlerAdapter(wsHandler, session); JettyWebSocketHandlerAdapter handlerAdapter = new JettyWebSocketHandlerAdapter(wsHandler, session);
@ -213,12 +214,12 @@ public class JettyRequestUpgradeStrategy implements RequestUpgradeStrategy, Serv
this.handler = handler; this.handler = handler;
this.selectedProtocol = protocol; this.selectedProtocol = protocol;
if (CollectionUtils.isEmpty(extensions)) { if (CollectionUtils.isEmpty(extensions)) {
this.extensionConfigs = new ArrayList<>(); this.extensionConfigs = new LinkedList<>();
} }
else { else {
this.extensionConfigs = new ArrayList<>(); this.extensionConfigs = new ArrayList<>();
for (WebSocketExtension e : extensions) { for (WebSocketExtension extension : extensions) {
this.extensionConfigs.add(new WebSocketToJettyExtensionConfigAdapter(e)); this.extensionConfigs.add(new WebSocketToJettyExtensionConfigAdapter(extension));
} }
} }
} }