diff --git a/spring-core/src/main/java/org/springframework/core/convert/ConversionService.java b/spring-core/src/main/java/org/springframework/core/convert/ConversionService.java index 84756278b21..75f9a276c70 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/ConversionService.java +++ b/spring-core/src/main/java/org/springframework/core/convert/ConversionService.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 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,36 +27,43 @@ package org.springframework.core.convert; public interface ConversionService { /** - * Returns true if objects of sourceType can be converted to targetType. - * If this method returns true, it means {@link #convert(Object, Class)} is capable of converting an instance of sourceType to targetType. - * Special note on collections, arrays, and maps types: - * For conversion between collection, array, and map types, this method will return 'true' - * even though a convert invocation may still generate a {@link ConversionException} if the underlying elements are not convertible. - * Callers are expected to handle this exceptional case when working with collections and maps. - * @param sourceType the source type to convert from (may be null if source is null) + * Return {@code true} if objects of {@code sourceType} can be converted to the {@code targetType}. + *

If this method returns {@code true}, it means {@link #convert(Object, Class)} is capable + * of converting an instance of {@code sourceType} to {@code targetType}. + *

Special note on collections, arrays, and maps types: + * For conversion between collection, array, and map types, this method will return {@code true} + * even though a convert invocation may still generate a {@link ConversionException} if the + * underlying elements are not convertible. Callers are expected to handle this exceptional case + * when working with collections and maps. + * @param sourceType the source type to convert from (may be {@code null} if source is {@code null}) * @param targetType the target type to convert to (required) - * @return true if a conversion can be performed, false if not - * @throws IllegalArgumentException if targetType is null + * @return {@code true} if a conversion can be performed, {@code false} if not + * @throws IllegalArgumentException if {@code targetType} is {@code null} */ boolean canConvert(Class sourceType, Class targetType); /** - * Returns true if objects of sourceType can be converted to the targetType. - * The TypeDescriptors provide additional context about the source and target locations where conversion would occur, often object fields or property locations. - * If this method returns true, it means {@link #convert(Object, TypeDescriptor, TypeDescriptor)} is capable of converting an instance of sourceType to targetType. - * Special note on collections, arrays, and maps types: - * For conversion between collection, array, and map types, this method will return 'true' - * even though a convert invocation may still generate a {@link ConversionException} if the underlying elements are not convertible. - * Callers are expected to handle this exceptional case when working with collections and maps. - * @param sourceType context about the source type to convert from (may be null if source is null) + * Return {@code true} if objects of {@code sourceType} can be converted to the {@code targetType}. + * The TypeDescriptors provide additional context about the source and target locations + * where conversion would occur, often object fields or property locations. + *

If this method returns {@code true}, it means {@link #convert(Object, TypeDescriptor, TypeDescriptor)} + * is capable of converting an instance of {@code sourceType} to {@code targetType}. + *

Special note on collections, arrays, and maps types: + * For conversion between collection, array, and map types, this method will return {@code true} + * even though a convert invocation may still generate a {@link ConversionException} if the + * underlying elements are not convertible. Callers are expected to handle this exceptional case + * when working with collections and maps. + * @param sourceType context about the source type to convert from + * (may be {@code null} if source is {@code null}) * @param targetType context about the target type to convert to (required) - * @return true if a conversion can be performed between the source and target types, false if not - * @throws IllegalArgumentException if targetType is null + * @return {@code true} if a conversion can be performed between the source and target types, + * {@code false} if not + * @throws IllegalArgumentException if {@code targetType} is {@code null} */ boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType); /** - * Convert the source to targetType. + * Convert the given {@code source} to the specified {@code targetType}. * @param source the source object to convert (may be null) * @param targetType the target type to convert to (required) * @return the converted object, an instance of targetType @@ -66,15 +73,17 @@ public interface ConversionService { T convert(Object source, Class targetType); /** - * Convert the source to targetType. - * The TypeDescriptors provide additional context about the source and target locations where conversion will occur, often object fields or property locations. + * Convert the given {@code source} to the specified {@code targetType}. + * The TypeDescriptors provide additional context about the source and target locations + * where conversion will occur, often object fields or property locations. * @param source the source object to convert (may be null) - * @param sourceType context about the source type converting from (may be null if source is null) + * @param sourceType context about the source type to convert from + * (may be {@code null} if source is {@code null}) * @param targetType context about the target type to convert to (required) - * @return the converted object, an instance of {@link TypeDescriptor#getObjectType() targetType} + * @return the converted object, an instance of {@link TypeDescriptor#getObjectType() targetType} * @throws ConversionException if a conversion exception occurred - * @throws IllegalArgumentException if targetType is null - * @throws IllegalArgumentException if sourceType is null but source is not null + * @throws IllegalArgumentException if targetType is {@code null}, + * or {@code sourceType} is {@code null} but source is not {@code null} */ Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType); diff --git a/spring-core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionConverterTests.java b/spring-core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionConverterTests.java index 9e12a26d772..ddb99a220b1 100644 --- a/spring-core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionConverterTests.java +++ b/spring-core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionConverterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 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. @@ -43,6 +43,7 @@ import static org.junit.Assert.*; /** * @author Keith Donald + * @author Juergen Hoeller */ public class CollectionToCollectionConverterTests { @@ -63,8 +64,9 @@ public class CollectionToCollectionConverterTests { assertTrue(conversionService.canConvert(sourceType, targetType)); try { conversionService.convert(list, sourceType, targetType); - } catch (ConversionFailedException e) { - assertTrue(e.getCause() instanceof ConverterNotFoundException); + } + catch (ConversionFailedException ex) { + assertTrue(ex.getCause() instanceof ConverterNotFoundException); } conversionService.addConverterFactory(new StringToNumberConverterFactory()); assertTrue(conversionService.canConvert(sourceType, targetType)); @@ -141,10 +143,10 @@ public class CollectionToCollectionConverterTests { TypeDescriptor targetType = new TypeDescriptor(getClass().getField("objectToCollection")); assertTrue(conversionService.canConvert(sourceType, targetType)); List>> result = (List>>) conversionService.convert(list, sourceType, targetType); - assertEquals((Integer)9, result.get(0).get(0).get(0)); - assertEquals((Integer)12, result.get(0).get(1).get(0)); - assertEquals((Integer)37, result.get(1).get(0).get(0)); - assertEquals((Integer)23, result.get(1).get(1).get(0)); + assertEquals((Integer) 9, result.get(0).get(0).get(0)); + assertEquals((Integer) 12, result.get(0).get(1).get(0)); + assertEquals((Integer) 37, result.get(1).get(0).get(0)); + assertEquals((Integer) 23, result.get(1).get(1).get(0)); } public List>> objectToCollection; diff --git a/spring-expression/src/main/java/org/springframework/expression/TypeConverter.java b/spring-expression/src/main/java/org/springframework/expression/TypeConverter.java index e6145ab20ef..2fad0f98a5c 100644 --- a/spring-expression/src/main/java/org/springframework/expression/TypeConverter.java +++ b/spring-expression/src/main/java/org/springframework/expression/TypeConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2013 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. @@ -19,10 +19,10 @@ package org.springframework.expression; import org.springframework.core.convert.TypeDescriptor; /** - * A type converter can convert values between different types encountered - * during expression evaluation. This is an SPI for the expression parser; - * see {@link org.springframework.core.convert.ConversionService} for the - * primary user API to Spring's conversion facilities. + * A type converter can convert values between different types encountered during + * expression evaluation. This is an SPI for the expression parser; see + * {@link org.springframework.core.convert.ConversionService} for the primary + * user API to Spring's conversion facilities. * * @author Andy Clement * @author Juergen Hoeller @@ -31,7 +31,8 @@ import org.springframework.core.convert.TypeDescriptor; public interface TypeConverter { /** - * Return true if the type converter can convert the specified type to the desired target type. + * Return {@code true} if the type converter can convert the specified type + * to the desired target type. * @param sourceType a type descriptor that describes the source type * @param targetType a type descriptor that describes the requested result type * @return true if that conversion can be performed @@ -39,12 +40,15 @@ public interface TypeConverter { boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType); /** - * Convert (may coerce) a value from one type to another, for example from a boolean to a string. - * The typeDescriptor parameter enables support for typed collections - if the caller really wishes they - * can have a List<Integer> for example, rather than simply a List. + * Convert (may coerce) a value from one type to another, for example from a boolean + * to a string. The typeDescriptor parameter enables support for typed collections - + * if the caller really wishes they can have a List<Integer> for example, rather + * than simply a List. * @param value the value to be converted - * @param sourceType a type descriptor that supplies extra information about the source object - * @param targetType a type descriptor that supplies extra information about the requested result type + * @param sourceType a type descriptor that supplies extra information about the + * source object + * @param targetType a type descriptor that supplies extra information about the + * requested result type * @return the converted value * @throws EvaluationException if conversion is not possible */ diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/support/StandardTypeConverter.java b/spring-expression/src/main/java/org/springframework/expression/spel/support/StandardTypeConverter.java index 483dade31e4..5d75cf55dbc 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/support/StandardTypeConverter.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/support/StandardTypeConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 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.expression.spel.SpelMessage; import org.springframework.util.Assert; /** - * Default implementation of the {@link TypeConverter} interface, - * delegating to a core Spring {@link ConversionService}. + * Default implementation of the {@link TypeConverter} interface, delegating to a core + * Spring {@link ConversionService}. * * @author Juergen Hoeller * @author Andy Clement @@ -65,11 +65,13 @@ public class StandardTypeConverter implements TypeConverter { try { return this.conversionService.convert(value, sourceType, targetType); } - catch (ConverterNotFoundException cenfe) { - throw new SpelEvaluationException(cenfe, SpelMessage.TYPE_CONVERSION_ERROR, sourceType.toString(), targetType.toString()); + catch (ConverterNotFoundException ex) { + throw new SpelEvaluationException( + ex, SpelMessage.TYPE_CONVERSION_ERROR, sourceType.toString(), targetType.toString()); } - catch (ConversionException ce) { - throw new SpelEvaluationException(ce, SpelMessage.TYPE_CONVERSION_ERROR, sourceType.toString(), targetType.toString()); + catch (ConversionException ex) { + throw new SpelEvaluationException( + ex, SpelMessage.TYPE_CONVERSION_ERROR, sourceType.toString(), targetType.toString()); } } diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterQueryTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterQueryTests.java index 73e9b46ff59..271caa93753 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterQueryTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterQueryTests.java @@ -173,11 +173,11 @@ public class NamedParameterQueryTests { parms.addValue("id", 3); Object o = template.queryForObject("SELECT AGE FROM CUSTMR WHERE ID = :id", parms, new RowMapper() { - @Override - public Object mapRow(ResultSet rs, int rowNum) throws SQLException { - return rs.getInt(1); - } - }); + @Override + public Object mapRow(ResultSet rs, int rowNum) throws SQLException { + return rs.getInt(1); + } + }); assertTrue("Correct result type", o instanceof Integer); verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?"); @@ -225,7 +225,7 @@ public class NamedParameterQueryTests { given(resultSet.getInt(1)).willReturn(22); MapSqlParameterSource parms = new MapSqlParameterSource(); - parms.addValue("ids", Arrays.asList(new Object[] { 3, 4 })); + parms.addValue("ids", Arrays.asList(3, 4)); Object o = template.queryForObject(sql, parms, Integer.class); assertTrue("Correct result type", o instanceof Integer); @@ -241,8 +241,8 @@ public class NamedParameterQueryTests { MapSqlParameterSource parms = new MapSqlParameterSource(); List l1 = new ArrayList(); - l1.add(new Object[] { 3, "Rod" }); - l1.add(new Object[] { 4, "Juergen" }); + l1.add(new Object[] {3, "Rod"}); + l1.add(new Object[] {4, "Juergen"}); parms.addValue("multiExpressionList", l1); Object o = template.queryForObject( "SELECT AGE FROM CUSTMR WHERE (ID, NAME) IN (:multiExpressionList)", diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterUtilsTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterUtilsTests.java index 2360b808405..0b8ab5afcd4 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterUtilsTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterUtilsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 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. @@ -20,11 +20,12 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; -import static org.junit.Assert.*; import org.junit.Test; import org.springframework.dao.InvalidDataAccessApiUsageException; +import static org.junit.Assert.*; + /** * @author Thomas Risberg * @author Juergen Hoeller @@ -117,7 +118,7 @@ public class NamedParameterUtilsTests { @Test(expected = InvalidDataAccessApiUsageException.class) public void buildValueArrayWithMissingParameterValue() throws Exception { String sql = "select count(0) from foo where id = :id"; - NamedParameterUtils.buildValueArray(sql, new HashMap()); + NamedParameterUtils.buildValueArray(sql, Collections.emptyMap()); } @Test @@ -168,7 +169,7 @@ public class NamedParameterUtilsTests { String sql4 = "/*+ HINT */ xxx /* comment :a ? */ :a yyyy :b :c :a zzzzz /* :xx XX*"; ParsedSql psql4 = NamedParameterUtils.parseSqlStatement(sql4); - Map parameters = Collections.singletonMap("a", "0"); + Map parameters = Collections.singletonMap("a", "0"); assertEquals("/*+ HINT */ xxx /* comment :a ? */ ? yyyy ? ? ? zzzzz /* :xx XX*", NamedParameterUtils.substituteNamedParameters(psql4, new MapSqlParameterSource(parameters))); } @@ -235,7 +236,6 @@ public class NamedParameterUtilsTests { assertEquals(0, parsedSql2.getParameterNames().size()); String finalSql2 = NamedParameterUtils.substituteNamedParameters(parsedSql2, null); assertEquals(expectedSql2, finalSql2); - } /* @@ -294,4 +294,5 @@ public class NamedParameterUtilsTests { assertEquals(1, psql2.getTotalParameterCount()); assertEquals("xxx", psql2.getParameterNames().get(0)); } + } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessor.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessor.java index edb113e6079..4f02a0b7352 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessor.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessor.java @@ -61,18 +61,16 @@ public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodPro super(messageConverters, contentNegotiationManager); } + public boolean supportsParameter(MethodParameter parameter) { - Class parameterType = parameter.getParameterType(); - return HttpEntity.class.equals(parameterType); + return HttpEntity.class.equals(parameter.getParameterType()); } public boolean supportsReturnType(MethodParameter returnType) { - Class parameterType = returnType.getParameterType(); - return HttpEntity.class.isAssignableFrom(parameterType) || ResponseEntity.class.isAssignableFrom(parameterType); + return HttpEntity.class.isAssignableFrom(returnType.getParameterType()); } - public Object resolveArgument( - MethodParameter parameter, ModelAndViewContainer mavContainer, + public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws IOException, HttpMediaTypeNotSupportedException { @@ -85,22 +83,21 @@ public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodPro private Type getHttpEntityType(MethodParameter parameter) { Assert.isAssignable(HttpEntity.class, parameter.getParameterType()); - ParameterizedType type = (ParameterizedType) parameter.getGenericParameterType(); - if (type.getActualTypeArguments().length == 1) { - return type.getActualTypeArguments()[0]; + Type parameterType = parameter.getGenericParameterType(); + if (parameterType instanceof ParameterizedType) { + ParameterizedType type = (ParameterizedType) parameterType; + if (type.getActualTypeArguments().length == 1) { + return type.getActualTypeArguments()[0]; + } } - throw new IllegalArgumentException("HttpEntity parameter (" - + parameter.getParameterName() + ") in method " + parameter.getMethod() - + " is not parameterized or has more than one parameter"); + throw new IllegalArgumentException("HttpEntity parameter '" + parameter.getParameterName() + + "' in method " + parameter.getMethod() + " is not parameterized or has more than one parameter"); } - public void handleReturnValue( - Object returnValue, MethodParameter returnType, - ModelAndViewContainer mavContainer, NativeWebRequest webRequest) - throws Exception { + public void handleReturnValue(Object returnValue, MethodParameter returnType, + ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception { mavContainer.setRequestHandled(true); - if (returnValue == null) { return; } @@ -124,7 +121,7 @@ public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodPro writeWithMessageConverters(body, returnType, inputMessage, outputMessage); } else { - // flush headers to the HttpServletResponse + // Flush headers to the HttpServletResponse outputMessage.getBody(); } }