Polishing

Issue: SPR-11225
This commit is contained in:
Juergen Hoeller 2013-12-19 23:08:18 +01:00
parent 11fb12b920
commit 2faf008c2e
2 changed files with 31 additions and 29 deletions

View File

@ -513,15 +513,18 @@ public class TypeDescriptor implements Serializable {
} }
ResolvableType element = (elementTypeDescriptor == null ? null ResolvableType element = (elementTypeDescriptor == null ? null
: elementTypeDescriptor.resolvableType); : elementTypeDescriptor.resolvableType);
return new TypeDescriptor(ResolvableType.forClassWithGenerics(collectionType, return new TypeDescriptor(ResolvableType.forClassWithGenerics(collectionType, element), null, null);
element), null, null);
} }
/** /**
* Create a new type descriptor from a {@link java.util.Map} type. * Create a new type descriptor from a {@link java.util.Map} type.
* <p>Useful for converting to typed Maps. * <p>Useful for converting to typed Maps.
* <p>For example, a Map&lt;String, String&gt; could be converted to a Map&lt;Id, EmailAddress&gt; by converting to a targetType built with this method: * <p>For example, a Map&lt;String, String&gt; could be converted to a Map&lt;Id, EmailAddress&gt;
* The method call to construct such a TypeDescriptor would look something like: map(Map.class, TypeDescriptor.valueOf(Id.class), TypeDescriptor.valueOf(EmailAddress.class)); * by converting to a targetType built with this method:
* The method call to construct such a TypeDescriptor would look something like:
* <pre class="code">
* map(Map.class, TypeDescriptor.valueOf(Id.class), TypeDescriptor.valueOf(EmailAddress.class));
* </pre>
* @param mapType the map type, which must implement {@link Map} * @param mapType the map type, which must implement {@link Map}
* @param keyTypeDescriptor a descriptor for the map's key type, used to convert map keys * @param keyTypeDescriptor a descriptor for the map's key type, used to convert map keys
* @param valueTypeDescriptor the map's value type, used to convert map values * @param valueTypeDescriptor the map's value type, used to convert map values
@ -538,18 +541,19 @@ public class TypeDescriptor implements Serializable {
/** /**
* Create a new type descriptor as an array of the specified type. * Create a new type descriptor as an array of the specified type.
* <p>For example to create a {@code Map<String,String>[]} use * <p>For example to create a {@code Map<String,String>[]} use:
* {@code TypeDescriptor.array(TypeDescriptor.map(Map.class, TypeDescriptor.value(String.class), TypeDescriptor.value(String.class)))}. * <pre class="code">
* TypeDescriptor.array(TypeDescriptor.map(Map.class, TypeDescriptor.value(String.class), TypeDescriptor.value(String.class)));
* </pre>
* @param elementTypeDescriptor the {@link TypeDescriptor} of the array element or {@code null} * @param elementTypeDescriptor the {@link TypeDescriptor} of the array element or {@code null}
* @return an array {@link TypeDescriptor} or {@code null} if {@code elementTypeDescriptor} is {@code null} * @return an array {@link TypeDescriptor} or {@code null} if {@code elementTypeDescriptor} is {@code null}
* @since 3.2.1 * @since 3.2.1
*/ */
public static TypeDescriptor array(TypeDescriptor elementTypeDescriptor) { public static TypeDescriptor array(TypeDescriptor elementTypeDescriptor) {
if(elementTypeDescriptor == null) { if (elementTypeDescriptor == null) {
return null; return null;
} }
return new TypeDescriptor( return new TypeDescriptor(ResolvableType.forArrayComponent(elementTypeDescriptor.resolvableType),
ResolvableType.forArrayComponent(elementTypeDescriptor.resolvableType),
null, elementTypeDescriptor.getAnnotations()); null, elementTypeDescriptor.getAnnotations());
} }
@ -572,13 +576,13 @@ public class TypeDescriptor implements Serializable {
* @return the nested type descriptor at the specified nesting level, or null * @return the nested type descriptor at the specified nesting level, or null
* if it could not be obtained * if it could not be obtained
* @throws IllegalArgumentException if the nesting level of the input * @throws IllegalArgumentException if the nesting level of the input
* {@link MethodParameter} argument is not 1 * {@link MethodParameter} argument is not 1, or if the types up to the
* @throws IllegalArgumentException if the types up to the specified nesting * specified nesting level are not of collection, array, or map types
* level are not of collection, array, or map types
*/ */
public static TypeDescriptor nested(MethodParameter methodParameter, int nestingLevel) { public static TypeDescriptor nested(MethodParameter methodParameter, int nestingLevel) {
if (methodParameter.getNestingLevel() != 1) { if (methodParameter.getNestingLevel() != 1) {
throw new IllegalArgumentException("methodParameter nesting level must be 1: use the nestingLevel parameter to specify the desired nestingLevel for nested type traversal"); throw new IllegalArgumentException("methodParameter nesting level must be 1: " +
"use the nestingLevel parameter to specify the desired nestingLevel for nested type traversal");
} }
return nested(new TypeDescriptor(methodParameter), nestingLevel); return nested(new TypeDescriptor(methodParameter), nestingLevel);
} }
@ -634,8 +638,10 @@ public class TypeDescriptor implements Serializable {
/** /**
* Create a new type descriptor for an object. * Create a new type descriptor for an object.
* <p>Use this factory method to introspect a source object before asking the conversion system to convert it to some another type. * <p>Use this factory method to introspect a source object before asking the
* <p>If the provided object is null, returns null, else calls {@link #valueOf(Class)} to build a TypeDescriptor from the object's class. * conversion system to convert it to some another type.
* <p>If the provided object is null, returns null, else calls {@link #valueOf(Class)}
* to build a TypeDescriptor from the object's class.
* @param source the source object * @param source the source object
* @return the type descriptor * @return the type descriptor
*/ */
@ -649,7 +655,8 @@ public class TypeDescriptor implements Serializable {
if (Object.class.equals(nested.getType())) { if (Object.class.equals(nested.getType())) {
// could be a collection type but we don't know about its element type, // could be a collection type but we don't know about its element type,
// so let's just assume there is an element type of type Object // so let's just assume there is an element type of type Object
} else { }
else {
nested = nested.getNested(2); nested = nested.getNested(2);
} }
} }

View File

@ -61,6 +61,7 @@ public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodPro
super(messageConverters, contentNegotiationManager); super(messageConverters, contentNegotiationManager);
} }
@Override @Override
public boolean supportsParameter(MethodParameter parameter) { public boolean supportsParameter(MethodParameter parameter) {
Class<?> parameterType = parameter.getParameterType(); Class<?> parameterType = parameter.getParameterType();
@ -69,13 +70,11 @@ public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodPro
@Override @Override
public boolean supportsReturnType(MethodParameter returnType) { public boolean supportsReturnType(MethodParameter returnType) {
Class<?> parameterType = returnType.getParameterType(); return HttpEntity.class.isAssignableFrom(returnType.getParameterType());
return HttpEntity.class.isAssignableFrom(parameterType) || ResponseEntity.class.isAssignableFrom(parameterType);
} }
@Override @Override
public Object resolveArgument( public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) NativeWebRequest webRequest, WebDataBinderFactory binderFactory)
throws IOException, HttpMediaTypeNotSupportedException { throws IOException, HttpMediaTypeNotSupportedException {
@ -92,19 +91,15 @@ public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodPro
if (type.getActualTypeArguments().length == 1) { if (type.getActualTypeArguments().length == 1) {
return type.getActualTypeArguments()[0]; return type.getActualTypeArguments()[0];
} }
throw new IllegalArgumentException("HttpEntity parameter (" throw new IllegalArgumentException("HttpEntity parameter (" + parameter.getParameterName() +
+ parameter.getParameterName() + ") in method " + parameter.getMethod() ") in method " + parameter.getMethod() + " is not parameterized or has more than one parameter");
+ " is not parameterized or has more than one parameter");
} }
@Override @Override
public void handleReturnValue( public void handleReturnValue(Object returnValue, MethodParameter returnType,
Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
ModelAndViewContainer mavContainer, NativeWebRequest webRequest)
throws Exception {
mavContainer.setRequestHandled(true); mavContainer.setRequestHandled(true);
if (returnValue == null) { if (returnValue == null) {
return; return;
} }
@ -128,7 +123,7 @@ public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodPro
writeWithMessageConverters(body, returnType, inputMessage, outputMessage); writeWithMessageConverters(body, returnType, inputMessage, outputMessage);
} }
else { else {
// flush headers to the HttpServletResponse // Flush headers to the HttpServletResponse
outputMessage.getBody(); outputMessage.getBody();
} }
} }