On 5/3/10 7:50 PM, Derek Baum wrote:
Hi,

I'm formatting Felix code using the Apache Felix Eclipse Template (linked from the bottom of this page https://cwiki.apache.org/confluence/display/FELIX/Coding+Standards) and I get significantly different results when reformatting gogo, specifically Reflective.java.

I attach a diff showing the differences, they are mostly due to different handling of breaking long lines, for example:

- public final static Set<String> KEYWORDS = new HashSet<String>(Arrays.asList(new String[]
-        {
-            "abstract", "continue", "for", "new",
+    public final static Set<String> KEYWORDS = new HashSet<String>(
+ Arrays.asList(new String[] { "abstract", "continue", "for", "new", "switch",

-    public Object method(
- CommandSession session, Object target, String name, List<Object> args)
-        throws IllegalArgumentException, IllegalAccessException,
-            InvocationTargetException, Exception
+ public Object method(CommandSession session, Object target, String name, + List<Object> args) throws IllegalArgumentException, IllegalAccessException,
+        InvocationTargetException, Exception

-                if (mname.equals(name)
-                    || mname.equals(get)
-                    || mname.equals(set)
-                    || mname.equals(is)
-                    || mname.equals(MAIN))
+ if (mname.equals(name) || mname.equals(get) || mname.equals(set)
+                    || mname.equals(is) || mname.equals(MAIN))

I'm happy to use something other than Eclipse to format Felix code, but I'd like to be able to automatically get the same results as other Felix committers! How do you format your code?

I use NetBeans. We don't really have strict rules for long line. The main rules are for placement of braces, spacing, etc.

p.s. I don't think it's possible to change the Felix Eclipse Template to produce the same results as your formatter, as their is not much control over how the Eclipse formatter breaks long lines.

I probably hand modified some of those long lines myself.

-> richard






On 3 May 2010 22:18, <rickh...@apache.org <mailto:rickh...@apache.org>> wrote:

    Author: rickhall
    Date: Mon May  3 21:18:10 2010
    New Revision: 940623

    URL: http://svn.apache.org/viewvc?rev=940623&view=rev
    <http://svn.apache.org/viewvc?rev=940623&view=rev>
    Log:
    Reformat code according to style guide.

    Modified:
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Closure.java felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Command.java felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandProxy.java felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandSessionImpl.java felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandShellImpl.java felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Parser.java felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Pipe.java felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Reflective.java felix/trunk/gogo/runtime/src/main/java/org/osgi/service/command/Flag.java felix/trunk/gogo/runtime/src/main/java/org/osgi/service/command/Option.java

    Modified:
    
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Closure.java
    URL:
    
http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Closure.java?rev=940623&r1=940622&r2=940623&view=diff
    
<http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Closure.java?rev=940623&r1=940622&r2=940623&view=diff>
    
==============================================================================
    ---
    
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Closure.java
    (original)
    +++
    
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Closure.java
    Mon May  3 21:18:10 2010
    @@ -487,7 +487,9 @@ public class Closure extends Reflective
                {
                    int i = name.charAt(0) - '0';
                    if (i > 0)
    +                {
                        return parms.get(i - 1);
    +                }
                }
            }
            return session.get(name);

    Modified:
    
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Command.java
    URL:
    
http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Command.java?rev=940623&r1=940622&r2=940623&view=diff
    
<http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Command.java?rev=940623&r1=940622&r2=940623&view=diff>
    
==============================================================================
    ---
    
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Command.java
    (original)
    +++
    
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Command.java
    Mon May  3 21:18:10 2010
    @@ -40,5 +40,4 @@ public class Command extends Reflective
        {
            return method(session, target, function, arguments);
        }
    -
    -}
    +}
    \ No newline at end of file

    Modified:
    
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandProxy.java
    URL:
    
http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandProxy.java?rev=940623&r1=940622&r2=940623&view=diff
    
<http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandProxy.java?rev=940623&r1=940622&r2=940623&view=diff>
    
==============================================================================
    ---
    
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandProxy.java
    (original)
    +++
    
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandProxy.java
    Mon May  3 21:18:10 2010
    @@ -27,7 +27,6 @@ import org.osgi.framework.BundleContext;

     public class CommandProxy extends Reflective implements Function
     {
    -
        BundleContext context;
        ServiceReference reference;
        String function;
    @@ -59,4 +58,4 @@ public class CommandProxy extends Reflec
                context.ungetService(reference);
            }
        }
    -}
    +}
    \ No newline at end of file

    Modified:
    
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandSessionImpl.java
    URL:
    
