Author: davidb
Date: Wed Jun 29 13:36:59 2016
New Revision: 1750633

URL: http://svn.apache.org/viewvc?rev=1750633&view=rev
Log:
Better support for Character array conversions.

Modified:
    
felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
    
felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/ConverterServiceTest.java

Modified: 
felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java?rev=1750633&r1=1750632&r2=1750633&view=diff
==============================================================================
--- 
felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
 (original)
+++ 
felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
 Wed Jun 29 13:36:59 2016
@@ -124,7 +124,8 @@ public class ConvertingImpl implements C
         Class<?> targetCls = primitiveToBoxed(cls);
 
         if (!Map.class.isAssignableFrom(targetCls) &&
-                !Collections.class.isAssignableFrom(targetCls)) {
+                !Collections.class.isAssignableFrom(targetCls) &&
+                !targetCls.isArray()) {
             // For maps and collections we always want copies returned
             if (targetCls.isAssignableFrom(object.getClass()))
                 return object;
@@ -145,7 +146,7 @@ public class ConvertingImpl implements C
         // At this point we know that the target is a 'singular' type: not a 
map, collection or array
         if (object instanceof Collection) {
             return convertCollectionToSingleValue(cls);
-        } else if (object instanceof Object[]) {
+        } else if ((object = asBoxedArray(object)) instanceof Object[]) {
             return convertArrayToSingleValue(cls);
         }
 
@@ -415,12 +416,37 @@ public class ConvertingImpl implements C
     }
 
     private static Collection<?> collectionView(Object obj) {
+        if (obj == null)
+            return null;
+
+        Collection<?> c = asCollection(obj);
+        if (c == null)
+            return Collections.singleton(obj);
+        else
+            return c;
+    }
+
+    private static Collection<?> asCollection(Object obj) {
         if (obj instanceof Collection)
             return (Collection<?>) obj;
-        else if (obj instanceof Object[])
+        else if ((obj = asBoxedArray(obj)) instanceof Object[])
             return Arrays.asList((Object[]) obj);
         else
-            return Collections.singleton(obj);
+            return null;
+    }
+
+    private static Object asBoxedArray(Object obj) {
+        Class<?> objClass = obj.getClass();
+        if (!objClass.isArray())
+            return obj;
+
+        int len = Array.getLength(obj);
+        Object arr = Array.newInstance(Object.class, len);
+        for (int i=0; i<len; i++) {
+            Object val = Array.get(obj, i);
+            Array.set(arr, i, val);
+        }
+        return arr;
     }
 
     @SuppressWarnings("rawtypes")

Modified: 
felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/ConverterServiceTest.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/ConverterServiceTest.java?rev=1750633&r1=1750632&r2=1750633&view=diff
==============================================================================
--- 
felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/ConverterServiceTest.java
 (original)
+++ 
felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/ConverterServiceTest.java
 Wed Jun 29 13:36:59 2016
@@ -47,6 +47,7 @@ import org.osgi.service.converter.TypeRe
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotSame;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
@@ -239,6 +240,22 @@ public class ConverterServiceTest {
     }
 
     @Test
+    public void testCharArrayConversion() {
+        char[] ca = converter.convert(new int[] {9,8,7}).to(char[].class);
+        assertArrayEquals(new char[] {9,8,7}, ca);
+        Character[] ca2 = converter.convert((long) 17).to(Character[].class);
+        assertArrayEquals(new Character[] {(char)17}, ca2);
+        char[] ca3 = converter.convert(new short[] {257}).to(char[].class);
+        assertArrayEquals(new char[] {257}, ca3);
+        char c = converter.convert(new char[] {'x', 'y'}).to(char.class);
+        assertEquals('x', c);
+        char[] ca4a = {'x', 'y'};
+        char[] ca4b = converter.convert(ca4a).to(char[].class);
+        assertArrayEquals(new char [] {'x', 'y'}, ca4b);
+        assertNotSame("Should have created a new instance", ca4a, ca4b);
+    }
+
+    @Test
     public void testStandardStringArrayConversion() {
         String[] sa = {"A", "B"};
         assertEquals("A", converter.convert(sa).toString());


Reply via email to