You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by gr...@apache.org on 2007/09/29 07:36:21 UTC
svn commit: r580546 - in /tapestry/tapestry5/trunk/tapestry-ioc/src:
main/java/org/apache/tapestry/ioc/services/TapestryIOCModule.java
test/java/org/apache/tapestry/ioc/internal/services/TypeCoercerImplTest.java
Author: gredler
Date: Fri Sep 28 22:36:19 2007
New Revision: 580546
URL: http://svn.apache.org/viewvc?rev=580546&view=rev
Log:
TAPESTRY-1702: Missing coercion from primitive arrays to List.
Modified:
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/services/TapestryIOCModule.java
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry/ioc/internal/services/TypeCoercerImplTest.java
Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/services/TapestryIOCModule.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/services/TapestryIOCModule.java?rev=580546&r1=580545&r2=580546&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/services/TapestryIOCModule.java (original)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/services/TapestryIOCModule.java Fri Sep 28 22:36:19 2007
@@ -16,6 +16,7 @@
import static org.apache.tapestry.ioc.IOCConstants.PERTHREAD_SCOPE;
+import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Arrays;
@@ -143,6 +144,7 @@
* <li>Null to String (still null)</li>
* <li>Collection to Boolean (false if empty)</li>
* <li>Object[] to List</li>
+ * <li>primitive[] to List</li>
* <li>Object to List (by wrapping as a singleton list)</li>
* <li>Null to List (still null)</li>
* <li>Null to Long (zero)</li>
@@ -157,7 +159,7 @@
* "weaker" than other coercions. Alternately, coercions from null may need to be handled
* specially. We'll see if we tweak the algorithm in the future.
*/
-
+ @SuppressWarnings("unchecked")
public static void contributeTypeCoercer(Configuration<CoercionTuple> configuration)
{
add(configuration, Object.class, String.class, new Coercion<Object, String>()
@@ -375,6 +377,29 @@
return input.doubleValue();
}
});
+
+ Coercion primitiveArrayCoercion = new Coercion<Object, List>()
+ {
+ public List<Object> coerce(Object input)
+ {
+ int length = Array.getLength(input);
+ Object[] array = new Object[length];
+ for (int i = 0; i < length; i++)
+ {
+ array[i] = Array.get(input, i);
+ }
+ return Arrays.asList(array);
+ }
+ };
+
+ add(configuration, byte[].class, List.class, primitiveArrayCoercion);
+ add(configuration, short[].class, List.class, primitiveArrayCoercion);
+ add(configuration, int[].class, List.class, primitiveArrayCoercion);
+ add(configuration, long[].class, List.class, primitiveArrayCoercion);
+ add(configuration, float[].class, List.class, primitiveArrayCoercion);
+ add(configuration, double[].class, List.class, primitiveArrayCoercion);
+ add(configuration, char[].class, List.class, primitiveArrayCoercion);
+ add(configuration, boolean[].class, List.class, primitiveArrayCoercion);
}
private static <S, T> void add(Configuration<CoercionTuple> configuration, Class<S> sourceType,
Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry/ioc/internal/services/TypeCoercerImplTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry/ioc/internal/services/TypeCoercerImplTest.java?rev=580546&r1=580545&r2=580546&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry/ioc/internal/services/TypeCoercerImplTest.java (original)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry/ioc/internal/services/TypeCoercerImplTest.java Fri Sep 28 22:36:19 2007
@@ -140,6 +140,8 @@
// compound tuples (built around specific tuples).
Float floatValue = new Float(31.14);
+ byte byte1 = 12, byte2 = 56;
+ short short1 = 34, short2 = 98;
return new Object[][]
{
// There's a lot of these!
@@ -175,10 +177,16 @@
{ null, List.class, null },
{ null, Collection.class, null },
{ null, String.class, null },
- { new Object[]
- { "a", 123 }, List.class, Arrays.asList("a", 123) },
- { new String[]
- { "a", "b" }, List.class, Arrays.asList("a", "b") },
+ { new Object[] { "a", 123 }, List.class, Arrays.asList("a", 123) },
+ { new String[] { "a", "b" }, List.class, Arrays.asList("a", "b") },
+ { new byte[] { byte1, byte2 }, List.class, Arrays.asList(byte1, byte2) },
+ { new short[] { short1, short2 }, List.class, Arrays.asList(short1, short2) },
+ { new int[] { 1, 2 }, List.class, Arrays.asList(1, 2) },
+ { new long[] { 123L, 321L }, List.class, Arrays.asList(123L, 321L) },
+ { new float[] { 3.4f, 7.777f }, List.class, Arrays.asList(3.4f, 7.777f) },
+ { new double[] { 3.4, 7.777 }, List.class, Arrays.asList(3.4, 7.777) },
+ { new char[] { 'a', 'b' }, List.class, Arrays.asList('a', 'b') },
+ { new boolean[] { true, false }, List.class, Arrays.asList(true, false) },
{ null, Long.class, 0l },
{ null, Short.class, (short) 0 },