http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandSessionImpl.java?rev=940623&r1=940622&r2=940623&view=diff
    
<http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandSessionImpl.java?rev=940623&r1=940622&r2=940623&view=diff>
    
==============================================================================
    ---
    
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandSessionImpl.java
    (original)
    +++
    
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandSessionImpl.java
    Mon May  3 21:18:10 2010
    @@ -34,9 +34,7 @@ public class CommandSessionImpl implemen
     {
        public static final String VARIABLES = ".variables";
        public static final String COMMANDS = ".commands";
    -
        private static final String COLUMN = "%-20s %s\n";
    -
        InputStream in;
        PrintStream out;
        PrintStream err;
    @@ -226,13 +224,13 @@ public class CommandSessionImpl implemen
                    {
                        StringBuilder sb = new StringBuilder();
                        Collection<?> c = (Collection<?>) target;
    -                                       sb.append("[");
    +                    sb.append("[");
                        for (Object o : c)
                        {
    -                                               if (sb.length() > 1)
    -                                               {
- sb.append(", ");
    -                                               }
    +                        if (sb.length() > 1)
    +                        {
    +                            sb.append(", ");
    +                        }
                            sb.append(format(o, level + 1, this));
                        }
                        sb.append("]");
    @@ -275,13 +273,13 @@ public class CommandSessionImpl implemen
                    {
                        StringBuilder sb = new StringBuilder();
                        Map<?, ?> c = (Map<?, ?>) target;
    -                                       sb.append("[");
    +                    sb.append("[");
                        for (Map.Entry<?, ?> entry : c.entrySet())
                        {
    -                                               if (sb.length() > 1)
    -                                               {
- sb.append(", ");
    -                                               }
    +                        if (sb.length() > 1)
    +                        {
    +                            sb.append(", ");
    +                        }
                            sb.append(format(entry, level + 1, this));
                        }
                        sb.append("]");
    @@ -355,5 +353,4 @@ public class CommandSessionImpl implemen
                return "<can not format " + result + ":" + e;
            }
        }
    -
    -}
    +}
    \ No newline at end of file

    Modified:
    
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandShellImpl.java
    URL:
    
http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandShellImpl.java?rev=940623&r1=940622&r2=940623&view=diff
    
<http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandShellImpl.java?rev=940623&r1=940622&r2=940623&view=diff>
    
==============================================================================
    ---
    
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandShellImpl.java
    (original)
    +++
    
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandShellImpl.java
    Mon May  3 21:18:10 2010
    @@ -47,7 +47,6 @@ public class CommandShellImpl implements

        public CommandSession createSession(InputStream in,
    PrintStream out, PrintStream err)
        {
    -
            return new CommandSessionImpl(this, in, out, err);
        }

    @@ -220,5 +219,4 @@ public class CommandShellImpl implements
            }
            return null;
        }
    -
    -}
    +}
    \ No newline at end of file

    Modified:
    
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Parser.java
    URL:
    
http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Parser.java?rev=940623&r1=940622&r2=940623&view=diff
    
<http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Parser.java?rev=940623&r1=940622&r2=940623&view=diff>
    
==============================================================================
    ---
    
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Parser.java
    (original)
    +++
    
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Parser.java
    Mon May  3 21:18:10 2010
    @@ -122,8 +122,8 @@ public class Parser
                        current += 4;
                        break;
                    default:
    -                    // We just take the next character literally
    -                    // but have the escaped flag set, important
    for {},[] etc
    +                // We just take the next character literally
    +                // but have the escaped flag set, important for
    {},[] etc
                }
            }
            if (!increment)
    @@ -422,7 +422,7 @@ public class Parser
            }
            throw new IllegalArgumentException(
                "Reference to variable does not match syntax of a
    variable: "
    -                + context(start));
    +            + context(start));
        }

        public String toString()
    @@ -439,4 +439,4 @@ public class Parser
            }
            return sb.toString();
        }
    -}
    +}
    \ No newline at end of file

    Modified:
    
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Pipe.java
    URL:
    
