Author: gnodet
Date: Thu May 17 13:08:30 2018
New Revision: 1831780

URL: http://svn.apache.org/viewvc?rev=1831780&view=rev
Log:
[FELIX-5856] Coercion between Object[] and List

Modified:
    
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Reflective.java
    
felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestCoercion.java

Modified: 
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Reflective.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Reflective.java?rev=1831780&r1=1831779&r2=1831780&view=diff
==============================================================================
--- 
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Reflective.java
 (original)
+++ 
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Reflective.java
 Thu May 17 13:08:30 2018
@@ -23,6 +23,7 @@ import java.lang.reflect.Array;
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.util.AbstractList;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -434,6 +435,29 @@ public final class Reflective
             return arg;
         }
 
+        if (type.isArray() && arg instanceof Collection)
+        {
+            return ((Collection) arg).toArray();
+        }
+
+        if (type.isAssignableFrom(List.class) && arg.getClass().isArray())
+        {
+            return new AbstractList<Object>()
+            {
+                @Override
+                public Object get(int index)
+                {
+                    return Array.get(arg, index);
+                }
+
+                @Override
+                public int size()
+                {
+                    return Array.getLength(arg);
+                }
+            };
+        }
+
         if (type.isArray())
         {
             return NO_MATCH;

Modified: 
felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestCoercion.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestCoercion.java?rev=1831780&r1=1831779&r2=1831780&view=diff
==============================================================================
--- 
felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestCoercion.java
 (original)
+++ 
felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestCoercion.java
 Thu May 17 13:08:30 2018
@@ -18,6 +18,9 @@
  */
 package org.apache.felix.gogo.runtime;
 
+import java.util.Arrays;
+import java.util.List;
+
 import org.apache.felix.service.command.CommandSession;
 import org.apache.felix.service.command.Descriptor;
 import org.apache.felix.service.command.Parameter;
@@ -129,6 +132,16 @@ public class TestCoercion extends Abstra
         return loc;
     }
 
+    public String methodarray(Object[] array)
+    {
+        return Arrays.toString(array);
+    }
+
+    public String methodlist(List<Object> array)
+    {
+        return array.toString();
+    }
+
     @Test
     public void testBestCoercion() throws Exception
     {
@@ -150,6 +163,26 @@ public class TestCoercion extends Abstra
         assertEquals("mymethod 1.10", "1.10", c.execute("mymethod 1.10"));
     }
 
+    @Test
+    public void testListToArray() throws Exception
+    {
+        Context c = new Context();
+        c.addCommand("methodarray", this);
+        c.set("a", Arrays.asList(1, 3));
+
+        assertEquals("methodarray [1 3]", "[1, 3]", c.execute("methodarray 
$a"));
+    }
+
+    @Test
+    public void testArrayToList() throws Exception
+    {
+        Context c = new Context();
+        c.addCommand("methodlist", this);
+        c.set("a", new int[] { 1, 3 });
+
+        assertEquals("methodlist [1 3]", "[1, 3]", c.execute("methodlist $a"));
+    }
+
     @Descriptor("list all installed bundles")
     public String p0(
         @Descriptor("show location") @Parameter(names = { "-l", "--location" 
}, presentValue = "true", absentValue = "false") boolean showLoc,


Reply via email to