better message/exception when arguments can't be coerced (FELIX-2375)


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@950110 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 14a5e4c..65a7e61 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
@@ -99,19 +99,16 @@
                 }
 
                 // Check if the command takes a session
-                if ((types.length > 0) && CommandSession.class.isAssignableFrom(types[0]))
+                if ((types.length > 0) && types[0].isInterface()
+                    && types[0].isAssignableFrom(session.getClass()))
                 {
                     xargs.add(0, session);
                 }
 
                 Object[] parms = new Object[types.length];
-                // if (types.length >= args.size() ) {
                 int local = coerce(session, target, m, types, parms, xargs);
                 if ((local >= xargs.size()) && (local >= types.length))
-                { // derek
-                    // -
-                    // stop
-                    // no-args
+                {
                     boolean exact = ((local == xargs.size()) && (local == types.length));
                     if (exact || (local > match))
                     {
@@ -128,21 +125,12 @@
                 {
                     possibleTypes.add(types);
                 }
-                // }
-                // if (match == -1 && types.length == 1
-                // && types[0] == Object[].class) {
-                // bestMethod = m;
-                // Object value = args.toArray();
-                // bestArgs = new Object[] { value };
-                // }
             }
         }
 
         if (bestMethod != null)
         {
             bestMethod.setAccessible(true);
-            // derek: BUGFIX catch InvocationTargetException
-            // return bestMethod.invoke(target, bestArgs);
             try
             {
                 return bestMethod.invoke(target, bestArgs);
@@ -159,9 +147,6 @@
         }
         else
         {
-            // throw new IllegalArgumentException("Cannot find command:" + name
-            // + " with args:" + args);
-            // { derek
             ArrayList<String> list = new ArrayList<String>();
             for (Class<?>[] types : possibleTypes)
             {
@@ -179,9 +164,18 @@
                 list.add(buf.toString());
             }
 
+            StringBuilder params = new StringBuilder();
+            for (Object arg : args)
+            {
+                if (params.length() > 1)
+                {
+                    params.append(", ");
+                }
+                params.append(arg.getClass().getSimpleName());
+            }
+
             throw new IllegalArgumentException(String.format(
-                "Cannot coerce %s%s to any of %s", name, args, list));
-            // } derek
+                "Cannot coerce %s(%s) to any of %s", name, params, list));
         }
     }
 
@@ -215,7 +209,8 @@
                 if (as[a] instanceof Parameter)
                 {
                     Parameter o = (Parameter) as[a];
-                    out[argIndex] = coerce(session, target, types[argIndex], o.absentValue());
+                    out[argIndex] = coerce(session, target, types[argIndex],
+                        o.absentValue());
                     start = argIndex + 1;
                 }
             }
@@ -273,8 +268,6 @@
             try
             {
                 // Try to convert one argument
-                // derek: add empty array as extra argument
-                // out[i] = coerce(session, target, types[i], in.get(i));
                 if (in.size() == 0)
                 {
                     out[i] = NO_MATCH;