http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Pipe.java?rev=940623&r1=940622&r2=940623&view=diff
    
<http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Pipe.java?rev=940623&r1=940622&r2=940623&view=diff>
    
==============================================================================
    ---
    
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Pipe.java
    (original)
    +++
    
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Pipe.java
    Mon May  3 21:18:10 2010
    @@ -130,4 +130,4 @@ public class Pipe extends Thread
                }
            }
        }
    -}
    +}
    \ No newline at end of file

    Modified:
    
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Reflective.java
    URL:
    
http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Reflective.java?rev=940623&r1=940622&r2=940623&view=diff
    
<http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Reflective.java?rev=940623&r1=940622&r2=940623&view=diff>
    
==============================================================================
    ---
    
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Reflective.java
    (original)
    +++
    
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Reflective.java
    Mon May  3 21:18:10 2010
    @@ -24,345 +24,448 @@ import java.util.*;

     import org.osgi.service.command.*;

    -public class Reflective {
    -       public final static Object NO_MATCH = new Object();
    -       public final static Set<String> KEYWORDS = new
    HashSet<String>(Arrays
    -                       .asList(new String[] { "abstract",
    "continue", "for", "new",
    -                                       "switch", "assert",
    "default", "goto", "package",
    -                                       "synchronized", "boolean",
    "do", "if", "private", "this",
    -                                       "break", "double",
    "implements", "protected", "throw",
    -                                       "byte", "else", "import",
    "public", "throws", "case",
    -                                       "enum", "instanceof",
    "return", "transient", "catch",
    -                                       "extends", "int", "short",
    "try", "char", "final",
    -                                       "interface", "static",
    "void", "class", "finally", "long",
    -                                       "strictfp", "volatile",
    "const", "float", "native",
    -                                       "super", "while" }));
    -       public final static String MAIN = "_main";
    -
    -       public Object method(CommandSession session, Object
    target, String name,
    -                       List<Object> args) throws
    IllegalArgumentException,
    -                       IllegalAccessException,
    InvocationTargetException, Exception {
    -               Method[] methods = target.getClass().getMethods();
    -               name = name.toLowerCase();
    -
    -               String get = "get" + name;
    -               String is = "is" + name;
    -               String set = "set" + name;
    -
    -               if (KEYWORDS.contains(name)) {
    -                       name = "_" + name;
    -               }
    -
    -               if (target instanceof Class) {
    -                       Method[] staticMethods = ((Class<?>)
    target).getMethods();
    -                       for (Method m : staticMethods) {
    -                               String mname =
    m.getName().toLowerCase();
    -                               if (mname.equals(name) ||
    mname.equals(get)
    -                                               ||
    mname.equals(set) || mname.equals(is)
    -                                               ||
    mname.equals(MAIN)) {
    -                                       methods = staticMethods;
    -                                       break;
    -                               }
    -                       }
    -               }
    -
    -               Method bestMethod = null;
    -               Object[] bestArgs = null;
    -               int match = -1;
    -               ArrayList<Class<?>[]> possibleTypes = new
    ArrayList<Class<?>[]>();
    -
    -               for (Method m : methods) {
    -                       String mname = m.getName().toLowerCase();
    -                       if (mname.equals(name) ||
    mname.equals(get) || mname.equals(set)
    -                                       || mname.equals(is) ||
    mname.equals(MAIN)) {
    -                               Class<?>[] types =
    m.getParameterTypes();
    -                               ArrayList<Object> xargs = new
    ArrayList<Object>(args);
    -
    -                               // pass command name as argv[0] to
    main, so it can handle
    -                               // multiple commands
    -                               if (mname.equals(MAIN)) {
    -                                       xargs.add(0, name);
    -                               }
    -
    -                               // Check if the command takes a
    session
    -                               if (types.length > 0
    - && CommandSession.class.isAssignableFrom(types[0])) {
    -                                       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) {
    -                                               bestMethod = m;
    -                                               bestArgs = parms;
    -                                               match = local;
    -                                       }
    -                                       if (exact) {
    -                                               break;
    -                                       }
    -                               } else {
    -                                       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);
    -                       } catch (InvocationTargetException e) {
    -                               Throwable cause = e.getCause();
    -                               if (cause instanceof Exception) {
    -                                       throw (Exception) cause;
    -                               }
    -                               throw e;
    -                       }
    -               } else {
    -                       // throw new
    IllegalArgumentException("Cannot find command:" + name
    -                       // + " with args:" + args);
    -                       // { derek
    -                       ArrayList<String> list = new
    ArrayList<String>();
    -                       for (Class<?>[] types : possibleTypes) {
    -                               StringBuilder buf = new
    StringBuilder();
    -                               buf.append('(');
    -                               for (Class<?> type : types) {
    -                                       if (buf.length() > 1) {
    -                                               buf.append(", ");
    -                                       }
- buf.append(type.getSimpleName());
    -                               }
    -                               buf.append(')');
    -                               list.add(buf.toString());
    -                       }
    -
    -                       throw new
    IllegalArgumentException(String.format(
    -                                       "Cannot coerce %s%s to any
    of %s", name, args, list));
    -                       // } derek
    -               }
    -       }
    -
    -       /**
    -        * Complex routein to convert the arguments given from the
    command line to
    -        * the arguments of the method call. First, an attempt is
    made to convert
    -        * each argument. If this fails, a check is made to see if
    varargs can be
    -        * applied. This happens when the last method argument is
    an array.
    -        *
    -        * @param session
    -        * @param target
    -        * @param m
    -        * @param types
    -        * @param out
    -        * @param in
    -        * @return
    -        * @throws Exception
    -        */
    -       @SuppressWarnings("unchecked")
    -    private int coerce(CommandSession session, Object target,
    Method m,
    -                       Class<?> types[], Object out[],
    List<Object> in) throws Exception {
    -               Annotation[][] pas = m.getParameterAnnotations();
    +public class Reflective
    +{
    +    public final static Object NO_MATCH = new Object();
    +    public final static Set<String> KEYWORDS = new
    HashSet<String>(Arrays.asList(new String[]
    +        {
    +            "abstract", "continue", "for", "new",
    +            "switch", "assert", "default", "goto", "package",
    +            "synchronized", "boolean", "do", "if", "private", "this",
    +            "break", "double", "implements", "protected", "throw",
    +            "byte", "else", "import", "public", "throws", "case",
    +            "enum", "instanceof", "return", "transient", "catch",
    +            "extends", "int", "short", "try", "char", "final",
    +            "interface", "static", "void", "class", "finally",
    "long",
    +            "strictfp", "volatile", "const", "float", "native",
    +            "super", "while"
    +        }));
    +    public final static String MAIN = "_main";
    +
    +    public Object method(
    +        CommandSession session, Object target, String name,
    List<Object> args)
    +        throws IllegalArgumentException, IllegalAccessException,
    +            InvocationTargetException, Exception
    +    {
    +        Method[] methods = target.getClass().getMethods();
    +        name = name.toLowerCase();
    +
    +        String get = "get" + name;
    +        String is = "is" + name;
    +        String set = "set" + name;
    +
    +        if (KEYWORDS.contains(name))
    +        {
    +            name = "_" + name;
    +        }
    +
    +        if (target instanceof Class)
    +        {
    +            Method[] staticMethods = ((Class<?>)
    target).getMethods();
    +            for (Method m : staticMethods)
    +            {
    +                String mname = m.getName().toLowerCase();
    +                if (mname.equals(name)
    +                    || mname.equals(get)
    +                    || mname.equals(set)
    +                    || mname.equals(is)
    +                    || mname.equals(MAIN))
    +                {
    +                    methods = staticMethods;
    +                    break;
    +                }
    +            }
    +        }
    +
    +        Method bestMethod = null;
    +        Object[] bestArgs = null;
    +        int match = -1;
    +        ArrayList<Class<?>[]> possibleTypes = new
    ArrayList<Class<?>[]>();
    +
    +        for (Method m : methods)
    +        {
    +            String mname = m.getName().toLowerCase();
    +            if (mname.equals(name)
    +                || mname.equals(get)
    +                || mname.equals(set)
    +                || mname.equals(is)
    +                || mname.equals(MAIN))
    +            {
    +                Class<?>[] types = m.getParameterTypes();
    +                ArrayList<Object> xargs = new
    ArrayList<Object>(args);
    +
    +                // pass command name as argv[0] to main, so it
    can handle
    +                // multiple commands
    +                if (mname.equals(MAIN))
    +                {
    +                    xargs.add(0, name);
    +                }
    +
    +                // Check if the command takes a session
    +                if ((types.length > 0)
    + && CommandSession.class.isAssignableFrom(types[0]))
    +                {
    +                    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))
    +                    {
    +                        bestMethod = m;
    +                        bestArgs = parms;
    +                        match = local;
    +                    }
    +                    if (exact)
    +                    {
    +                        break;
    +                    }
    +                }
    +                else
    +                {
    +                    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);
    +            }
    +            catch (InvocationTargetException e)
    +            {
    +                Throwable cause = e.getCause();
    +                if (cause instanceof Exception)
    +                {
    +                    throw (Exception) cause;
    +                }
    +                throw e;
    +            }
    +        }
    +        else
    +        {
    +            // throw new IllegalArgumentException("Cannot find
    command:" + name
    +            // + " with args:" + args);
    +            // { derek
    +            ArrayList<String> list = new ArrayList<String>();
    +            for (Class<?>[] types : possibleTypes)
    +            {
    +                StringBuilder buf = new StringBuilder();
    +                buf.append('(');
    +                for (Class<?> type : types)
    +                {
    +                    if (buf.length() > 1)
    +                    {
    +                        buf.append(", ");
    +                    }
    +                    buf.append(type.getSimpleName());
    +                }
    +                buf.append(')');
    +                list.add(buf.toString());
    +            }
    +
    +            throw new IllegalArgumentException(
    +                String.format("Cannot coerce %s%s to any of %s",
    name, args, list));
    +            // } derek
    +        }
    +    }
    +
    +    /**
    +     * Complex routein to convert the arguments given from the
    command line to
    +     * the arguments of the method call. First, an attempt is
    made to convert
    +     * each argument. If this fails, a check is made to see if
    varargs can be
    +     * applied. This happens when the last method argument is an
    array.
    +     *
    +     * @param session
    +     * @param target
    +     * @param m
    +     * @param types
    +     * @param out
    +     * @param in
    +     * @return
    +     * @throws Exception
    +     */
    +    @SuppressWarnings("unchecked")
    +    private int coerce(
    +        CommandSession session, Object target, Method m,
    +        Class<?> types[], Object out[], List<Object> in)
    +        throws Exception
    +    {
    +        Annotation[][] pas = m.getParameterAnnotations();

            int start = 0;
    -               for (int argIndex = 0; argIndex < pas.length;
    argIndex++) {
    -                       Annotation as[] = pas[argIndex];
    -                       for (int a = 0; a < as.length; a++) {
    -                if (as[a] instanceof Option) {
    -                                       Option o = (Option) as[a];
    -                                       out[argIndex] =
    coerce(session, target, types[argIndex], o
    -                                                       .dflt());
    +        for (int argIndex = 0; argIndex < pas.length; argIndex++)
    +        {
    +            Annotation as[] = pas[argIndex];
    +            for (int a = 0; a < as.length; a++)
    +            {
    +                if (as[a] instanceof Option)
    +                {
    +                    Option o = (Option) as[a];
    +                    out[argIndex] = coerce(session, target,
    types[argIndex], o.dflt());
                        start = argIndex + 1;
    -                } else if (as[a] instanceof Flag) {
    -                                       out[argIndex] =
    coerce(session, target, types[argIndex],
    -                                                       false);
    +                }
    +                else if (as[a] instanceof Flag)
    +                {
    +                    out[argIndex] = coerce(session, target,
    types[argIndex],
    +                        false);
                        start = argIndex + 1;
    -                               }
    -                       }
    -               }
    -
    -               in = new ArrayList(in);
    -               for (Iterator<Object> i = in.iterator();
    i.hasNext();) {
    -                       Object item = i.next();
    -                       if (item instanceof String) {
    -                               String option = (String) item;
    -                               if (option.startsWith("-")) {
    -                                       for (int argIndex = 0;
    argIndex < pas.length; argIndex++) {
    -                                               Annotation as[] =
    pas[argIndex];
    -                                               for (int a = 0; a
    < as.length; a++) {
    -                            if (as[a] instanceof Option) {
- Option o = (Option) as[a];
    -                                                               if
    (o.name <http://o.name>().equals(option)) {
- i.remove(); - assert i.hasNext(); - Object value = i.next(); - i.remove(); - out[argIndex] = coerce(session, target, - types[argIndex], value);
    -                                                               }
    -                            } else if (as[a] instanceof Flag) {
- Flag o = (Flag) as[a];
    -                                                               if
    (o.name <http://o.name>().equals(option)) {
- i.remove(); - out[argIndex] = coerce(session, target, - types[argIndex], true);
    -                                                               }
    -                                                       }
    -                                               }
    -                                       }
    -                               }
    -                       }
    -               }
    -
    -               int i = start;
    -               while (i < out.length) {
    -                       out[i] = null;
    -                       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;
    -                               } else {
    -                                       out[i] = coerce(session,
    target, types[i], in.get(0));
    -                    if (out[i] != NO_MATCH) {
    +                }
    +            }
    +        }
    +
    +        in = new ArrayList(in);
    +        for (Iterator<Object> itArgs = in.iterator();
    itArgs.hasNext();)
    +        {
    +            Object item = itArgs.next();
    +            if (item instanceof String)
    +            {
    +                String option = (String) item;
    +                if (option.startsWith("-"))
    +                {
    +                    for (int argIndex = 0; argIndex < pas.length;
    argIndex++)
    +                    {
    +                        Annotation as[] = pas[argIndex];
    +                        for (int a = 0; a < as.length; a++)
    +                        {
    +                            if (as[a] instanceof Option)
    +                            {
    +                                Option o = (Option) as[a];
    +                                if (o.name
    <http://o.name>().equals(option))
    +                                {
    +                                    itArgs.remove();
    +                                    assert itArgs.hasNext();
    +                                    Object value = itArgs.next();
    +                                    itArgs.remove();
    +                                    out[argIndex] =
    coerce(session, target,
    +                                        types[argIndex], value);
    +                                }
    +                            }
    +                            else if (as[a] instanceof Flag)
    +                            {
    +                                Flag o = (Flag) as[a];
    +                                if (o.name
    <http://o.name>().equals(option))
    +                                {
    +                                    itArgs.remove();
    +                                    out[argIndex] =
    coerce(session, target,
    +                                        types[argIndex], true);
    +                                }
    +                            }
    +                        }
    +                    }
    +                }
    +            }
    +        }
    +
    +        int i = start;
    +        while (i < out.length)
    +        {
    +            out[i] = null;
    +            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;
    +                }
    +                else
    +                {
    +                    out[i] = coerce(session, target, types[i],
    in.get(0));
    +                    if (out[i] != NO_MATCH)
    +                    {
                            in.remove(0);
                        }
    -                               }
    +                }

    -                               if (out[i] == NO_MATCH) {
    -                                       // Failed
    -                                       // No match, check for varargs
    -                                       if (types[i].isArray() &&
    i == types.length - 1) {
    -                                               // Try to parse
    the remaining arguments in an array
    -                                               Class<?> component
    = types[i].getComponentType();
    -                                               Object components
    = Array.newInstance(component, in.size());
    -                                               int n = i;
    -                                               while (in.size() >
    0) {
    -                                                       Object t =
    coerce(session, target, component, in.remove(0));
    -                                                       if (t ==
    NO_MATCH) {
- return -1;
    -                                                       }
- Array.set(components, i - n, t);
    -                                                       i++;
    -                                               }
    -                                               out[n] = components;
    -                                               // Is last
    element, so we will quite hereafter
    -                                               // return n;
    -                                               if (in.size() == 0) {
    -                                                       ++i;
    -                                               }
    -                                               return i; // derek
    - return number of args converted
    -                                       }
    -                                       return -1;
    -                               }
    -                               i++;
    -                       } catch (Exception e) {
    -                               System.err.println("Reflective:" + e);
    -                               e.printStackTrace();
    -
    -                               // should get rid of those
    exceptions, but requires
    -                               // reg ex matching to see if it
    throws an exception.
    -                               // dont know what is better
    -                               return -1;
    -                       }
    -               }
    -               return i;
    -       }
    -
    -       Object coerce(CommandSession session, Object target,
    Class<?> type,
    -                       Object arg) throws Exception {
    -               if (arg == null) {
    -                       return null;
    -               }
    -
    -               if (type.isAssignableFrom(arg.getClass())) {
    -                       return arg;
    -               }
    -
    -               Object converted = session.convert(type, arg);
    -               if (converted != null) {
    -                       return converted;
    -               }
    -
    -               String string = arg.toString();
    -               if (type.isAssignableFrom(String.class)) {
    -                       return string;
    -               }
    -
    -               if (type.isArray()) {
    -                       // Must handle array types
    -                       return NO_MATCH;
    -               } else {
    -                       if (!type.isPrimitive()) {
    -                               try {
    -                                       return
    type.getConstructor(String.class)
- .newInstance(string);
    -                               } catch (Exception e) {
    -                                       return NO_MATCH;
    -                               }
    -                       }
    -               }
    -
    -               try {
    -                       if (type == boolean.class) {
    -                               return new Boolean(string);
    -                       } else {
    -                               if (type == byte.class) {
    -                                       return new Byte(string);
    -                               } else {
    -                                       if (type == char.class) {
    -                                               if
    (string.length() == 1) {
    -                                                       return
    string.charAt(0);
    -                                               }
    -                                       } else {
    -                                               if (type ==
    short.class) {
    -                                                       return new
    Short(string);
    -                                               } else {
    -                                                       if (type
    == int.class) {
- return new Integer(string);
    -                                                       } else {
    -                                                               if
    (type == float.class) {
- return new Float(string);
    -                                                               }
    else {
- if (type == double.class) { - return new Double(string); - } else { - if (type == long.class) { - return new Long(string); - } - }
    -                                                               }
    -                                                       }
    -                                               }
    -                                       }
    -                               }
    -                       }
    -               } catch (NumberFormatException e) {
    -               }
    -
    -               return NO_MATCH;
    -       }
    -
    -       public static boolean hasCommand(Object target, String
    function) {
    -               Method[] methods = target.getClass().getMethods();
    -               for (Method m : methods) {
    -                       if (m.getName().equals(function)) {
    -                               return true;
    -                       }
    -               }
    -               return false;
    -       }
    -}
    +                if (out[i] == NO_MATCH)
    +                {
    +                    // Failed
    +                    // No match, check for varargs
    +                    if (types[i].isArray() && (i == types.length
    - 1))
    +                    {
    +                        // Try to parse the remaining arguments
    in an array
    +                        Class<?> component =
    types[i].getComponentType();
    +                        Object components =
    Array.newInstance(component, in.size());
    +                        int n = i;
    +                        while (in.size() > 0)
    +                        {
    +                            Object t = coerce(session, target,
    component, in.remove(0));
    +                            if (t == NO_MATCH)
    +                            {
    +                                return -1;
    +                            }
    +                            Array.set(components, i - n, t);
    +                            i++;
    +                        }
    +                        out[n] = components;
    +                        // Is last element, so we will quite
    hereafter
    +                        // return n;
    +                        if (in.size() == 0)
    +                        {
    +                            ++i;
    +                        }
    +                        return i; // derek - return number of
    args converted
    +                    }
    +                    return -1;
    +                }
    +                i++;
    +            }
    +            catch (Exception e)
    +            {
    +                System.err.println("Reflective:" + e);
    +                e.printStackTrace();
    +
    +                // should get rid of those exceptions, but requires
    +                // reg ex matching to see if it throws an exception.
    +                // dont know what is better
    +                return -1;
    +            }
    +        }
    +        return i;
    +    }
    +
    +    Object coerce(CommandSession session, Object target, Class<?>
    type,
    +        Object arg) throws Exception
    +    {
    +        if (arg == null)
    +        {
    +            return null;
    +        }
    +
    +        if (type.isAssignableFrom(arg.getClass()))
    +        {
    +            return arg;
    +        }
    +
    +        Object converted = session.convert(type, arg);
    +        if (converted != null)
    +        {
    +            return converted;
    +        }
    +
    +        String string = arg.toString();
    +        if (type.isAssignableFrom(String.class))
    +        {
    +            return string;
    +        }
    +
    +        if (type.isArray())
    +        {
    +            // Must handle array types
    +            return NO_MATCH;
    +        }
    +        else
    +        {
    +            if (!type.isPrimitive())
    +            {
    +                try
    +                {
    +                    return
    type.getConstructor(String.class).newInstance(string);
    +                }
    +                catch (Exception e)
    +                {
    +                    return NO_MATCH;
    +                }
    +            }
    +        }
    +
    +        try
    +        {
    +            if (type == boolean.class)
    +            {
    +                return new Boolean(string);
    +            }
    +            else
    +            {
    +                if (type == byte.class)
    +                {
    +                    return new Byte(string);
    +                }
    +                else
    +                {
    +                    if (type == char.class)
    +                    {
    +                        if (string.length() == 1)
    +                        {
    +                            return string.charAt(0);
    +                        }
    +                    }
    +                    else
    +                    {
    +                        if (type == short.class)
    +                        {
    +                            return new Short(string);
    +                        }
    +                        else
    +                        {
    +                            if (type == int.class)
    +                            {
    +                                return new Integer(string);
    +                            }
    +                            else
    +                            {
    +                                if (type == float.class)
    +                                {
    +                                    return new Float(string);
    +                                }
    +                                else
    +                                {
    +                                    if (type == double.class)
    +                                    {
    +                                        return new Double(string);
    +                                    }
    +                                    else
    +                                    {
    +                                        if (type == long.class)
    +                                        {
    +                                            return new Long(string);
    +                                        }
    +                                    }
    +                                }
    +                            }
    +                        }
    +                    }
    +                }
    +            }
    +        }
    +        catch (NumberFormatException e)
    +        {
    +        }
    +
    +        return NO_MATCH;
    +    }
    +
    +    public static boolean hasCommand(Object target, String function)
    +    {
    +        Method[] methods = target.getClass().getMethods();
    +        for (Method m : methods)
    +        {
    +            if (m.getName().equals(function))
    +            {
    +                return true;
    +            }
    +        }
    +        return false;
    +    }
    +}
    \ No newline at end of file

    Modified:
    felix/trunk/gogo/runtime/src/main/java/org/osgi/service/command/Flag.java
    URL:
    
