fix NPE/coercion error when passing null first argument FELIX-2432


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@956547 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Reflective.java b/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Reflective.java
index 65a7e61..7df0e17 100644
--- a/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Reflective.java
+++ b/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Reflective.java
@@ -171,7 +171,7 @@
                 {
                     params.append(", ");
                 }
-                params.append(arg.getClass().getSimpleName());
+                params.append(arg == null ? "null" : arg.getClass().getSimpleName());
             }
 
             throw new IllegalArgumentException(String.format(
@@ -275,6 +275,13 @@
                 else
                 {
                     out[i] = coerce(session, target, types[i], in.get(0));
+                    
+                    if (out[i] == null && types[i].isArray() && in.size() > 0)
+                    {
+                        // don't coerce null to array FELIX-2432
+                        out[i] = NO_MATCH;
+                    }
+                    
                     if (out[i] != NO_MATCH)
                     {
                         in.remove(0);
diff --git a/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestParser2.java b/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestParser2.java
index 162b37d..a21e0a1 100644
--- a/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestParser2.java
+++ b/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestParser2.java
@@ -35,7 +35,7 @@
 
         assertEquals("file://wibble#tag", c.execute("echo file://wibble#tag"));
         assertEquals("file:", c.execute("echo file: //wibble#tag"));
-        
+
         assertEquals("PWD/*.java", c.execute("echo PWD/*.java"));
         try
         {
@@ -46,31 +46,37 @@
         {
             // expected
         }
-        
+
         assertEquals("ok", c.execute("// can't quote\necho ok\n"));
-        
+
         // quote in comment in closure
         assertEquals("ok", c.execute("x = { // can't quote\necho ok\n}; x"));
         assertEquals("ok", c.execute("x = {\n// can't quote\necho ok\n}; x"));
         assertEquals("ok", c.execute("x = {// can't quote\necho ok\n}; x"));
     }
 
+    public void testCoercion() throws Exception
+    {
+        Context c = new Context();
+        c.addCommand("echo", this);
+
+        // FELIX-2432
+        assertEquals("null x", c.execute("echo $expandsToNull x"));
+    }
+
     public CharSequence echo(Object args[])
     {
         if (args == null)
         {
-            return "";
+            return "null args!";
         }
 
         StringBuilder sb = new StringBuilder();
         for (Object arg : args)
         {
-            if (arg != null)
-            {
-                if (sb.length() > 0)
-                    sb.append(' ');
-                sb.append(arg);
-            }
+            if (sb.length() > 0)
+                sb.append(' ');
+            sb.append(String.valueOf(arg));
         }
         return sb.toString();
     }