http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/osgi/service/command/Flag.java?rev=940623&r1=940622&r2=940623&view=diff
    
<http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/osgi/service/command/Flag.java?rev=940623&r1=940622&r2=940623&view=diff>
    
==============================================================================
    ---
    felix/trunk/gogo/runtime/src/main/java/org/osgi/service/command/Flag.java
    (original)
    +++
    felix/trunk/gogo/runtime/src/main/java/org/osgi/service/command/Flag.java
    Mon May  3 21:18:10 2010
    @@ -25,6 +25,7 @@ import java.lang.annotation.Target;

     @Retention(RetentionPolicy.RUNTIME)
     @Target({ElementType.PARAMETER})
    -public @interface Flag {
    -       String name();
    -}
    +public @interface Flag
    +{
    +    String name();
    +}
    \ No newline at end of file

    Modified:
    felix/trunk/gogo/runtime/src/main/java/org/osgi/service/command/Option.java
    URL:
    
http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/osgi/service/command/Option.java?rev=940623&r1=940622&r2=940623&view=diff
    
<http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/osgi/service/command/Option.java?rev=940623&r1=940622&r2=940623&view=diff>
    
==============================================================================
    ---
    felix/trunk/gogo/runtime/src/main/java/org/osgi/service/command/Option.java
    (original)
    +++
    felix/trunk/gogo/runtime/src/main/java/org/osgi/service/command/Option.java
    Mon May  3 21:18:10 2010
    @@ -25,7 +25,8 @@ import java.lang.annotation.Target;

     @Retention(RetentionPolicy.RUNTIME)
     @Target({ElementType.PARAMETER})
    -public @interface Option {
    -       String name();
    -       String dflt();
    -}
    \ No newline at end of file
    +public @interface Option
    +{
    +    String name();
    +    String dflt();
    +}



Reply via email to